Search code examples
dockerubuntu-server

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!


Solution

  • 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!