Containers launched with Docker-Compose cannot connect to each other

I have a beginner question with docker-compose. I am trying to extend py_zipkin’s yml file (https://github.com/openzipkin/zipkin/blob/master/docker/examples/docker-compose-slim.yml) to include a simple FastAPI app that I have written, unfortunately they are not able to connect to each other. FastAPI got rejected while trying to POST to zipkin container even though they are both connected to the same network with explicit links and port mapping defined in the yml file. However, I am able to connect to both of them from the host. Could you please tell me what I have done wrong? Thanks a lot

Here is the error message:

Error emitting zipkin trace. ConnectionError(MaxRetryError(“HTTPConnectionPool(host=‘127.0.0.1’, port=9411): Max retries exceeded with url: /api/v2/spans (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x7fce354711c0>: Failed to es tablish a new connection: [Errno 111] Connection refused '))”))

Here is the docker-compose yml:

version: ‘2.4’

services: zipkin: image: openzipkin/zipkin-slim container_name: zipkin environment: - STORAGE_TYPE=mem ports: # Port used for the Zipkin UI and HTTP Api - 9411:9411 depends_on: - storage

storage: image: busybox:1.31.0 container_name: fake_storage

myfastapi: build: . ports: - 8000:8000 links: - zipkin depends_on: - zipkin

dependencies: image: busybox:1.31.0 container_name: fake_dependencies

networks: default: name: foo_network

Here is the Dockerfile:

FROM python:3.8.5 ADD . /app WORKDIR /app

RUN pip install -r requirements.txt

EXPOSE 8000 CMD [“uvicorn”, “wsgi:app”, “–host”, “0.0.0.0”, “–port”, “8000”]

Hint: localhost in a container is local to the container, not local to the host. Use the container or service name to access other containers in the same custom container network - docker-compose will create a default one by default.

1 Like

Oh it working. Many thanks for your help!

how i can do that? please guide me.

Just replace “localhost” with the service name or container name. If the compose service name is “server” and your project name is “myproject” replacing “localhost” with “server” or “myproject-server-1”. If you set the container_name parameter in the compose file, you that value.