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.

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.

Hello everyone , I am facing the same problem .

I have three servers namely- chatbot_parser, shrug_command and email_Server. If I am not using docker they are working perfectly. But when I am connecting them using docker compose then problem starts. When chatbot_parser server tries to communicate to email_server or shrug_command i get 500 connection error.

This is the error

requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5051): Max retries exceeded with url: /execute (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb0ec5eb2b0>: Failed to establish a new connection: [Errno 111] Connection refused'))

My docker-compose.yaml file is below

version: '3.8'

networks:
  mynetwork:

services:
  chatbot_parser:
    build:
      context: ./chatbot_parser
      dockerfile: Dockerfile
    ports:
      - "5050:5050" 
    networks:
      - mynetwork
    volumes:
      - ./chatbot_parser:/app
    environment:
      - DATABASE_URL=${DATABASE_URL}
    depends_on:
      - shrug_command
      - postgres_container
      - email_server

  shrug_command:
    build:
      context: ./shrug_command
      dockerfile: Dockerfile
    ports:
      - "5051:5051" 
    networks:
      - mynetwork
    volumes:
      - ./shrug_command:/app
  
  email_server:
    build:
      context: ./email_server
      dockerfile: Dockerfile
    ports:
      - "5055:5055" 
    networks:
      - mynetwork
    
  
  postgres_container:
    image: postgres
    restart: always
    container_name: postgres_container
    environment:
      POSTGRES_DB: chatbot
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: example
    ports:
      - "5432:5432"
    networks:
      - mynetwork
    volumes:
      - ./postgres_data:/var/lib/postgresql/data

When I tried to inspect the network I am getting this output which is not recognizing any containers in it, although three containers are running

docker network inspect mynetwork
[
    {
        "Name": "mynetwork",
        "Id": "8d6cf6801ad1216f39cff65734b6e9940170fe5a3e12d465f3accf6b2f65b425",
        "Created": "2024-04-20T07:50:29.867409454Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

I tried to use the solutions mentioned in this thread earlier but they are also not working.

localhost:5051 is only valid inside the shrug_command container. Every other container in the network needs to use shrug_command:5051 instead. Of course localhost:5051 will work on the host as well because you published the container port 5051 on the host port 5051.

It is always funny how people say they tried everything, but completely skip the part where they actually show how they actually tried it. This would allow other people to immediately see the issue… But mo, instead we get an “I tried every mentioned solution” :smiley:

I agree, the main problem is what @meyay already pointed out which I mentioned just before your question.

In your case it is shrug_command

You are checking a different network. You probably created mynetwork but no container is connected to that as compose will create new networks prefixed with the project name. So you should have a <projectname>_mynetwork as well but that is not what you inspected. If you want to connect to external networks, you need to explicitely configure that:

networks:
  mynetwork:
    external: true

.