golfadas
golfadas

Reputation: 5531

Docker Compose: Stop "depends_on" service when "parent" finishes

Is it possible to tell docker compose to kill services specified on the depends_on array, after the parent service test finishes without using --abort-on-container-exit?

I'm trying to build a docker file that runs tests from multiple apps, something like:

services:
  test-spa:
    build:
      context: ./spa
      dockerfile: Dockerfile.test
    command: ["yarn", "run", "test", "--watch", "false"]
  lint:
    build:
      context: ./spa
      dockerfile: Dockerfile.test
    command: ["yarn", "run", "lint"]
  build:
    build:
      context: ./spa
      dockerfile: Dockerfile.test
    command: ["yarn", "run", "build", "--prod"]
  test-backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.dev
    entrypoint: [scripts/entrypoint.sh]
    command: bundle exec rails test
    depends_on:
      - db
  db:
    image: postgres

Problem is test-backend starts up db service as a dependency, runs its tests, quits and db never finishes.

Using --abort-on-container-exit is not an option since spa related services might exit before test-backend is over.

Upvotes: 11

Views: 4075

Answers (2)

piotrze
piotrze

Reputation: 489

Oneliner useful when using docker compose for development. It starts an app(with dependencies) and when you hit ctrl+c it stops all services;

docker-compose up app; docker-compose stop

Upvotes: 1

BMitch
BMitch

Reputation: 264376

I do not believe this is a built in feature of docker-compose. But you can easily script a solution, e.g.:

docker-compose up -d db
docker-compose up test-backend
docker-compose down db

Outside of docker-compose, there's also the docker container wait command that lets you hang until a container exits. You can lookup the container name with a docker container ls and filters on a label or other unique value, or you can rely on the predictable container names from compose ($project_$service_$replica). The docker container wait command would allow you to spin up the entire project in a detached state and then wait for one container to exit before proceeding to stop it, e.g.:

docker-compose up -d
docker container wait $(basename $(pwd))_test-backend_1
docker-compose down db

Upvotes: 4

Related Questions