Those of us who use docker-compose to manage our fleets of containers generally fall into two camps. Camp A prefers one monolithic docker-compose file per host using docker-compose <command> <service>
to interact with each service. Camp B split up their configuration into multiple files with some kind of logical grouping which makes sense to them.
This post will provide an overview of using profiles with docker-compose to address multiple services within a monolithic file. This aims to provide all the advantages of Camp B's approach without the faff and hassle of changing directories or managing multiple compose files.
Example A
Take the following compose file:
version: "3.3"
services:
nginx1:
image: nginx
container_name: nginx1
profiles:
- prod
- test
nginx2:
image: nginx
container_name: nginx2
profiles:
- prod
nginx3:
image: nginx
container_name: nginx3
profiles:
- test
Using the profiles it's possible to put a single service into more than profile at once giving much more flexibility than multiple files as Camp B would typically do.
alex@slartibartfast tmp % docker-compose --profile test up -d
[+] Running 2/2
⠿ Container nginx3 Started 0.5s
⠿ Container nginx1 Started
As you can see above nginx1
and nginx3
were both started because they are members of the profile test
. Profiles support all operations that you'd expect via compose and the full documentation can be found over in Docker's documentation.
Note that if you include a profile a simple docker-compose up -d
will fail:
alex@slartibartfast tmp % docker-compose up -d
no service selected
These profiles aren't perfect and certainly have nuances you will need to become familiar with but they are quite promisingly useful.
Conclusion
A relatively short post this but after a discussion on the Self-Hosted discord server earlier the topic of monolith vs multiple smaller files came up. It occurred to me there must be a way to filter containers and this is what I found.
Hope you found this useful. Let me know if you use this in the real world!