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.
DockerBuildDevOpsCLIdocker build .
. 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.docker build -t myapp:latest .
-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.docker build -t myapp:1.2.0 -t myapp:latest .
-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.docker build -f Dockerfile.prod -t myapp:prod .
-f flag specifies a non-default Dockerfile path. Useful when you maintain separate Dockerfiles for development, staging, and production environments in the same repository.docker build --no-cache -t myapp:latest .
--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.docker build --build-arg NODE_ENV=production -t myapp .
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.docker build --platform linux/amd64 -t myapp .
--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.docker images
docker images myapp to filter by a specific name. Images with no tag show as <none> (dangling images).docker inspect myapp:latest
docker image prune
docker image prune -a to also remove unused images (not referenced by any container).docker build -t myapp:latest . from the same directory.docker images to verify the image was created with the correct tag.docker run --rm myapp:latest before pushing to a registry.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.
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).
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.