Docker not mapping port using gunicorn

2019-06-28 02:55发布

问题:

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?

回答1:

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.



标签: docker ports