Image and Build Commands
Commands for the full image lifecycle: building from Dockerfiles, tagging releases, pushing to registries, and cleaning up local images.
Building Images
Basic Build
# Build from current directory
docker build -t my-app:1.0.0 .
# Build with specific Dockerfile
docker build -f Dockerfile.prod -t my-app:1.0.0 .
# Build without cache (clean rebuild)
docker build --no-cache -t my-app:1.0.0 .
Multi-Stage Build Target
# Build only a specific stage
docker build --target builder -t my-app:build-stage .
Build Arguments
# Pass build-time variables
docker build --build-arg NODE_VERSION=18 -t my-app:1.0.0 .
Multi-Platform Build
# Build for multiple architectures (requires buildx)
docker buildx build --platform linux/amd64,linux/arm64 -t my-app:1.0.0 .
.dockerignore
Exclude files from the build context to speed up builds and avoid leaking secrets:
node_modules
.git
.env
*.md
docker-compose*.yml
Tagging Images
# Tag an existing image for a registry
docker tag my-app:1.0.0 registry.example.com/my-app:1.0.0
# Multi-tag during build
docker build -t my-app:1.0.0 -t my-app:latest -t registry.example.com/my-app:1.0.0 .
# Tag with commit SHA for traceability
docker tag my-app:1.0.0 registry.example.com/my-app:sha-abc1234
Tagging Strategy
| Tag Type | Example | Purpose |
|---|---|---|
| Semantic version | app:1.2.3 | Release identification |
| Commit SHA | app:sha-abc1234 | Exact source traceability |
latest | app:latest | Convenience (mutable, not for production references) |
| Digest | app@sha256:8c5b... | Immutable, guaranteed same image |
Publishing Images
# Login to registry
docker login registry.example.com
# Push an image
docker push registry.example.com/my-app:1.0.0
# Push all tags for an image
docker push --all-tags registry.example.com/my-app
# Logout when done
docker logout registry.example.com
Inspecting Images
# List all local images
docker images
# List with sizes and digests
docker images --digests
# Filter dangling images (untagged)
docker images -f dangling=true
# Show image layer history (see what each layer does)
docker history my-app:1.0.0
# Full layer history without truncation
docker history --no-trunc my-app:1.0.0
# Full image metadata
docker image inspect my-app:1.0.0
# Get specific fields
docker inspect my-app:1.0.0 --format '{{.Config.Cmd}}'
docker inspect my-app:1.0.0 --format '{{.Config.ExposedPorts}}'
docker inspect my-app:1.0.0 --format '{{index .RepoDigests 0}}'
Pulling Images
# Pull latest tag
docker pull nginx
# Pull specific tag
docker pull nginx:1.25-alpine
# Pull by digest (immutable)
docker pull nginx@sha256:8c5b...
# Pull all tags for an image
docker pull --all-tags nginx
Saving and Loading Images
Transfer images without a registry:
# Save image to a tar file
docker save -o my-app.tar my-app:1.0.0
# Load image from a tar file
docker load -i my-app.tar
Useful for air-gapped environments or offline deployments.
Cleaning Up Images
# Remove a specific image
docker rmi my-app:1.0.0
# Remove dangling images (untagged <none>)
docker image prune
# Remove all images not used by running containers
docker image prune -a
# Remove images older than 24 hours
docker image prune -a --filter "until=24h"
Build Quick Reference
| Goal | Command |
|---|---|
| Build image | docker build -t NAME:TAG . |
| Build with specific Dockerfile | docker build -f FILE -t NAME:TAG . |
| Build without cache | docker build --no-cache -t NAME:TAG . |
| Build specific stage | docker build --target STAGE -t NAME:TAG . |
| Pass build argument | docker build --build-arg KEY=VAL -t NAME:TAG . |
| Tag image | docker tag SOURCE TARGET |
| Push to registry | docker push REGISTRY/NAME:TAG |
| List images | docker images |
| Image layer history | docker history IMAGE |
| Image metadata | docker image inspect IMAGE |
| Get image digest | docker inspect IMAGE --format '{{index .RepoDigests 0}}' |
| Remove image | docker rmi IMAGE |
| Remove dangling images | docker image prune |
| Remove all unused images | docker image prune -a |
| Save image to file | docker save -o FILE.tar IMAGE |
| Load image from file | docker load -i FILE.tar |
What's Next
- Continue to Container Ops Commands.