Remove service on docker-compose through overriding

13.4k Views Asked by At

I want to remove a container defined in docker-compose.yml file when we run in composition/override with another file docker-compose.prod.yml, by example:

# docker-compose.yml
version: 2
services: 

  www:
    image: php56

  db_for_development:
    image: mariadb

override with:

# docker-compose.prod.yml
version: 2
services: 

  www:
    image: php70

  db_for_development:
    [control: override-and-remove] # hypothesis

Then, when running:

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
docker-compose -f docker-compose.yml -f docker-compose.prod.yml ps

Actually, i have www and db_for_development together.

I want only www container, not others.

5

There are 5 best solutions below

6
On

Let's say you want to remove (disable) a service that's defined in your compose file

Contents of docker-compose.yml

version: "3.4"

services:
  app:
    restart: always
    image: "rasa/rasa-x-demo:${RASA_X_DEMO_VERSION}"
    expose:
      - "5055"
    depends_on:
      - rasa-production

Contents of docker-compose.override.yml

version: "3.4"

services:
  app:
    image: alpine:latest
    command: "true"
    entrypoint: "true"

Done. Now your container will still launch but it's disabled using an empty image

0
On

This is not possible. Your only real option would be to specify the services (selectively) when running docker-compose up.

2
On

You're going about this backwards

docker-compose.yml -> specify all services that will be always running

docker-compose.override.yml -> gets picked up automatically, usually used for development

docker-compose.*.yml -> special cases

So, in your case:

You don't remove a container defined in docker-compose.yml, you add it by override with another file or customize it with docker-compose.prod.yml, by example:

docker-compose.yml -> this is the base

version: 2
services: 
  www:
    image: php56

docker-compose.override.yml -> this is dev

version: 2
services: 
  db_for_development:
    image: mariadb

docker-compose.production.yml -> this is prod

version: 2
services: 
  www:
    environment:
      - APP_ENV=production
    env_file:
      - /home/ubuntu/production-app

docker-compose.admin.yml -> this is for the dba

version: 2
services: 
  adminer:
  image: adminer
  restart: always
  ports:
    - 8080:8080

instructions:

For development, docker-compose.yml and docker-compose.override.yml will be used just by running

$ docker-compose up

Production, manually specify both files

$ docker-compose -f docker-compose.yml -f docker-compose.production.yml up --remove-orphans

If you want to bring also bring adminer up (not recommended for production, but sometimes needed anyways)

$  docker-compose -f docker-compose.yml -f docker-compose.production.yml -f docker-compose.admin.yml up

Lastly, when you're done with adminer, just run the production command again this will leave adminer running as an orphan, and you do not want that. That's why the flag --remove-orphans is for

0
On

Another workaround is possible by using the profile feature added in docker compose version '3.9'

The idea is that in your docker-compose.prod.yml, set a dummy profile for the service to be removed, and call docker compose up without specifying that profile. So in your case:

# docker-compose.prod.yml
version: '3.9' # make sure the version is at least 3.9
services: 
  db_for_development:
    profiles:
      - dummy-profile

Running docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d will not bring up the db_for_development at all.

More examples of how profile works can be found in the official documentation

2
On

You may have to switch to version: 3 to do this, I believe on version: 2 you can use the "scale" parameter but I'm not 100% sure.

Anyways, you can override the "replicas" parameter like this:

# docker-compose.prod.yml
version: "3"
services: 

  db_for_development:
    deploy:
      replicas: 0