Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 391
Rutger Luther Avatar asked Apr 14 '19 19:04

Rutger Luther


2 Answers

docker-compose up <service_name> will start only the service you have specified and its dependencies. (those specified in the dependends_on option.)

you may also define multiple services in the docker-compose up command:

docker-compose up <service_name> <service_name>

note - what does it mean "start the service and its dependecies"?

usually your production services (containers) are attached to each other via the dependes_on chain, therefore you can start only the last containers of the chain. for example, take the following compose file:

version: '3.7'
services:
  frontend:
    image: efrat19/vuejs
    ports:
      - "80:8080"
    depends_on:
       - backend
  backend:
    image: nginx:alpine
    depends_on: 
      - fpm
  fpm:
    image: php:7.2
  testing:
    image: hze∂ƒxhbd
    depends_on:
      - frontend

all the services are chained in the depends_on option, while the testing container is down bellow the frontend. so when you hit docker-compose up frontend docker will run the fpm first, then the backend, then the frontend, and it will ignore the testing container, which is not required for running the frontend.

like image 99
Efrat Levitan Avatar answered Oct 16 '22 02:10

Efrat Levitan


Starting with docker-compose 1.28.0 the new service profiles are just made for that! With profiles you can mark services to be only started in specific profiles:

services:
  webapp:
    # ...

  server:
    # ...

  redis_db:
    # ...

  postgres:
    # ...

  test:
    profiles: ["test"]
    # ...
docker-compose up # start only your app services
docker-compose --profile test up # start app and test services
docker-compose run test # run test service
like image 34
acran Avatar answered Oct 16 '22 02:10

acran