0

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 this question, to no avail.

The services can be successfully stopped with docker-compose down, however I would really like them to be stopped automatically upon tests service exit. Using the command docker-compose run tests && docker-compose down works but I find it rather ugly. A workaround would be to include this in my package.json scripts. However, I am interested to know, if this could be achieved with a single docker-compose command, i.e., changing my docker-compose.yml or Dockerfile.

The project and commit in question can be found here.

Contents of docker-compose.yml:

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

Contents of Dockerfile in root:

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

Thank you for your help!

basse
  • 1,088
  • 1
  • 19
  • 40
  • 1
    To be honest, I don’t think there is anything wrong with the docket-compose down. You could perhaps just create a .sh script that has all the commands in there and then that. – mvoase Mar 20 '20 at 18:07
  • 1
    Gotcha. My solution for the moment is to use an npm script `"test": "docker-compose build test-app && docker-compose run tests; docker-compose down"`. – basse Mar 20 '20 at 19:28

0 Answers0