1

I am learning docker and started with a simple python-based web server which replies "Hello World" when called. I have a hard time understanding how to expose specific ports.

Background

# the Dockerfile
FROM ubuntu
RUN apt-get -qq update
RUN apt-get install -y python
RUN echo "Hello world" > index.html
CMD ["/usr/bin/python", "-m", "SimpleHTTPServer"]

By default, this web server exposes port 8000:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

When starting the container I get

# docker run -d  06b5c2fc603e
2ca568ba0799ab00af72f230ec99038e225265a4af073da54026b7a90bacb0c9
# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
2ca568ba0799        06b5c2fc603e        "/usr/bin/python -m    4 seconds ago       Up 3 seconds                                 tender_davinci
# docker inspect 2ca568ba0799 | grep IPAddress
        "IPAddress": "172.17.0.84"

Checking if the port 8000 answers on the assignated IP address - it does.

# curl -XGET 172.17.0.84:8000
Hello world

Now I want to publish this port so that other hosts can access.

# docker run -d -p 8000:8000 06b5c2fc603e
6715c6fd58100c27fbdc26895755e164ae3b241f6bd6e43c331ca44cf388d6f1
# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
6715c6fd5810        06b5c2fc603e        "/usr/bin/python -m    3 seconds ago       Up 3 seconds        0.0.0.0:8000->8000/tcp   cocky_tesla 

Checking from a remote site - it works

# curl -XGET 10.242.136.232:8000
Hello world

Now the problem: this fails when I want to change the published port (from 8000 to 8888):

# docker run -d -p 8000:8888 06b5c2fc603e
0f2e9f79a51e4710f9e8be90376e080928904e6b7320771ca5ad4f829043a6ca
# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
0f2e9f79a51e        06b5c2fc603e        "/usr/bin/python -m    4 seconds ago       Up 3 seconds        0.0.0.0:8000->8888/tcp   adoring_goodall

and access from a remote host

# curl -XGET 10.242.136.232:8888
curl: (7) couldn't connect to host

docker seems to correctly NAT the port (0.0.0.0:8000->8888/tcp) so why isn't it available as soon as it changes from the one exposed by the service in the container?

Note: there are no firewalls / network ACLs which would block the traffic

There were several questions on the topic of networking (an answer to one of them gives a good overview) and the docs are quite clear (particularly with examples) so I am at a loss.

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

7

You have to swap the order of the ports, like this:

# docker run -d -p 8888:8000 06b5c2fc603e

According to docker run documentation here, the first port is the host port and second one is the container port -> hostPort:containerPort

Alex da Silva
  • 4,552
  • 2
  • 17
  • 25