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

2

6

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?

Horology answered 26/12, 2014 at 17:33 Comment(0)
I
5

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.

Itinerant answered 26/12, 2014 at 18:13 Comment(3)
@Mykola Gurov :I am facing same problem in docker-compose, although I have exposed the required ports.Schadenfreude
I'm in the same boat also. Were you able to find a solution?Edmead
I wanted to alias varnish as api.localhost.com but when I curl it from another container it won't, because curl was using 80, because I thought that 80 is the port I was exposing, but when you are inside the container and you want to use the alias, you should use the port that is listening inside the varnish container which was 6081 and not the exposed one, hope it helps.Reify
G
0

Just make sure your application is listening on the loopback address 0.0.0.0 rather than localhost or 127.0.0.1.

In my case, it was a compose of two containers: golang http app & mongodb. While I was able to ping my app from the mongodb container, I wasn't able to curl it.

Courtesy: https://www.reddit.com/r/docker/comments/16o8wwv/comment/k1jblv9/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

Gauhati answered 24/3 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.