Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 801
golfadas Avatar asked Jul 10 '18 17:07

golfadas


People also ask

How do I stop a specific service in Docker compose?

There are two steps to stop a docker application containers: First, stop the running containers using docker-compose stop. Second, remove the stopped containers using docker-compose rm -f.

How do you stop a container from exiting Docker compose?

In order to prevent the docker container from exiting immediately after creation, tty should be set to true in the docker-compose. yml file. tty: true in docker-compose corresponds to the docker run -it . With tty: true for the service, the created by docker-compose up -d container status is Up .

How do I quit Containerd?

If you want to stop and exit the container, and are in an interactive, responsive shell - press ctrl+d to exit the session. You could as well type the exit command. TL;DR: press ctrl+c then ctrl+d - that means, keep the ctrl key pressed, type a c, and let go of ctrl. Then the same with ctrl and d.


2 Answers

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
like image 52
BMitch Avatar answered Sep 20 '22 14:09

BMitch


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
like image 21
piotrze Avatar answered Sep 17 '22 14:09

piotrze