Project Names
Every docker compose command works on a project. The project name is the prefix on everything Compose creates (shop-api-1, shop_default, shop_pgdata) and the label (com.docker.compose.project=shop) Compose uses to find "its" containers again on the next up, ps or down. Getting the name right is what lets you run several copies of an app side by side, and getting it wrong is how you end up with two databases or a down that removes the wrong thing.
Where the name comes from
Compose takes the first of these that is set:
| Priority | Source | Example |
|---|---|---|
| 1 | -p / --project-name flag |
docker compose -p staging up -d |
| 2 | COMPOSE_PROJECT_NAME (shell or .env) |
COMPOSE_PROJECT_NAME=shop-dev |
| 3 | top-level name: in the compose file |
name: shop |
| 4 | the project directory's name | ~/code/demo/ → demo |
All examples on this site run in a folder called demo, which is why you've seen demo_default everywhere. Setting name: replaces it, and -p beats both:
# $ docker compose config
# $ docker compose -p staging config
name: shop
services:
api:
image: ghcr.io/acme/api:3.1
labels:
com.acme.stack: ${COMPOSE_PROJECT_NAME}
volumes:
- data:/data
volumes:
data:$ docker compose config
name: shop
services:
api:
image: ghcr.io/acme/api:3.1
labels:
com.acme.stack: shop
networks:
default: null
volumes:
- type: volume
source: data
target: /data
volume: {}
networks:
default:
name: shop_default
volumes:
data:
name: shop_data
$ docker compose -p staging config
name: staging
services:
api:
image: ghcr.io/acme/api:3.1
labels:
com.acme.stack: staging
networks:
default: null
volumes:
- type: volume
source: data
target: /data
volume: {}
networks:
default:
name: staging_default
volumes:
data:
name: staging_data
Notice two things. The network and volume names follow the project (shop_data, then staging_data), so the staging copy gets its own data. And Compose provides the variable COMPOSE_PROJECT_NAME for interpolation inside the file, set to the final name, even when you never defined it.
COMPOSE_PROJECT_NAME in .env beats name::
name: shop
services:
api:
image: ghcr.io/acme/api:3.1
# --- .env
COMPOSE_PROJECT_NAME=shop-devname: shop-dev
services:
api:
image: ghcr.io/acme/api:3.1
networks:
default: null
networks:
default:
name: shop-dev_default
Valid names
Project names may contain only lowercase letters, digits, - and _, and must start with a letter or digit. Names given with -p are checked strictly:
# $ docker compose -p Shop.V2 config
services:
api:
image: ghcr.io/acme/api:3.1invalid project name "Shop.V2": must consist only of lowercase alphanumeric characters, hyphens, and underscores as well as start with a letter or number
Names that come from name: or from the directory are normalised instead: lower-cased, with invalid characters dropped. A folder called My Shop becomes myshop:
name: My Shop
services:
api:
image: ghcr.io/acme/api:3.1name: myshop
services:
api:
image: ghcr.io/acme/api:3.1
networks:
default: null
networks:
default:
name: myshop_default
The same normalisation as a small function, handy to predict a name from a folder:
import re
def normalise(name):
name = name.lower()
name = re.sub(r"[^a-z0-9_-]", "", name) # drop anything that isn't allowed
name = name.lstrip("-_") # must start with a letter or digit
return name
def resolve(flag=None, env=None, file_name=None, directory="demo"):
if flag is not None:
if not re.fullmatch(r"[a-z0-9][a-z0-9_-]*", flag):
return f'invalid project name "{flag}"'
return flag
for candidate in (env, file_name, directory):
if candidate:
return normalise(candidate)
print(resolve(directory="My Shop"))
print(resolve(directory="api.v2"))
print(resolve(file_name="shop", directory="demo"))
print(resolve(env="shop-dev", file_name="shop"))
print(resolve(flag="staging", env="shop-dev", file_name="shop"))
print(resolve(flag="Shop.V2"))myshop
apiv2
shop
shop-dev
staging
invalid project name "Shop.V2"
Variables in name:
name: is interpolated like the rest of the file, and the result is then available as ${COMPOSE_PROJECT_NAME}:
name: shop-${DEPLOY_ENV:-dev}
services:
api:
image: ghcr.io/acme/api:3.1
container_name: ${COMPOSE_PROJECT_NAME}-apiname: shop-dev
services:
api:
container_name: shop-dev-api
image: ghcr.io/acme/api:3.1
networks:
default: null
networks:
default:
name: shop-dev_default
DEPLOY_ENV=prod docker compose up -d would create shop-prod with a container shop-prod-api. (A fixed container_name stops the service from scaling, see Service Discovery & DNS; here it only shows the variable.)
Several copies on one host
Because everything is prefixed, one compose file can run many times in parallel. Each copy needs a unique project name and, if it publishes ports, unique host ports:
$ PR=1234
$ API_PORT=18234 docker compose -p "pr-$PR" up -d --wait
$ docker compose ls
NAME STATUS CONFIG FILES
pr-1234 running(3) /srv/review/compose.yaml
shop running(4) /srv/shop/compose.yaml
$ docker compose -p "pr-$PR" down -v # remove this copy and its data, nothing elseThis is how CI systems run tests for many branches on one machine without collisions; see Compose in CI. docker compose ls lists running projects with their config files.
Pitfalls
- Renaming the folder changes the default project name. The next
upcreates a new network, new empty volumes and new containers, while the old ones keep running under the old name. If the data matters, pinname:in the file early. - Two checkouts with the same folder name (
~/work/appand~/tmp/app) share the default nameappand fight over containers. Use-porname:. downwith the wrong name does nothing ("no resource found"), which is safe but confusing.docker compose lsshows which names exist.- Compose finds containers by labels, not by name prefix. Containers you started by hand with a similar name are never touched.
$ docker ps --filter label=com.docker.compose.project=shop --format '{{.Names}}'
shop-api-1
shop-db-1Learn more: The Compose CLI · Compose in CI · Stacks (the Swarm equivalent: the stack name)
Test yourself
The file says name: shop, .env says COMPOSE_PROJECT_NAME=shop-dev, and you run docker compose -p qa up. What is the project name?
- shop
- shop-dev
- qa
- demo (the folder)
Why does the staging copy (-p staging) not share the database with the default copy?
- Compose copies the data
- Volume names are prefixed with the project name, so staging gets staging_data
- Volumes are never shared
- Because of different networks only
What does a folder named My Shop become as a default project name?
- My Shop
- my-shop
- myshop
- an error