35

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.

4 Answers 4

31

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
2
  • 1
    deploy key used to be only for swarm. (See here, for example: github.com/docker/compose/issues/4672) Does Compose support it now? I could not find it in docs, but in my experiments, looks like it does... Still better to have some doc to refer.
    – C-F
    Commented Mar 7, 2023 at 21:10
  • You answered your own question . . . note that six year old issue is . . . closed. Follow it through and you can find the release where it became supported?
    – Matt Blaha
    Commented Mar 9, 2023 at 0:56
4

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

6
  • 5
    That would still consume resources by booting up an alpine linux. Not the ideal solution.
    – Amir
    Commented Apr 9, 2020 at 20:06
  • Of course, it will. ~35MB RAM. Ideally, you wouldn't have null services in your compose file.
    – Pian0_M4n
    Commented Jul 17, 2020 at 13:41
  • FYI the rasa-production depends_on would still be in play, which is probably not what anyone expects. Commented Jul 21, 2021 at 11:02
  • this will not disable the service, just make the service unusable.
    – Theo
    Commented Sep 20, 2021 at 8:40
  • 1
    Work for me. I'd add a restart: "no" in case it has the restart policy specified in one of the overridden yml files.
    – cosmin
    Commented Jul 14, 2022 at 7:29
3

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.