Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote access to webserver in docker container

I've started using docker for dev, with the following setup:

  • Host machine - ubuntu server.
  • Docker container - webapp w/ tomcat server (using https).

As far as host-container access goes - everything works fine. However, I can't manage to access the container's webapp from a remote machine (though still within the same network).

When running

docker port <container-id> 443

output is as expected, so docker's port binding seems fine.

172.16.*.*:<random-port>

Any ideas?

Thanks!

like image 429
tomper Avatar asked Feb 23 '14 15:02

tomper


People also ask

How do I access the web interface of a Docker container?

docker build -t chrome-vnc . Once the image has been fully built and the Docker container is running you can visit http://localhost:8080/vnc.html where you will be prompted to insert the password specified in the docker file: password1 by default.

Can you remote into a Docker container?

That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.

Can we open browser in Docker container?

Open https://localhost:5800 and access the firefox over the browser. Click on “Dashboard” to see what percentage of resources this Docker container is consuming: Click on the console sign just near to the stats to open browser.


3 Answers

If your container was built with a Dockerfile that has an EXPOSE statement, e.g. EXPOSE 443, then you can start the container with the -P option (as in "publish" or "public"). The port will be made available to connections from remote machines:

$ docker run -d -P mywebservice

If you didn't use a Dockerfile, or if it didn't have an EXPOSE statement (it should!), then you can also do an explicit port mapping:

$ docker run -d -p 80 mywebservice

In both cases, the result will be a publicly-accessible port:

$ docker ps
9bcb… mywebservice:latest … 0.0.0.0:49153->80/tcp …

Last but not least, you can force the port number if you need to:

$ docker run -d -p 8442:80 mywebservice

In that case, connecting to your Docker host IP address on port 8442 will reach the container.

like image 64
jpetazzo Avatar answered Oct 22 '22 09:10

jpetazzo


I figured out what I missed, so here's a simple flow for accessing docker containers webapps from remote machines:

Step #1 : Bind physical host ports (e.g. 22, 443, 80, ...) to container's virtual ports. possible syntax:

    docker run -p 127.0.0.1:443:3444 -d <docker-image-name>

(see docker docs for port redirection with all options)

Step #2 : Redirect host's physical port to container's allocated virtual port. possible (linux) syntax:

    iptables -t nat -A PREROUTING -i <host-interface-device> -p tcp --dport <host-physical-port> -j REDIRECT --to-port <container-virtual-port>

That should cover the basic use case.

Good luck!

like image 36
tomper Avatar answered Oct 22 '22 08:10

tomper


Correct me if I'm wrong but as far as I'm aware docker host creates a private network for it's containers which is inaccessible from the outside. That said your best bet would probably be to access the container at {host_IP}:{mapped_port}.

like image 29
Evgeny Chernyavskiy Avatar answered Oct 22 '22 10:10

Evgeny Chernyavskiy