Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define optional docker-compose services?

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.

like image 213
Dancrumb Avatar asked Aug 14 '17 18:08

Dancrumb


People also ask

What file does Docker Compose use to define services?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services.

Can you have two Docker compose files?

Using Multiple Docker Compose Files Use multiple Docker Compose files when you want to change your app for different environments (e.g., dev, staging, and production) or when you want to run admin tasks against a Compose application.

Is Docker compose deprecated?

Following the deprecation of Compose on Kubernetes, support for Kubernetes in the stack and context commands in the docker CLI is now marked as deprecated as well.


2 Answers

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.

like image 66
ivan.sim Avatar answered Sep 19 '22 13:09

ivan.sim


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.

like image 44
yoannisj Avatar answered Sep 17 '22 13:09

yoannisj