问题
I find myself in the situation, that I want to disable a service temporarily in a docker-compose file.
Of course I could comment it out, but is there any option to just say "enabled: false" ?
回答1:
You could simply redefine the entrypoint or command in order to replace said command with something which does nothing (/bin/true)
That would make the container exit immediately, doing nothing.
shadi adds the following tips in the comments:
If you don't want the service to get built at all, redefine the build key to point to a
Dockerfilethat only has:
FROM tianon/true
ENTRYPOINT ["/true"]
回答2:
You can do it in a docker-compose.override.yaml file.
This file is automatically read by docker-compose and merged into the main docker-compose.yaml.
If you have it excluded from Git, each developer can tweak the configuration (with a few limitations) without changing the original docker-compose.yaml.
So, service foo can be disabled ad-hoc by redefining its entrypoint in docker-compose.override.yaml:
version: "3"
services:
foo:
entrypoint: ["echo", "Service foo disabled"]
回答3:
I add the following extra line to the service I want to temporarily disable:
command: echo "{put your service name here} disabled"
It starts anyway, but does nothing.
回答4:
There is no way to disable a service defined in Docker compose yaml file. VonC's suggestion is a good workaround Please see below the docker compose documentation for available options https://docs.docker.com/compose/compose-file/
回答5:
I would scale the service to 0 replicas with: deploy: replicas: 0
Unfortunately as the documentation states this only works with docker swarm.
回答6:
Primitive but add a # at the beginning of each line of the service.
来源:https://stackoverflow.com/questions/37254881/is-there-any-way-to-disable-a-service-in-docker-compose-yml