52

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.

1
  • The project name can be set using the name: my-project top level element in the docker-compose file. See answer below Oct 17, 2022 at 16:14

2 Answers 2

109
+50

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.

3
10

It looks like the project name can be set using the name top level element in the docker-compose file as described here: https://docs.docker.com/compose/compose-file/#name-top-level-element

Top-level name property is defined by the specification as project name to be used if user doesn’t set one explicitly. Compose implementations MUST offer a way for user to override this name, and SHOULD define a mechanism to compute a default project name, to be used if the top-level name element is not set.

This doesn't seem to be well documented anywhere else, and I didn't see a reference to it in the spec changelog, so I'm not sure if this has always been part of the spec, or if compose just didn't support it until recently.


Example

My current directory (pwd):

/Users/brahmlower/development/compose-test

My compose file (cat docker-compose.yml):

version: "3.8"

name: my-project

services:
  hello-world:
    image: "hello-world:latest"

Without the name property, we would expect to see the stack started with the prefix compose-test since that's the name of the directory. However when I bring the stack up, we see compose names the stack my-project as expected.

The stack output (docker compose up -d):

[+] Running 2/2
 ⠿ Network my-project_default          Created
 ⠿ Container my-project-hello-world-1  Started

Versions

In case it's helpful, my docker versions are:

compose (docker compose version):

Docker Compose version v2.10.2

docker (docker version):

Client:
 Cloud integration: v1.0.29
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:04:45 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.12.0 (85629)
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:01 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
1
  • Indeed, IntelliJ was giving a highlight error on it, but it does work Mar 5 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.