I have a docker compose file with 2 exclusive profiles local and dev, which we use for local testing and pulling the dev environment down for debugging.
Is there a way to default to local so that we can use docker-compose build without specifying --profile and docker will default to local?
You'll want to use the COMPOSE_PROFILES environment variable. To always have it set, you could put:
COMPOSE_PROFILES=local in /etc/environment
or...
export COMPOSE_PROFILES=local in ~/.bashrc or ~/.zshrc
Then restart your shell. You can type this to ensure your shell has picked up the env var:
echo $COMPOSE_PROFILES
I was just playing with this; it turns out that services using an empty string as a profile will be started when a profile is not specified but, unlike those without a profile, not when another profile is.
While it should be noted that this isn't documented, so the functionality may change in the future, I reckon it should be sufficient to implement a decent "default" profile functionality for situations the COMPOSE_PROFILES variable doesn't suffice.
E.g.
services:
a:
image: busybox
profiles:
- ''
b:
image: busybox
profiles:
- ''
- a
c:
image: busybox
profiles:
- a
aj@laptop:~/test$ docker compose up
[+] Running 2/0
✔ Container test-b-1 Created 0.0s
✔ Container test-a-1 Created 0.0s
Attaching to test-a-1, test-b-1
test-a-1 exited with code 0
test-b-1 exited with code 0
aj@laptop:~/test$ docker compose --profile a up
[+] Running 2/0
✔ Container test-c-1 Created 0.0s
✔ Container test-b-1 Created 0.0s
Attaching to test-b-1, test-c-1
test-b-1 exited with code 0
test-c-1 exited with code 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With