Search code examples
dockerdocker-composedockerfile

Docker compose do not exit depends_on containers on exit. Why?


I'm trying to figure out why docker compose leaves the depends_on containers up when the main container exits. This is my setup:

If I run this command docker compose run --rm --service-ports --build --entrypoint /usr/bin/fish shop-server and then exit the shop-server container, it still remains shop-db up and is not removed (even if the --rm flag is specified).

This is my docker-compose.yml:

version: '3.6'

name: dev

services:
  shop-server:
    container_name: shop-server-${COMPOSE_PROJECT_NAME}
    build:
      context: .
      dockerfile: Dockerfile.${COMPOSE_PROJECT_NAME}
    volumes:
      - ./:/app/
      - ./tmp/fish-shop-server/:/root/.local/share/fish/
      - /tmp/shop_debug.log:/tmp/shop_debug.log
    stdin_open: true
    tty: true
    ports:
      - ${DJANGO_PORT}:${DJANGO_PORT}
    depends_on:
      - shop-db
    env_file: .env

  shop-db:
    container_name: shop-db-${COMPOSE_PROJECT_NAME}
    build:
      context: .
      dockerfile: Dockerfile.db
    volumes:
      - postgres_data:/var/lib/postgresql/data/
      - ./tmp/fish-shop-db/:/root/.local/share/fish/
    ports:
      - ${SQL_PORT}:${SQL_PORT}
    env_file: .env

volumes:
  postgres_data:

What am I doing wrong?


Solution

  • You are not doing anything wrong here, but depends_on does not intend to shut down depending on services when you exit another one.

    As stated in the Docker Documentation, depends_on is used to control the startup order in a compose file containing multiple services.

    Compose guarantees dependency services have been started before starting a dependent service. Compose waits for dependency services to be "ready" before starting a dependent service.