Containers launched with Docker-Compose cannot connect to each other

1.2k views Asked by At

I have a beginner question with Docker Compose. I am trying to extend the docker-compose-slim.yml example file from Zipkin GitHub repository.

I need to change it so that it can include a simple FastAPI app that I have written. Unfortunately, I cannot make them connect to each other. FastAPI gets rejected when it attempts to send a POST request to the Zipkin container, even though they are both connected to the same network with explicit links and port mapping defined in the YAML file. However, I am able to connect to both of them from the host, however.

Could you please tell me what I have done wrong?

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 YAML file:

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"]
1

There are 1 answers

6
Gregor Wedlich On

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