Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker not mapping port using gunicorn

Tags:

docker

ports

I'm running gunicorn inside a docker container. I know this works because sshing into it and curling localhost:8000/things in docker container gives me the response I want, however, I am not able to reach this on my host, despite docker telling me the port has been mapped. What gives?

I ran

docker run -d -p 80:8000 myapp:version1.1 /bin/bash -c 'gunicorn things:app'

docker ps gives me

CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                            NAMES
614df1f2708e        myapp:version1.1  "/bin/bash -c 'gunico"   6 minutes ago       Up 6 minutes        5000/tcp, 0.0.0.0:80->8000/tcp   evil_stallman

On my host, curling locahost/things gives me

curl: (52) Empty reply from server

However, when I docker exec -t -i 614df1f2708e /bin/bash and then curl localhost:8000/things, I succesfully get my correct response.

Why isn't docker mapping my port 8000 correctly?

like image 711
Alexander Kleinhans Avatar asked Sep 29 '16 21:09

Alexander Kleinhans


People also ask

How do I use Docker with port mapping?

Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100. Map UDP port 80 in the container to port 8080 on the Docker host. Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

Does Docker use port 8080?

Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .

How do I expose Docker network to port?

You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls ). You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag.


1 Answers

When you publish a port, Docker will forward requests into the container, but the container needs to be listening for them. The container has an IP address from the Docker network, and your app needs to be listening on that address.

Check your gunicorn bind setting - if it's only listening on 127.0.0.1:8000 then it's not binding to the container's IP address, and won't get requests from outside. 0.0.0.0:8000 is safe as it will bind to all addresses.

like image 152
Elton Stoneman Avatar answered Sep 21 '22 16:09

Elton Stoneman