Skip to content
Advertisement

How to access a port exposed from a docker container?

$ docker container ls --format "table {{.ID}}t{{.Names}}t{{.Ports}}" -a
CONTAINER ID        NAMES               PORTS
ae87d83af7d3        hopeful_engelbart   
d13e260c4dec        unruffled_bouman    
db2c482de210        jenkinsci           0.0.0.0:8080->8080/tcp, 50000/tcp
cd201cbd413e        xyz                 0.0.0.0:5000->5000/tcp
c64c32ac68b8        pqr              
$ docker container ls -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                      PORTS                               NAMES
ae87d83af7d3        442c97a73937          "/bin/bash"              11 minutes ago      Exited (0) 9 minutes ago                                        hopeful_engelbart
d13e260c4dec        442c97a73937          "/bin/bash"              27 minutes ago      Exited (0) 24 minutes ago                                       unruffled_bouman
db2c482de210        jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   3 days ago          Up 41 minutes               0.0.0.0:8080->8080/tcp, 50000/tcp   jenkinsci
cd201cbd413e        442c97a73937          "bash"                   3 days ago          Up 7 minutes                0.0.0.0:5000->5000/tcp        xyz
c64c32ac68b8        442c97a73937          "bash"                   3 days ago          Exited (0) 2 days ago                                           pqr

Above outputs show that the port 5000 has been exposed (I hope).

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' xyz
172.17.0.3

Now when I do from the host machine:

wget -c 172.17.0.3:5000
--2019-12-30 16:26:44--  http://172.17.0.3:5000/
Connecting to 172.17.0.3:5000... failed: Connection refused.

What is the way to access that port since it is exposed and the container is running?

$ wget -c localhost:5000
--2019-12-30 16:41:57--  http://localhost:5000/
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

--2019-12-30 16:41:58--  (try: 2)  http://localhost:5000/
Connecting to localhost (localhost)|127.0.0.1|:5000... connected.
HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers.
Retrying.

Advertisement

Answer

First of all, check if you have an application, listening to the port inside your container. Just try to connect to it from your container:

docker exec xyz wget 127.0.0.1:5000

If it’ll work then you have a problem with port exposing, otherwise, there is no web server running inside your container.

And the error you get

Read error (Connection reset by peer) in headers.

seems to point to the problem in your web server rather then connectivity issues.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement