Search code examples
docker-composeautomated-tests

Get docker-compose up to only run certain containers


So i currently can use "docker-compose up test" which only runs my database and my testing scripts. I want to be able to us say docker-compose up app" or something like that that runs everything besides testing. That way Im not running unnecessary containers. Im not sure if theres a way but thats what I was wondering. If possible Id appreciate some links to some that already do that and I can figure out the rest. Basically can I only run certain containers with a single command without running the others.

Yaml

version: '3'
services:
  webapp:
    build: ./literate-app 
    command: nodemon -e vue,js,css start.js
    depends_on:
      - postgres
    links:
      - postgres
    environment:
      - DB_HOST=postgres
    ports:
     - "3000:3000"
    networks:
      - literate-net


  server:
    build: ./readability-server
    command: nodemon -L --inspect=0.0.0.0:5555 server.js
    networks:
      - literate-net


  redis_db:
    image: redis:alpine
    networks:
      - literate-net


  postgres:
    restart: 'always'
    #image: 'bitnami/postgresql:latest'
    volumes:
     - /bitnami
    ports:
      - "5432:5432"
    networks:
      - literate-net
    environment:
      - "FILLA_DB_USER=my_user"
      - "FILLA_DB_PASSWORD=password123"
      - "FILLA_DB_DATABASE=my_database"
      - "POSTGRES_PASSWORD=password123"
    build: './database-creation'


  test: 
    image: node:latest
    build: ./test
    working_dir: /literate-app/test
    volumes:
      - .:/literate-app
    command:
      npm run mocha
    networks:
      - literate-net
    depends_on:
      - postgres
    environment:
      - DB_HOST=postgres


networks:
  literate-net:
    driver: bridge

I can run docker-compose up test

Which only runs the postgres. Though I'd like to be able to just run my app without having to run my testing container.

Edit

Thanks to @ideam for the link

I was able to create an additional yaml file for just testing. For those that dont want to look it up simply create a new yaml file like so

docker-compose.dev.yml

replace dev with whatever you like besides override which causes docker-compose up to automatically run that unless otherwise specified

To run the new file simply call

docker-compose -f docker-compose.dev.yml up

The -f is a flag for selecting a certain file to run. You can run multiple files to have different enviornments set-up

Appreciate the help


Solution

  • Maybe you want to share your docker-compose.yml for a better answer than this.

    For reusing docker-compose configurations have a look at https://docs.docker.com/compose/extends/#example-use-case which explains the combination of multiple configuration files for reuse of configs for different use cases (test, production, etc.)