Docker in depth
Example: a production-grade Dockerfile, explained
Example: a hypothetical Go service. The Dockerfile has two stages. The build stage starts from an official Go image, copies the module files first and downloads dependencies, so that layer is cached until dependencies change, then copies the source and compiles a static binary. The final stage starts from a minimal base, copies only the binary, sets a numeric non-root user, exposes the port and sets the entry point. A .dockerignore file keeps the .git folder, local environment files and test data out of the build context. Built with BuildKit, the image is small, rebuilds quickly after code changes and carries no compiler or shell for an attacker to use.
Habits that make Docker builds fast and safe
- Order layers by change frequency: dependencies before source code, so caches survive most commits.
- Use cache mounts and remote caches: BuildKit can cache package manager directories and share build cache between CI runs.
- Never put secrets in build arguments or layers: use BuildKit secret mounts for private registries or tokens needed during the build.
- Pin base images: by version, and by digest for reproducible builds; rebuild regularly to pick up security fixes.
- Build for the right platforms: docker buildx can produce images for both amd64 and arm64, useful when clusters use ARM instances.
- Add metadata: OCI labels for source repository and commit make images traceable.
Docker Compose for local development
Compose describes an application and its dependencies, such as a database, cache and message broker, in one file, so a new developer can start the whole stack with one command. Good Compose setups mirror production versions of dependencies, use named volumes for data, load configuration from environment files that are not committed, and include health checks so services start in the right order. Compose is a development and testing tool for most teams; production usually runs on Kubernetes or a managed container service.
How to review a team's Docker usage
| Look at | Healthy sign |
| Image size | Production images contain no build tools or package caches |
| Build time in CI | Small code changes rebuild quickly thanks to caching |
| User | Containers run as non-root |
| Scan results | Few fixable critical findings, and base images refreshed regularly |
| Dockerfile consistency | Shared templates per language rather than many hand-written variants |
Common mistakes
- Running apt-get update in one layer and install in another, which caches stale package lists.
- Copying the entire repository before installing dependencies, which breaks caching.
- Using latest tags in production manifests.
- Mounting the Docker socket into containers, which effectively grants root on the host.
- Assuming Docker Desktop licensing is the same for every company size without checking the current terms.
Docker builds containers that pipelines in CI/CD push to registries and that orchestrators run. Alternatives such as Podman and Buildah produce the same OCI images. We cover image design, build pipelines and migration to containers in Docker and containerization.