Search code examples
linuxdjangocurldockergunicorn

Can't curl linked container in Docker even though I can ping it


I have a Docker container called backend that exposes a port, 8200, and runs a django server behind gunicorn inside of it. This is my Dockerfile:

FROM debian:wheezy

RUN rm /bin/sh && \
    ln -s /bin/bash /bin/sh && \
    apt-get -y update && \
    apt-get install -y -q \
                    curl \
                    procps \
                    python=2.7.3-4+deb7u1 \
                    git \
                    python-pip=1.1-3 \
                    python-dev \
                    libpq-dev && \
    rm -rf /var/lib/{apt,dpkg,cache,log}

RUN pip install virtualenv && \
    virtualenv mockingbird && \
    /bin/bash -c "source mockingbird/bin/activate"

ADD ./requirements.txt /mockingbird/backend/requirements.txt
RUN /mockingbird/bin/pip install -r /mockingbird/backend/requirements.txt

ADD ./src /mockingbird/backend/src

CMD ["/mockingbird/bin/gunicorn", "--workers", "8", "--pythonpath", "/mockingbird/backend/src/", "--bind", "localhost:8200", "backend.wsgi"]

I'm running this container like so:

vagrant@10:~$ sudo docker run --name backend --env-file /mockingbird/apps/backend/env/dev -d --restart always --expose 8200 mockingbird/backend

I know that the django server is up and responding on the correct port by doing the following and getting a response:

vagrant@10:~$ sudo docker exec -it backend /bin/bash
root@b488874c204d:/# curl localhost:8200

I then start a new container linking to backend as follows:

sudo docker run -it --link backend:backend debian:wheezy /bin/bash

But when I try to curl backend, it doesn't work:

root@72946da3dff9:/# apt-get update && apt-get install curl
root@72946da3dff9:/# curl backend:8200
curl: (7) couldn't connect to host

I am, however, able to ping backend:

root@72946da3dff9:/# ping backend
PING backend (172.17.0.41): 48 data bytes
56 bytes from 172.17.0.41: icmp_seq=0 ttl=64 time=0.116 ms
56 bytes from 172.17.0.41: icmp_seq=1 ttl=64 time=0.081 ms

Anyone know anything else I can try to debug why I can't connect to the service running in my linked Docker container? Is there something I am missing here to be able to curl backend:8200 from the linked container?


Solution

  • this might be a problem: "--bind", "localhost:8200" as connections to backend hostname won't be accepted. You might want to change it to "0.0.0.0:8200" or maybe ":8200", depending on the notation supported.