31

I have a docker container built with "EXPOSE 8000" among its instructions. I started the process like this:

sudo docker run -t -i -P imagename

The process in the container is listening on 8000.

# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:8000          *:*                     LISTEN     
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
# 

On the host (i.e., outside the container), I see that port 49164 is bound to container port 8000:

[S-22]jeff@siegfried:~ $ sudo docker ps 
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                     NAMES
0b0e333c6ec9        lkz:latest                 "/bin/bash"         About an hour ago   Up 6 minutes        0.0.0.0:49164->8000/tcp   lkxyz__2015-01-18_202737   
[S-22]jeff@siegfried:~ $ 

Inded, docker inspect says (among other things)

"NetworkSettings": {
    "Bridge": "docker0",
    "Gateway": "172.17.42.1",
    "IPAddress": "172.17.0.16",
    "IPPrefixLen": 16,
    "PortMapping": null,
    "Ports": {
        "8000/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "49164"
            }
        ]
    }
},

And yet, I can't talk to the container. Outside,

[S-22]jeff@siegfried:~ $ telnet localhost 49164
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
1,[S-22] jeff@siegfried:~ $ 

while inside,

# telnet localhost 8000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /

[18/Jan/2015 23:00:59] "GET /" 200 4066
<!DOCTYPE html>
<html>
<head>
...

I would expect the telnet to 49164 on the outside to return html as it does on the inside.

Any suggestions?

1
  • Aha, this might be that the container is listening really only to localhost. Doing the same thing on host siegfried, I can connect with telnet localhost 8000 but not with telnet siegfried 8000. OK, so now how do I fix this...
    – jma
    Jan 18, 2015 at 22:30

3 Answers 3

47

You probably want to have the service you're running in the container to listen on 0.0.0.0 rather than 127.0.0.1

5
  • this was a lifesaver. Using docker for Apache Spark, spark://0.0.0.0:7077
    – javaNoober
    Jun 15, 2017 at 3:25
  • 1
    how do we do this in windows 10 with docker? listen to 0.0.0.0?
    – klewis
    Jul 19, 2017 at 22:22
  • 1
    In our case the port was listening on 0.0.0.0, but we needed to reset Docker to factory defaults (Docker Preferences/Bomb tab) and reinstall images.
    – dfdumaresq
    Dec 4, 2017 at 17:46
  • Just to confirm, yes this is the behaviour you'll see when Docker is accepting incoming connections but the service you're running inside Docker is rejecting them. I also had to change the Dockerised service to listen on 0.0.0.0 to fix the problem, but the Docker config itself was otherwise unchanged.
    – Malvineous
    Oct 8, 2018 at 0:33
  • This answer goes into details as to why this works
    – Liam
    Aug 15, 2019 at 10:53
0

Try to connect 172.17.0.16:8000 from the host. I had a similar problem with nginx. I replaced 127.0.0.1:8000 to 172.17.0.16:8000 inside the container and it worked for me.

0

bind port 8000 of the container to TCP port 8000

docker run -d -p 8000:8000 image-name

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.