Docker Community Forums

Share and learn in the Docker community.

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”]

You must tell the containers the network “foo_network”. The External flag says that the containers are not accessible from outside. Of course you don’t have to bet, but I thought as an example it might be quite good.

And because of the “links” function look here Link

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
networks:
- foo_network

storage:
image: busybox:1.31.0
container_name: fake_storage
networks:
- foo_network

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

dependencies:
image: busybox:1.31.0
container_name: fake_dependencies
networks:
- foo_network

networks:
foo_network:
external: false

Hi Lewish, thanks for the suggestion. I have tried that but unfortunately it didn’t work :frowning:

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.

Oh it working. Many thanks for your help!