Build, Tag & the Build Context
The docker build command
Section titled “The docker build command”docker build reads a Dockerfile and produces an image. The basic syntax is:
docker build [OPTIONS] PATHThe PATH argument (usually .) is called the build context — it is the directory that Docker sends to the build daemon. Files in the build context are available to COPY instructions inside the Dockerfile. Files outside the build context are not accessible.
docker build .This sends the current directory as the build context. Without -t, the resulting image has no name — only a hex digest. You will almost always add -t.
Tagging with -t
Section titled “Tagging with -t”The -t flag assigns a human-readable name:tag to the image:
docker build -t myapp:1.0 .The full form of a tag is:
[registry/][owner/]name:tagExamples:
myapp:1.0myapp:latestghcr.io/acme/myapp:1.0docker.io/library/node:22-alpineIf you omit the tag part, Docker defaults to :latest.
Listing images with docker images
Section titled “Listing images with docker images”docker imagesExpected output:
REPOSITORY TAG IMAGE ID CREATED SIZEmyapp 1.0 a3f2b1c9d4e7 2 minutes ago 145MBmyapp latest a3f2b1c9d4e7 2 minutes ago 145MBnode 22-alpine 1b2c3d4e5f60 3 weeks ago 52MBRetagging with docker tag
Section titled “Retagging with docker tag”docker tag adds a new name or tag to an existing image without copying it. Both names point to the same image ID.
# Promote a build image to a release namedocker tag myapp:1.0 myapp:stabledocker tag myapp:1.0 ghcr.io/acme/myapp:1.0The meaning of latest
Section titled “The meaning of latest”latest is not a magic “newest” tag. It is just a default string that Docker uses when you omit the tag. It has no semantic version meaning. If you push myapp:1.0 and myapp:latest independently, they can point to completely different image layers.
In production pipelines, always use an explicit, meaningful tag (version number, git SHA, date) so you know exactly what is deployed.
Speeding up the context with .dockerignore
Section titled “Speeding up the context with .dockerignore”By default, docker build . sends your entire current directory to the daemon — including node_modules/, .git/, test fixtures, and local secrets. A .dockerignore file (same syntax as .gitignore) excludes paths from the build context, which:
- Speeds up the build (less data to transfer)
- Prevents accidentally
COPY-ing secrets into your image
node_modules/.git/.env*.logdist/coverage/Putting it together
Section titled “Putting it together”# Build with a version tagdocker build -t myapp:1.0 .
# Also tag as latestdocker tag myapp:1.0 myapp:latest
# Inspect the resultdocker images myappExpected output:
REPOSITORY TAG IMAGE ID CREATED SIZEmyapp 1.0 a3f2b1c9d4e7 1 minute ago 145MBmyapp latest a3f2b1c9d4e7 1 minute ago 145MBHands-on practice
Section titled “Hands-on practice”# syntax=docker/dockerfile:1
FROM alpine:3.20
LABEL maintainer="[email protected]"
WORKDIR /app
RUN echo "v1.0" > version.txt
CMD ["cat", "version.txt"]
# --- build, tag, and inspect ---
# docker build -t myapp:1.0 .
# docker tag myapp:1.0 myapp:latest
# docker images myapp
# docker run --rm myapp:1.0