Skip to main content

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 TypeExamplePurpose
Semantic versionapp:1.2.3Release identification
Commit SHAapp:sha-abc1234Exact source traceability
latestapp:latestConvenience (mutable, not for production references)
Digestapp@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

GoalCommand
Build imagedocker build -t NAME:TAG .
Build with specific Dockerfiledocker build -f FILE -t NAME:TAG .
Build without cachedocker build --no-cache -t NAME:TAG .
Build specific stagedocker build --target STAGE -t NAME:TAG .
Pass build argumentdocker build --build-arg KEY=VAL -t NAME:TAG .
Tag imagedocker tag SOURCE TARGET
Push to registrydocker push REGISTRY/NAME:TAG
List imagesdocker images
Image layer historydocker history IMAGE
Image metadatadocker image inspect IMAGE
Get image digestdocker inspect IMAGE --format '{{index .RepoDigests 0}}'
Remove imagedocker rmi IMAGE
Remove dangling imagesdocker image prune
Remove all unused imagesdocker image prune -a
Save image to filedocker save -o FILE.tar IMAGE
Load image from filedocker load -i FILE.tar

What's Next