docker
docker-compose
image naming
containerization
DevOps

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

yaml
services:
  webapp:
    build: .

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 images output harder to scan when you have many projects

Setting an explicit image name

yaml
1services:
2  webapp:
3    build:
4      context: .
5      dockerfile: Dockerfile
6    image: mycompany/webapp:1.0.0

Compose tags the built image mycompany/webapp:1.0.0, so you can push it without re-tagging:

bash
docker compose build webapp
docker push mycompany/webapp:1.0.0

Image name rules

RuleExampleValid?
Lowercase onlymyappYes
With namespacemycompany/myappYes
With registryghcr.io/mycompany/myappYes
With tagmyapp:v2.1Yes
Uppercase lettersMyAppNo
Spacesmy appNo
Special characters (except -, _, ., /)my@appNo

With no tag, Docker uses :latest.

Naming several services

Each service gets its own name, which matters when you push components separately:

yaml
1services:
2  api:
3    build:
4      context: ./api
5      dockerfile: Dockerfile
6    image: mycompany/api:1.0.0
7
8  worker:
9    build:
10      context: ./worker
11      dockerfile: Dockerfile
12    image: mycompany/worker:1.0.0
13
14  frontend:
15    build:
16      context: ./frontend
17      dockerfile: Dockerfile.prod
18    image: mycompany/frontend:1.0.0

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:

yaml
1services:
2  webapp:
3    build:
4      context: .
5    image: "${REGISTRY:-ghcr.io}/${IMAGE_NAME:-mycompany/webapp}:${TAG:-latest}"

Then choose the name at build time:

bash
1# Uses defaults: ghcr.io/mycompany/webapp:latest
2docker compose build
3
4# Override for a specific release
5REGISTRY=ecr.aws TAG=v2.3.1 docker compose build

Defaults in a .env file

bash
1# .env
2REGISTRY=ghcr.io
3IMAGE_NAME=mycompany/webapp
4TAG=latest

Compose reads .env automatically. Developers or CI jobs override values with environment variables or an alternate env file:

bash
docker compose --env-file .env.production build

image without build

With only image, Compose pulls from a registry instead of building:

yaml
1services:
2  database:
3    image: postgres:16-alpine
4    ports:
5      - "5432:5432"
ConfigurationBehavior
build onlyBuilds and auto-names the image
image onlyPulls from registry
build + imageBuilds and tags with the specified name

Changing the project name prefix

The default name includes the project name, so you can set that explicitly:

yaml
1# compose.yaml
2name: myproject
3
4services:
5  webapp:
6    build: .

This gives myproject-webapp instead of using the directory name. On the command line:

bash
docker compose -p myproject build

Or with COMPOSE_PROJECT_NAME:

bash
export COMPOSE_PROJECT_NAME=myproject
docker compose build

This only changes the generated prefix. For registry-ready names, use the image key.

Build and push in CI

yaml
1# compose.yaml
2services:
3  api:
4    build:
5      context: .
6      dockerfile: Dockerfile
7      args:
8        BUILD_DATE: "${BUILD_DATE}"
9        GIT_SHA: "${GIT_SHA}"
10    image: "ghcr.io/mycompany/api:${TAG:-latest}"
bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4export TAG="${GITHUB_SHA:0:8}"
5export BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
6export GIT_SHA="${GITHUB_SHA}"
7
8docker compose build api
9docker compose push api

docker compose push uses the name in image. Without that key there is no registry-compatible name to push.

Compose V1 vs V2 naming

AspectV1 (docker-compose)V2 (docker compose)
Default separator_ (underscore)- (hyphen)
Default name{dir}_{service}{dir}-{service}
version keyRequiredOptional (ignored)
Command syntaxdocker-compose builddocker 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 build or only image. build alone gives an auto-generated name that is useless for a registry; image alone 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. :latest is 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design