Docker Build Command Examples

docker build reads a Dockerfile and creates a container image. Understanding build flags lets you tag versions, inject build-time arguments, bypass the layer cache for fresh builds, and target specific platforms. These patterns are essential for CI/CD pipelines and production deployments.

DockerBuildDevOpsCLI
Basic Builds
Build from current directory
docker build .
The dot . sets the build context to the current directory, which is where Docker looks for the Dockerfile. The resulting image has no tag and is only accessible by its generated image ID.
Build and tag with a name
docker build -t myapp:latest .
The -t flag tags the image with a name and optional tag (default: latest). Always tag your images for easy reference. Use the format name:tag or registry/name:tag for registry pushes.
Build with a version tag
docker build -t myapp:1.2.0 -t myapp:latest .
Apply multiple -t flags to tag the same image with multiple names in one build. A common pattern: tag with the version number and also update the latest tag simultaneously.
Use a different Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
The -f flag specifies a non-default Dockerfile path. Useful when you maintain separate Dockerfiles for development, staging, and production environments in the same repository.
Build Options
Build with no layer cache
docker build --no-cache -t myapp:latest .
The --no-cache flag ignores all cached layers and rebuilds from scratch. Use this when cached layers contain stale data, such as after changing package indexes or when debugging a suspected caching issue in CI.
Pass a build argument
docker build --build-arg NODE_ENV=production -t myapp .
Build arguments are passed to the Dockerfile as ARG NODE_ENV and are available during the build process only (not in the running container). Use them for environment-specific build settings, API endpoints, or version numbers.
Build for a specific platform
docker build --platform linux/amd64 -t myapp .
The --platform flag builds for a specific CPU architecture. Essential when building on Apple Silicon (arm64) for deployment to x86-64 servers. Use linux/arm64 to build for ARM servers like AWS Graviton.
Post-Build
List built images
docker images
Lists all locally available Docker images with their repository, tag, image ID, creation date, and size. Use docker images myapp to filter by a specific name. Images with no tag show as <none> (dangling images).
Inspect image metadata
docker inspect myapp:latest
Returns detailed JSON metadata about an image including layers, environment variables, exposed ports, entrypoint, and build history. Useful for verifying build arguments were set correctly or understanding an unfamiliar image.
Remove dangling (untagged) images
docker image prune
Removes dangling images — images with no tag that are no longer referenced by any container. These accumulate with repeated builds. Use docker image prune -a to also remove unused images (not referenced by any container).

How to Use

  1. Write a Dockerfile in your project directory with FROM, RUN, COPY, and CMD/ENTRYPOINT.
  2. Run docker build -t myapp:latest . from the same directory.
  3. Use docker images to verify the image was created with the correct tag.
  4. Test the image with docker run --rm myapp:latest before pushing to a registry.

Frequently Asked Questions

What is the build context and why does it matter?

The build context is the directory passed to docker build (usually .). Docker sends all files in this directory to the Docker daemon before building. A large context slows down builds significantly. Use a .dockerignore file to exclude node_modules, .git, and other unnecessary files.

What is the difference between ARG and ENV in a Dockerfile?

ARG defines build-time variables available only during the docker build process. ENV sets environment variables that persist in the running container. Use ARG for build configuration (like version numbers) and ENV for runtime configuration (like NODE_ENV).

How do I speed up Docker builds?

Order Dockerfile instructions from least to most frequently changed to maximize layer caching. Copy dependency files (package.json, requirements.txt) and install packages before copying source code. Use multi-stage builds to keep final images small. Use a .dockerignore to reduce context size.

Related Tools