← All free guides
FREE GUIDE · 102 TIPS · 13 CATEGORIES

100+ Docker
Tips & Tricks

Practical tips for building, running, troubleshooting, and securing containers. From the basics to Compose Watch, Docker Scout, and the latest AI tools in Docker Desktop.

102
Tips
13
Categories
9
Pages
Free
No sign-up

Inside the guide: all 102 tips

1Fundamentals & CLI Efficiency11 tips

1.1 Use docker ps with smart formatting

Example: docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" — show only the information you actually need instead of the full default table.

1.2 Clean up clutter in one sweep

Example: docker system prune -a --volumes — removes stopped containers, unused networks, dangling images, and volumes that aren't in use. Use with caution in production.

1.3 Jump into a running container with an interactive shell

Example: docker exec -it container_name /bin/bash — drop into a running container to troubleshoot without stopping it.

  • 1.4 Watch container logs efficiently
  • 1.5 Copy files to and from containers
  • 1.6 Use aliases for common commands
  • 1.7 Inspect anything with docker inspect
  • 1.8 Run one-off commands without keeping the container around
  • 1.9 Keep an eye on resource usage live
  • 1.10 Search local images quickly
  • 1.11 Rename an image without rebuilding it
2Dockerfile & Image Building13 tips

2.1 Build in multiple stages for slimmer images

Multi-stage builds separate the build environment from the production environment. Example: build with a full SDK image in the first stage, then copy only the compiled artifact into a minimal runtime image in the final stage.

2.2 Choose slim base images

Example: use python:3.14-slim instead of python:3.14 — fewer packages means fewer known vulnerabilities (CVEs) and a smaller image size.

2.3 Use layer caching smartly

Put commands that rarely change (e.g. apt install) early in the Dockerfile and commands that change often (e.g. COPY . .) last — that maximizes how much Docker can reuse from the cache.

  • 2.4 Combine RUN commands for fewer layers
  • 2.5 Use .dockerignore consistently
  • 2.6 Name your build stages clearly
  • 2.7 Pin exact versions instead of latest
  • 2.8 Use BuildKit cache for faster CI builds
  • 2.9 Build multi-platform images in a single command
  • 2.10 Offload heavy builds to the cloud
  • 2.11 Use BuildKit secrets instead of ARG for sensitive data
  • 2.12 Lint your Dockerfile before building
  • 2.13 Test that an image actually starts before you push it
3Docker Compose9 tips

3.1 Develop with automatic sync and restart

Example: docker compose up --watch — Compose Watch syncs source changes straight into running containers and rebuilds when needed, eliminating the manual edit-build-run cycle.

3.2 Separate environments with multiple compose files

Example: docker compose -f compose.yaml -f compose.prod.yaml up — let a base file hold what's shared and override files handle environment-specific differences.

3.3 Use profiles for optional services

Example: mark non-critical services (e.g. an email-testing tool, a search engine) with profiles: ["debug"] in the compose file so they only start with docker compose --profile debug up.

  • 3.4 Handle secrets safely in Compose
  • 3.5 Define dependency order correctly
  • 3.6 Scale services horizontally, locally
  • 3.7 Validate the compose file before running it
  • 3.8 Declare AI models as dependencies
  • 3.9 Reuse .env files across projects
4Networking8 tips

4.1 Create isolated networks per project

Example: docker network create my-project-net — keep containers for different projects separated so they can't accidentally reach each other.

4.2 Use service names instead of IP addresses

Within the same Docker network, containers can reach each other by service name (e.g. db or redis) thanks to built-in DNS — never hardcode IP addresses between containers.

4.3 Publish ports deliberately

Example: docker run -p 127.0.0.1:8080:80 nginx — bind to localhost instead of all interfaces (0.0.0.0) if the service shouldn't be reachable from outside.

  • 4.4 Troubleshoot networking issues between containers
  • 4.5 Use host networking mode sparingly
  • 4.6 Create your own DNS aliases for containers
  • 4.7 Restrict outbound traffic with network policies
  • 4.8 Clean up unused networks
5Volumes & Storage7 tips

5.1 Prefer named volumes over bind mounts in production

Example: docker volume create my-data and mount with -v my-data:/var/lib/data — Docker manages the lifecycle better than direct paths into the host's filesystem.

5.2 Use bind mounts for active development

Example: -v $(pwd)/src:/app/src — mirror local source code straight into the container so changes show up immediately without a rebuild.

5.3 Back up a volume the easy way

Example: docker run --rm -v my-data:/data -v $(pwd):/backup ubuntu tar czf /backup/backup.tar.gz /data — archive a volume's contents to a local file without extra tooling.

  • 5.4 Clear out unused volumes regularly
  • 5.5 Mount volumes read-only where you can
  • 5.6 Check where a volume is actually stored
  • 5.7 Migrate data between volumes safely
6Security10 tips

6.1 Scan images before you push them

Example: docker scout cves myimage:1.0 — catch known vulnerabilities early in the development workflow instead of discovering them in production.

6.2 Use docker scout sbom instead of the old sbom command

docker sbom is deprecated in favor of docker scout sbom — update any scripts or pipelines that still use the old command.

6.3 Run containers as non-root by default

Example: add USER node (or create your own restricted account) in the Dockerfile — limits the damage if a container gets compromised.

  • 6.4 Sign and verify images
  • 6.5 Restrict a container's capabilities
  • 6.6 Set resource limits to prevent a "noisy neighbor"
  • 6.7 Keep a production checklist
  • 6.8 Use secrets management, never environment variables, for sensitive data
  • 6.9 Keep Docker Engine and Desktop updated
  • 6.10 Make the filesystem read-only where you can
7Performance & Optimization7 tips

7.1 Measure image size before and after changes

Example: docker images myimage --format "{{.Size}}" — compare the size before and after a Dockerfile change to confirm an optimization actually helped.

7.2 Use healthchecks that reflect real readiness

Example: HEALTHCHECK CMD curl -f http://localhost:3000/health || exit 1 — a container that's "running" isn't the same as a container that can actually handle traffic.

7.3 Avoid unnecessary layers in the Dockerfile

Every RUN, COPY, and ADD creates a new layer. Combine related commands where it makes sense to keep the image both smaller and faster to pull.

  • 7.4 Analyze what's taking up space in an image
  • 7.5 Use tmpfs for fast, temporary storage
  • 7.6 Prime the CI cache with registry-based caching
  • 7.7 Limit how many log lines Docker keeps in memory
8Troubleshooting & Diagnostics7 tips

8.1 Understand why a container crashed

Example: docker inspect container_name --format '{{.State.ExitCode}}' together with docker logs container_name usually gives you the full picture of what went wrong.

8.2 Debug a container that has already exited

Example: docker run -it --entrypoint sh myimage — start a new instance with an interactive shell instead of the default command, to explore the filesystem manually.

8.3 Use Gordon AI for fast troubleshooting

Docker Desktop's built-in AI agent Gordon can analyze why a container is exiting unexpectedly or why a build is underperforming — worth asking before you dig in manually.

  • 8.4 Monitor events in real time
  • 8.5 Check daemon logs for system-level problems
  • 8.6 Test network connectivity from inside a container
  • 8.7 See what changed inside a container since it started
9Docker Desktop & Local Development7 tips

9.1 Sandbox AI coding agents with docker sbx

docker sbx (Docker Sandboxes) runs an AI coding agent — Claude Code, Codex, Gemini CLI, and similar tools — inside an isolated container instead of directly on your machine, so the agent can read, edit, and run code without full access to your host filesystem or network. Worth reaching for whenever you're letting an agent operate somewhat autonomously and want a hard boundary around what it can touch.

9.2 Take advantage of synchronized file shares for WSL2

Synchronized file shares give significantly faster filesystem performance for Windows developers running Linux containers via WSL2, compared to a traditional bind mount across the Windows/Linux boundary.

9.3 Let VirtioFS handle file sharing on Mac

The older osxfs sharing system has been fully phased out in favor of VirtioFS, which delivers better performance and avoids unnecessary file-ownership overhead.

  • 9.4 Use Resource Saver to save battery
  • 9.5 Test Kubernetes locally, right inside Desktop
  • 9.6 Keep an eye on the log viewer for a quick overview
  • 9.7 Keep Desktop extensions switched off unless you're actively using them
10CI/CD & Registry6 tips

10.1 Use multi-stage caching in CI pipelines

Example: build the builder stage separately and cache it between runs — only changed layers need rebuilding, which can cut pipeline times in half.

10.2 Tag images with the commit hash, not just "latest"

Example: docker build -t myregistry/app:$(git rev-parse --short HEAD) . — makes it possible to trace exactly which code is running in a given environment.

10.3 Scan images automatically in the pipeline

Add docker scout cves as a step in CI before pushing to the registry — stop vulnerable images before they ever reach production.

  • 10.4 Use a private registry for internal images
  • 10.5 Automatically clean up old images in the registry
  • 10.6 Build once, deploy everywhere
11Orchestration5 tips

11.1 Try Swarm for simple clustering

Example: docker swarm init followed by docker service create --replicas 3 myimage — built-in orchestration without having to stand up a full Kubernetes cluster for smaller workloads.

11.2 Update services with zero downtime

Example: docker service update --image myimage:2.0 --update-parallelism 1 --update-delay 10s myservice — rolling update, one instance at a time.

11.3 Use Docker Desktop's bundled Kubernetes for local testing

Before manifests go to a production cluster, test them locally against Desktop's integrated Kubernetes cluster for faster iteration.

  • 11.4 Define resource limits in orchestration manifests
  • 11.5 Keep health checks consistent between Compose and orchestration
12AI & Docker5 tips

12.1 Run AI models locally with Docker Model Runner

Example: docker model pull ai/smollm2:360M followed by docker model run ai/smollm2:360M — prototype against a language model locally without depending on external APIs or sending data to a third party.

12.2 Use registry mirrors for models

Docker Model Runner now supports registry mirrors, making it faster and more reliable to pull models in environments with limited or unstable internet connectivity.

12.3 Containerize MCP servers safely

Docker MCP Toolkit runs Model Context Protocol servers in containers, giving AI tools access to resources without installing them directly on the host machine — easier to sandbox and clean up.

  • 12.4 Ask Gordon before troubleshooting manually
  • 12.5 Version AI models just like images
13Production & Operations7 tips

13.1 Use restart policies deliberately

Example: docker run --restart unless-stopped myimage — let Docker automatically restart the container on crash or host reboot, except when you've stopped it manually yourself.

13.2 Log to the right driver for your environment

Example: docker run --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 myimage — stops container logs from growing unbounded and filling the disk.

13.3 Separate configuration from the image with environment variables

Build the same image for every environment and inject environment-specific configuration via environment variables or compose files — avoid building different images for test, staging, and production.

  • 13.4 Set up automatic restarts on health failures
  • 13.5 Document images with OCI standard labels
  • 13.6 Have a clear rollback plan
  • 13.7 Monitor containers continuously, not just during incidents
The full guide, as a PDFEvery tip above is written up in full. Free, no email needed.

Related guides: 100+ Tips & Tricks — Ubuntu Server 26.04 LTS, 100+ Linux Commands. All free guides are in the library.