Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stack name with docker-compose

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.

like image 346
Thomas Avatar asked Nov 20 '18 21:11

Thomas


People also ask

What is docker stack vs compose?

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.

What is a stack in docker?

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.


1 Answers

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 before up.

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.

like image 71
Mafor Avatar answered Sep 20 '22 09:09

Mafor