title: Stop dependee services after tests complete with docker
body: <p>I have created a docker configuration for running integration tests with docker-compose. However, the services the tests depend on stay up after the tests complete. I have tried many ways of running this set-up, including the suggestions in <a href="https://stackoverflow.com/questions/33799885/how-to-stop-all-containers-when-one-container-stops-with-docker-compose">this question</a>, to no avail.</p>
  
  <p>The services can be successfully stopped with <code>docker-compose down</code>, however I would really like them to be stopped automatically upon <code>tests</code> service exit. Using the command <code>docker-compose run tests &amp;&amp; docker-compose down</code> works but I find it rather ugly. A workaround would be to include this in my <code>package.json</code> scripts. However, I am interested to know, if this could be achieved with a single <code>docker-compose</code> command, i.e., changing my <code>docker-compose.yml</code> or <code>Dockerfile</code>.</p>
  
  <p>The project and commit in question can be found <a href="https://github.com/joonashak/vyardage/tree/b09ec06bf2e2485f6fc908d82bd6408bde2ff31e" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Contents of <code>docker-compose.yml</code>:</p>
  
  <pre><code>version: "3.2"
  
  services:
    app:
      build: .
      ports:
        - 3001:3001
      container_name: vyardage-app
      environment:
        - NODE_ENV=development
        - DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
      depends_on:
        - db
  
    test-app:
      build: .
      ports:
        - 3001:3001
      container_name: test-app
      environment:
        - NODE_ENV=test
        - DATABASE_URL=postgresql://postgres:postgres@test-db:5432/postgres
      depends_on:
        - test-db
  
    db:
      image: postgres:12.2
      container_name: vyardage-db
      environment:
        - PGDATA=/data
        - POSTGRES_PASSWORD=postgres
      volumes:
        - ./pg_data:/data
      ports:
        - 5432:5432
  
    test-db:
      image: postgres:12.2
      container_name: test-db
      environment:
        - POSTGRES_PASSWORD=postgres
      ports:
        - 5432:5432
  
    tests:
      build: ./e2e/
      container_name: vyardage-test
      environment:
        - CYPRESS_BASE_URL=http://test-app:3001
      depends_on:
        - test-app
      volumes:
        - ./e2e/cypress:/e2e/cypress
  </code></pre>
  
  <p>Contents of <code>Dockerfile</code> in root:</p>
  
  <pre><code>FROM node:12.16
  
  # Port can be configure via --build-arg.
  ARG port=3001
  
  # Install frontend dependencies.
  WORKDIR /usr/src/app/client
  COPY client/package.json client/package-lock.json ./
  RUN npm ci
  
  # Install backend dependencies.
  WORKDIR /usr/src/app/server
  COPY server/package.json server/package-lock.json ./
  RUN npm ci
  
  # Build frontend.
  WORKDIR /usr/src/app
  COPY client client/
  WORKDIR /usr/src/app/client
  RUN npm run build
  RUN cp -r build/ ../server/public
  
  # Build backend.
  WORKDIR /usr/src/app
  COPY server server/
  WORKDIR /usr/src/app/server
  RUN npm run build
  
  EXPOSE $port
  
  CMD npm start
  </code></pre>
  
  <p>Thank you for your help!</p>