Is there a way to set the stack name when using docker-compose?
Currently it takes the folder name (which is a horrible area) and that leads to some confusion.
For example, we have several projects that have a database folder that contains the database stack. When running these on a single host, we have now several database stacks.
Docker Compose is an official tool that helps you manage your Docker containers by letting you define everything through a docker-compose. yml file. docker stack is a command that's embedded into the Docker CLI. It lets you manage a cluster of Docker containers through Docker Swarm.
Docker Stack is run across a Docker Swarm, which is essentially a group of machines running the Docker daemon, which are grouped together, essentially pooling resources. Stacks allow for multiple services, which are containers distributed across a swarm, to be deployed and grouped logically.
There are several ways to do it:
1. Using --project-name
(or -p
) option when calling docker-compose
:
docker-compose -p "my-app" up
Caution:
-p "my-app"
must come beforeup
.
2. COMPOSE_PROJECT_NAME environment variable:
export COMPOSE_PROJECT_NAME=my-app docker-compose up
3. COMPOSE_PROJECT_NAME in .env file
Create a file named .env in the project root and set the COMPOSE_PROJECT_NAME environment variable there:
COMPOSE_PROJECT_NAME=some_app
and then:
docker-compose up
The .env file is read from the folder where the docker-compose command is executed, NOT from the folder of the docker-compose.yml file.
Assuming the following project structure:
root - database - docker-compose.yml - .env - app - docker-compose.yml - .env - ...
The command below, executed in the root folder, will not give desired effects:
# Stack name 'database' (the folder name). The root/database/.env file not read. docker-compose -f ./database/docker-compose.yml up
The docker-compose command needs to be executed in the root/database folder:
# Stack name from the root/database/.env cd database docker-compose up
If you use option 2 or 3, the project name is applied to all docker-compose commands, as if it were specified with the
-p
option.
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