Can a Dockerfile set the output image name?
Name image outputs with build tags, Compose configuration or a build tool, and distinguish names from stages and labels.
A Dockerfile has no instruction that assigns the output image its repository name and tag. Choose those in the build command or build configuration. FROM selects an input image or stage; LABEL adds metadata.
docker build -t registry.example.com/team/myapp:1.2 .
docker build -t myapp:1.2 -t myapp:latest .
docker tag myapp:1.2 registry.example.com/team/myapp:1.2
The first command sets a fully qualified repository reference. The second gives the same build output two tags. The third adds a reference to an existing local image; it does not rename away the old reference or copy all the layer data.
Similar-looking names have different jobs
| Dockerfile feature | Meaning |
|---|---|
FROM nginx:stable |
Base image for the stage |
FROM ... AS builder |
Stage name used within the build |
LABEL org.opencontainers.image.title="myapp" |
Descriptive image metadata |
ARG IMAGE_NAME=... |
Build argument, useful only where consumed |
An argument can parameterize a FROM reference or other supported instructions. It does not change the output tag passed to the build command. A label called “title” is still not a repository tag.
Compose and CI
Compose can define the output name alongside a build definition:
services:
app:
build: .
image: myapp:1.2
With no explicit image, Compose normally tags a locally built service image using the project and service names. The project name itself depends on the chosen Compose configuration and invocation. Use an explicit image reference when other systems need a stable distribution name.
In a CI shell, construct the output reference outside the Dockerfile:
: "${CI_COMMIT_SHORT_SHA:?Supply a commit identifier}"
docker build -t "registry.example.com/team/myapp:${CI_COMMIT_SHORT_SHA}" .
A commit-derived tag helps traceability, but the tag can still be moved unless repository policy prevents it. Record the resulting digest for deployment identity. latest is simply a tag; it does not mean newest by timestamp and should not be your only release identifier.
FAQ
What happens without -t? With a builder that loads the result into the local image store, the output may have an image ID without a useful repository tag. You can add one with docker tag. Other Buildx drivers may retain the result only in build cache unless you request an output such as --load or --push; do not assume every successful build appears in docker image ls.
Can I use a shell variable inside -t? Yes, when your shell expands it in the build command. That is separate from a Dockerfile ARG.
Why does an example appear to name an image in FROM? It is naming the base input or a build stage. Inspect the actual build command, Compose file, or Bake configuration to find the output naming decision.
See Image tags and repository names, Compose build reference, Build driver output behavior.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan