How to set the image name for a Docker Compose build
Codemia Knowledge Hub ·
Add an image key next to build in the service definition. With both present, Compose builds from the Dockerfile and tags the result with the name in image. Without it, Compose generates a name like {project_name}-{service_name}, which is unclear in image listings and CI pipelines.
The default name without image
docker compose build produces {project}-webapp, where {project} defaults to the name of the directory holding compose.yaml. In a directory called my-app, the image is my-app-webapp.
Problems with that default:
- It changes if you move the project to a different directory
- It is not suitable for pushing to a container registry
- It makes
docker imagesoutput harder to scan when you have many projects
Setting an explicit image name
Compose tags the built image mycompany/webapp:1.0.0, so you can push it without re-tagging:
Image name rules
| Rule | Example | Valid? |
| Lowercase only | myapp | Yes |
| With namespace | mycompany/myapp | Yes |
| With registry | ghcr.io/mycompany/myapp | Yes |
| With tag | myapp:v2.1 | Yes |
| Uppercase letters | MyApp | No |
| Spaces | my app | No |
Special characters (except -, _, ., /) | my@app | No |
With no tag, Docker uses :latest.
Naming several services
Each service gets its own name, which matters when you push components separately:
docker compose build builds all three with distinct, registry-ready names.
Setting the name and tag from environment variables
Hardcoded tags mean editing the file every release. Use variables instead:
Then choose the name at build time:
Defaults in a .env file
Compose reads .env automatically. Developers or CI jobs override values with environment variables or an alternate env file:
image without build
With only image, Compose pulls from a registry instead of building:
| Configuration | Behavior |
build only | Builds and auto-names the image |
image only | Pulls from registry |
build + image | Builds and tags with the specified name |
Changing the project name prefix
The default name includes the project name, so you can set that explicitly:
This gives myproject-webapp instead of using the directory name. On the command line:
Or with COMPOSE_PROJECT_NAME:
This only changes the generated prefix. For registry-ready names, use the image key.
Build and push in CI
docker compose push uses the name in image. Without that key there is no registry-compatible name to push.
Compose V1 vs V2 naming
| Aspect | V1 (docker-compose) | V2 (docker compose) |
| Default separator | _ (underscore) | - (hyphen) |
| Default name | {dir}_{service} | {dir}-{service} |
version key | Required | Optional (ignored) |
| Command syntax | docker-compose build | docker compose build |
When migrating from V1 to V2, scripts that depend on the generated name can break because of the separator change. An explicit image key avoids that.
The version key (like version: '3.8') is not required in Compose V2 and is officially deprecated, so you can remove it.
Naming mistakes
- Using only
buildor onlyimage.buildalone gives an auto-generated name that is useless for a registry;imagealone makes Compose pull instead of build. - Relying on the generated project name. Renaming or moving the directory changes the image name and can silently break deployment scripts.
- Uppercase letters. Docker rejects the tag; every component must be lowercase.
- Hardcoded version tags. Use environment variables with defaults.
- No tag at all.
:latestis mutable and does not identify a version. Tag releases with a version number or commit hash. - Parsing generated names across V1 and V2. The
_vs-separator differs.
Related reading
- How do I deploy updated Docker images to Amazon ECS tasks?
- How do I Docker COPY as non root?
- How do I edit a file after I shell to a Docker container?
- How do I force Kubernetes to re-pull an image?
- How do I disable log messages from the Requests library?
- How do I do logging in C without using 3rd party libraries?
- How do I get into a Docker container's shell?
- How do I make a comment in a Dockerfile?
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.