Docker – Webserver in docker container is not reachable via the internet

docker

I've been reading: https://docs.docker.com/engine/userguide/networking/#user-defined-networks

I've created my own network (docker network create --driver bridge devils_network):

441be50f3792        bridge              bridge              local
0d73f7c6fe00        devils_network      bridge              local
8e189dda9fef        host                host                local
5ebca4a1e514        none                null                local

I run the container so:

docker run -it -d -v ~/dockervolume/deus:/srv/www --name deus --hostname deus --network=devils_network -p 80:8080 karl/node

And in the dockerfile expose port 8080:

EXPOSE  8080

I've attached a terminal session to the container and inspected the container to make sure the webserver is up and running:

root        17  0.0  0.1   4508   660 ?        S    22:46   0:00 sh -c NODE_PATH="$(pwd)" NODE_ENV=production node hello_world
root        19  0.0  4.3 882896 22004 ?        Sl   22:46   0:00 node hello_world

The webserver is utilizing port 8080 inside the docker container.

I'm running on a DigitalOcean droplet. If I start the server up directly on the host and not via a docker container it works.

Best Answer

  1. You need to inverse the mapping -p 80:8080 to -p 8080:80 The 1st port is on the host and the second is the container's.
  2. On your work/home network, make sure to redirect, in your router/fw, the port 8080 to your docker host port 8080.

This should work: Here is the same steps I performed with my nginx container:

docker network create --driver bridge devils_network
4320854ef67c5489848c1e1f14ffaf4d65183c5e3fac5f655c038bb15aa50df7  

docker run -it -v ~/dockervolume/deus:/usr/share/nginx/html --name deus --hostname deus --network=devils_network -p 8080:80 ajnouri/nginx
root@deus:/# 

Copied an index.php file into ~/dockervolume/deus:

And browsed my public IP from my phone:

enter image description here

Related Topic