Reputation: 27539
Is there a way to define a Docker Compose service such that it is only brought up
when you explicitly request it?
That is to say:
docker-compose up
would not start it, but
docker-compose up optional_service
would.
Upvotes: 62
Views: 25534
Reputation: 986
Docker compose now supports profiles, which allow you to a) disable one or more services by default, and b) enable them when docker compose is ran with the corresponding --profile
argument.
This is done by adding a profiles
key to your service in the docker-compose.yml
file. Services that have don't have this key are always started by docker-compose (for all profiles).
For example, with the following docker-compose.yml file
version: '3.9'
services
app_service:
# ...
test_service:
profiles:
- testing
# ...
Running docker-compose up
will only start the app_service
, and running docker-compose --profile testing up
will start both the app_service
and the test_service
.
Multiple profiles can be specified by passing multiple --profile
flags.
More info can be found here: https://docs.docker.com/compose/profiles/
Edit: I had to update to docker engine v20.10.5 for this to work.
Upvotes: 87
Reputation: 1068
The extend
keyword has been removed in version 3.x
.
I faced the same issue and this is what I did:
COMPOSE_FILE
environment variable in a .env
file.docker-compose up
, instead of reading docker-compose.yml
, it will read all the compose files specified in COMPOSE_FILE
variable.For example,
COMPOSE_FILE=docker-compose.yml:./optional/docker-compose.prod.yml
.env
file is auto-read (if located in the same level as docker-compose.yml
), when we invoke docker-compose up
.-f
flag in CLI.Note: separate file names with a
:
in case of linux or;
in case of windows.
Upvotes: 19
Reputation: 9268
One way to achieve that is to define your optional service in a different compose file. Then to start the optional service, run:
$ docker-compose -f docker-compose.yml -f optional-service.yaml up
For example, if I have a docker-compose.yml file that looks like:
version: '2.1'
services:
lb:
image: nginx:1.13
db:
image: redis:3.2.9
I can extend it with an optional-service.yml that looks like:
version: '2.1'
services:
busy:
image: busybox
Notice that both compose files must use the same Compose file version.
You can read more about it in the Compose documentation.
Upvotes: 39