Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker listening in container but not answering outside, why?

Tags:

docker

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?

like image 363
jma Avatar asked Jan 18 '15 22:01

jma


People also ask

Can Docker container connect to outside world?

Your Docker container can connect to the outside world, but the outside world cannot connect to the container. To make the ports accessible for external use or with other containers not on the same network, you will have to use the -P (publish all available ports) or -p (publish specific ports) flag.

How do you access a container from the outside?

If you go for the first option, you will be able to access the container at http://W:8080 where W is the IP address or hostname of the Windows host. If you opt for the second, you will be able to access the container at http://L:8080 where L is the IP address or hostname of the Linux VM.

What happens when you press Ctrl P Q inside the container in Docker?

To detach from a running container, use ^P^Q (hold Ctrl , press P , press Q , release Ctrl ).

What is Docker command for exposing to outside world?

By default docker is secure and isolated. We need to expose the port to allow broadcast to be received outside the container. $ docker run -d --expose 8080 sending_broadcast.


1 Answers

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

like image 188
Evgeny Chernyavskiy Avatar answered Oct 04 '22 11:10

Evgeny Chernyavskiy