Francesco Bianco
Francesco Bianco

Reputation: 589

Remove service on docker-compose through overriding

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.

Upvotes: 37

Views: 16058

Answers (5)

Carl Walsh
Carl Walsh

Reputation: 6949

In v3 docker compose, removing a YAML property is easy using !reset null syntax:

docker-compose.yml

services: 
  www:
    image: php56
  db_for_development:
    image: mariadb

docker-compose.prod.yml

services: 
  www:
    image: php70
  db_for_development: !reset null 
$ docker compose -f docker-compose.yml -f docker-compose.prod.yml config
name: del-20241206-17-18-2284
services:
  www:
    image: php70
    networks:
      default: null
networks:
  default:
    name: del-20241206-17-18-2284_default

Upvotes: 4

Charles  Chen
Charles Chen

Reputation: 105

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

Upvotes: 4

Matt Blaha
Matt Blaha

Reputation: 977

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

Upvotes: 31

Pian0_M4n
Pian0_M4n

Reputation: 2548

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

Upvotes: 4

yomateo
yomateo

Reputation: 2367

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

Upvotes: 0

Related Questions