Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make profile default for docker-compose?

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?

like image 784
merhoo Avatar asked Jan 23 '26 13:01

merhoo


2 Answers

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

like image 81
code_monk Avatar answered Jan 27 '26 00:01

code_monk


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
like image 27
Andrew Dinmore Avatar answered Jan 27 '26 00:01

Andrew Dinmore