docker-compose container name use dash - instead of underscore _
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker Compose uses dashes (-) rather than underscores (_) by default when generating container names because dashes are valid in DNS hostnames while underscores are not. Since Docker uses container names for internal DNS resolution on custom networks, dash-based names work seamlessly for inter-container communication. Understanding this convention helps avoid networking issues in multi-container applications.
How Docker Compose Names Containers
By default, Docker Compose generates container names using the pattern {project}_{service}_{index} (Compose V1) or {project}-{service}-{index} (Compose V2):
Setting a Custom Container Name
Use the container_name property to override the generated name:
With container_name set, Docker uses exactly that name regardless of the project name.
Why Dashes Over Underscores
DNS Compatibility
Containers on the same Docker network can reach each other by name. Docker's internal DNS follows RFC 952 and RFC 1123, which allow only letters, digits, and hyphens in hostnames:
Docker Service Discovery
In Docker Swarm and Compose, service names are used as DNS entries. Underscores in these names can cause resolution failures with certain DNS resolvers:
The Project Name
The project name (prefix for container names) defaults to the directory name. You can set it explicitly:
| Method | Example |
| Default | Directory name |
name in compose file | name: my-project |
-p flag | docker compose -p my-project up |
COMPOSE_PROJECT_NAME | Environment variable |
.env file | COMPOSE_PROJECT_NAME=my-project |
Scaling and Container Names
container_name prevents scaling because each container needs a unique name:
Compose V1 vs V2 Naming
If you are migrating from V1 to V2 and have scripts that reference container names with underscores, update them to use dashes or set explicit container_name values.
Common Pitfalls
- Underscore DNS failures: Container names with underscores may not resolve via Docker's internal DNS on custom networks. Always use dashes for names that will be used in inter-container communication.
- Scaling conflict: Setting
container_namepreventsdocker compose up --scalefrom working. Only usecontainer_namewhen you need exactly one instance of a service. - V1/V2 migration: Switching from
docker-compose(V1) todocker compose(V2) changes the separator from underscore to dash. Scripts or configs referencing old names will break. - Project name from directory: If your project directory has spaces or special characters, the auto-generated project name may cause issues. Set it explicitly via
name:in the compose file. - Unique names required:
container_namemust be unique across all running containers on the host, not just within the compose project. Conflicts with containers from other projects will prevent startup.
Summary
- Docker Compose V2 uses dashes as separators in generated container names (
project-service-index) - Dashes are preferred because they are valid DNS hostname characters; underscores may cause resolution failures
- Use
container_nameto set an explicit name, but this prevents scaling - The project name defaults to the directory name — set it explicitly with
name:or-pflag - When migrating from Compose V1 to V2, update any references from underscore-based to dash-based container names
Related reading
- Docker-Compose Entrypoint/Command
- docker-compose for Detached mode
- docker-compose how to use minio in- and outside of the docker network
- docker-compose INTERNAL ERROR cannot create temporary directory
- docker-compose kafka wait for zookeeper and schema-registry wait for kafka
- Docker-compose node_modules not present in a volume after npm install succeeds
- docker-compose not showing any changes to code
- Docker-Compose persistent data MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.