Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container published ports not accessible?

So here is the situation, I have a container running built with this dockerfile:

FROM python:2-onbuild
EXPOSE 8888
CMD [ "nohup", "mock-server", "--dir=/usr/src/app", "&" ]

I run it with this command:

 docker build -t mock_server .
 docker run -d -p 8888:8888 --name mocky mock_server

I am using it on a mac so boot2docker is going and I hit it from the boot2docker ip on 8888. I tried boot2docker ssh and hitting the container from there. I ran docker exec -it mocky bash and ps aux shows:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.9 113316 18576 ?        Ss   15:16   0:00 /usr/local/bin/python2 /usr/local/bin/mock-server --dir=/usr/src/app &
root         5  1.6  0.1  21916  3440 ?        Ss   17:52   0:00 bash
root         9  0.0  0.1  19180  2404 ?        R+   17:53   0:00 ps aux

When I cURL it:

curl -I -XGET localhost:8888/__manage
HTTP/1.1 200 OK
Content-Length: 183108
Set-Cookie: flash_msg_success=; expires=Thu, 04 Sep 2014 17:54:58 GMT; Path=/
Set-Cookie: flash_msg_error=; expires=Thu, 04 Sep 2014 17:54:58 GMT; Path=/
Server: TornadoServer/4.2.1
Etag: "efdb5b362491b8e4b8347b97ccafeca02db8d27d"
Date: Fri, 04 Sep 2015 17:54:58 GMT
Content-Type: text/html; charset=UTF-8

So I the app is running inside the container but I can't get anything from outside it. What can be done here?

like image 990
Hasen Avatar asked Sep 04 '15 17:09

Hasen


People also ask

How do I expose Docker container ports?

There are several ways to do this: you can expose a port via the --expose flag at runtime, or include an EXPOSE instruction in the Dockerfile. You can also publish ports by using the -p or -P flags in the Docker run string. There's also container linking via --link .

How do I expose Docker port to public?

In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

How do you expose a port 8080 Docker?

To publish a port for our container, we'll use the --publish flag ( -p for short) on the docker run command. The format of the --publish command is [host_port]:[container_port] . So if we wanted to expose port 8080 inside the container to port 3000 outside the container, we would pass 3000:8080 to the --publish flag.

How does Docker exposing ports work?

What Is Docker Expose Port? This tells Docker your webserver will listen on port 80 for TCP connections since TCP is the default. For UDP, specify the protocol after the port. For more than one port, you can list EXPOSE more than once.


2 Answers

First guess is the python program is explicitly binding to the loopback IP address 127.0.0.1 which disallows any remote connections. Check the docs for that python mock tornado server for something like --bind=0.0.0.0 and adjust accordingly.

You can confirm if this is the case by doing a docker exec and in the container running netstat -ntlp | grep 8888 and seeing which IP is bound. If it's 127.0.0.1, that confirms that is indeed the problem.

like image 103
Peter Lyons Avatar answered Nov 13 '22 08:11

Peter Lyons


Docker runs on top of an OS and docker machine has its own ip address. One possible reason why the port is not accessible is that you are using localhost which is trying to hit 127.0.0.1: but your docker machine might be running another ip address hence by replacing the ip address your curl should work.

$ docker-machine ip default

This should give you docker machine's ip address replace it with localhost.

like image 27
user1775015 Avatar answered Nov 13 '22 07:11

user1775015