Search code examples
dockerdocker-composedocker-network

Why in docker-compose after recreate conteiner i get "Docker cannot link to a non running container"?


I have two conteiners:
docker-compose.yml

version: '3.8'

services:
  db:
    image: postgres:14.1
    container_name: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ......
    network_mode: bridge

  web:
    container_name: web
    build: .
    ........
    network_mode: bridge
    external_links:
      - postgres
    depends_on:
      - db

volumes:
  postgres_data:
    name: postgres_data

After docker-compose up, when i recreate only one container - "db", all works, but i can not connect to conteiner "web", i get error: "Failure Cannot link to a non running container: /postgres AS /web/postgres". In conteiner "web" i call db as host=postgres. What am I doing wrong?


Solution

  • The external_links: setting is obsolete and you don't need it. You can just remove it with no adverse consequences.

    network_mode: bridge and container_name: are also unnecessary, though they shouldn't specifically cause problems; still, I'd delete them. What you show can be reduced to

    version: '3.8'
    
    services:
      db:
        image: postgres:14.1
        volumes:
          - postgres_data:/var/lib/postgresql/data/
        ......
    
      web:
        build: .
        ........
        depends_on:
          - db
    
    volumes:
      postgres_data: # empty
    

    Since Compose creates a network named default for you and attaches containers to it, your application container can still reach the database container using the hostname db. Networking in Compose in the Docker documentation describes this further.