Reference

Docker & Kubernetes

Read image builds, container commands, Compose files and Kubernetes workload configuration.

Scope and conventions

Docker CLI/Dockerfile, Docker Compose and Kubernetes native YAML/kubectl. These are complementary tools, not interchangeable editions. Commands assume your own disposable local environment; image names, cluster objects and paths are illustrative. Nothing runs from this guide.

Images & builds

Image, container, tag and digest

Docker & Kubernetes

An image is a packaged filesystem/configuration. A container is a running or stopped instance with a writable layer. Tags can move; a digest identifies content.

Fragment

Docker & Kubernetes · Image, container, tag and digest
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
docker image ls
docker container ls -a
docker image inspect IMAGE

Pinning a digest fixes content identity but does not establish trust or patch freshness.

Build from a context

Docker & Kubernetes

The final operand is the build context. COPY paths come from that context unless another source is named. -f selects the Dockerfile.

Fragment

Docker & Kubernetes · Build from a context
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
docker build -f docker/Dockerfile -t demo:local .

.dockerignore excludes context paths; it is separate from .gitignore.

Read common Dockerfile instructions

Docker & Kubernetes

FROM selects a base/stage. WORKDIR sets later instruction and runtime working directory. COPY adds context files. RUN executes during the build.

Fragment

Docker & Kubernetes · Read common Dockerfile instructions
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FROM alpine:3.22
WORKDIR /app
COPY ./message.txt ./message.txt
RUN test -s ./message.txt
CMD ["cat", "/app/message.txt"]

Base-image versions are illustrative; select and maintain the version your application supports.

Understand build cache boundaries

Docker & Kubernetes

Build steps can reuse cached results when their inputs match. Put infrequently changing dependency inputs before frequently changing source, while preserving a correct build.

Fragment

Docker & Kubernetes · Understand build cache boundaries
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# Shape of an application build:
COPY dependency-manifest lockfile ./
RUN install-dependencies
COPY ./src ./src
RUN build-application

This is schematic: replace filenames and commands with the native toolchain.

Copy artifacts from a build stage

Docker & Kubernetes

A named build stage can contain the compiler. COPY --from selects its output for a smaller final image.

Fragment

Docker & Kubernetes · Copy artifacts from a build stage
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FROM golang:1.27 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app

FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]

Assumes the named Go project exists and supports a static build. scratch supplies no shell, CA certificates or user database; add runtime assets the app needs.

Separate default command and entrypoint

Docker & Kubernetes

Exec-form ENTRYPOINT chooses an executable; CMD can provide default arguments. docker run arguments normally replace CMD; --entrypoint replaces ENTRYPOINT.

Fragment

Docker & Kubernetes · Separate default command and entrypoint
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ENTRYPOINT ["/app"]
CMD ["serve", "--port", "8080"]

JSON exec form does not expand $VARIABLE through a shell. Shell form has different signal and argument behavior.

Build arguments versus runtime environment

Docker & Kubernetes

ARG is a build-time variable. ENV persists in image configuration and supplies runtime defaults; docker run -e can override them.

Fragment

Docker & Kubernetes · Build arguments versus runtime environment
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ARG BUILD_MODE=release
ENV PORT=8080

Neither ARG nor ENV is a secret store. Use BuildKit secret mounts for build credentials.

Use a build secret mount

Docker & Kubernetes

BuildKit can mount a secret for one RUN instruction without copying it into an image layer by default.

Fragment

Docker & Kubernetes · Use a build secret mount
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=token \
    test -s /run/secrets/token

Build with --secret id=token,src=PATH in your own environment. A command can still leak a mounted value by logging or copying it.

Choose the runtime user

Docker & Kubernetes

USER sets the default user for subsequent RUN instructions and the launched process. File permissions must allow that identity to read/write required paths.

Fragment

Docker & Kubernetes · Choose the runtime user
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
USER 10001:10001

A nonroot container is one defense; capabilities, mounts and host access still matter.

EXPOSE documents a port

Docker & Kubernetes

EXPOSE records the intended container port. It does not publish that port on the host.

Fragment

Docker & Kubernetes · EXPOSE documents a port
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPOSE 8080

Port publication belongs to runtime configuration such as docker run -p.

Pull, tag and push an image

Docker & Kubernetes

An image reference can include registry, namespace, repository and tag. tag adds a local name; push uploads the named image to a registry; pull obtains it locally.

Fragment

Docker & Kubernetes · Pull, tag and push an image
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
docker pull registry.example.test/team/demo:1.0
docker tag demo:local registry.example.test/team/demo:1.0
docker push registry.example.test/team/demo:1.0

Authentication and repository permissions are separate requirements. latest is an ordinary default tag, not a guarantee of newest or stable content.