Table of Contents
Docker Usage Guide
This document collects practical Docker commands you can use in daily development and troubleshooting. It starts with the basics and then moves into more advanced but still broadly useful commands.
Core Concepts
- Image: A read-only template used to create containers.
- Container: A running or stopped instance of an image.
- Volume: Persistent storage managed by Docker.
- Network: A virtual network used for container-to-container or host-to-container communication.
Basic Commands
Check Docker Version and Info
docker --versionshows the installed CLI version.docker versionshows both client and server versions.docker infoshows engine details, storage driver, container count, and more.
Search and Download Images
- Use
searchto discover images on Docker Hub. - Use
pullto download a specific image and tag.
List Local Images
Run a Container
-druns the container in detached mode.-p 8080:80maps host port8080to container port80.--name webgives the container a readable name.
List Running and All Containers
Stop, Start, Restart, Remove
rm -fforce removes a running container.
Remove Images
Interactive Usage
Start a Shell in a New Container
-itcombines interactive and TTY modes.
Open a Shell in an Existing Container
Use exec when the container is already running and you want to inspect or debug it.
Logs and Monitoring
View Container Logs
-ffollows live log output.--taillimits how many recent lines are shown.
View Resource Usage
This is useful for checking CPU, memory, network, and block I/O usage.
Inspecting Containers and Images
Detailed Inspection
This returns JSON with configuration, mounts, ports, IP address, and metadata.
Useful Inspect Filters
These filtered templates are much faster than reading the full JSON output.
View Port Mappings
Working with Files
Copy Files Between Host and Container
Mount a Host Directory
This mounts the current host directory into /app inside the container.
Volumes
Create and List Volumes
Use a Volume
Inspect and Remove Volumes
Volumes are preferred over container-local storage when data must survive container recreation.
Networks
List and Create Networks
Attach a Container to a Network
Inspect and Remove Networks
Custom networks are useful when multiple containers need to communicate by container name.
Building Images
Build from a Dockerfile
Tag and Push Images
Advanced but Useful Commands
Show Only Specific Fields with --format
This is very useful when you want readable CLI output without manual filtering.
Filter Results
Inspect Container Processes
This helps when debugging containers that appear healthy but are not doing the expected work.
See Container Changes
This shows filesystem changes inside a container since it was created.
Commit a Container as an Image
This is helpful for temporary debugging workflows, though rebuilding from a Dockerfile is usually better for long-term use.
Save and Load Images
Useful when moving images between environments without pulling from a registry.
Export and Import Container Filesystems
Unlike save and load, this works with container filesystems instead of image layers.
Run Commands in One-Off Containers
--rmremoves the container automatically after the command finishes.
Set Environment Variables
Run PostgreSQL with Environment Configuration
This starts PostgreSQL with a database, user, password, port mapping, and a persistent volume.
Example using an environment file:
Example .env.postgres content:
Useful PostgreSQL follow-up commands:
If you want a temporary PostgreSQL container with no persisted data, remove the volume mount:
Limit CPU and Memory
This is useful for testing behavior under constrained resources.
Restart Policies
This is practical for local servers and lightweight long-running services.
Health Checks
Health checks help detect containers that are running but not actually ready.
Cleanup Commands
Remove Stopped Containers
Remove Unused Images
Remove Unused Volumes
Remove Unused Networks
Clean Everything Unused
Be careful with prune commands because they can remove data you still need.
Remove All Containers Shown by docker ps -a
This force-removes every container, including running ones.
Remove All Containers and Related Artifacts
If you want to remove all containers and also delete unused images, volumes, networks, and build cache:
If you want a more explicit cleanup flow:
Use the explicit version carefully. Removing all volumes deletes persisted database and application data.
Safer Variant for Scripts
If there may be no containers or no volumes, this avoids command errors:
Docker Compose Essentials
If Docker Compose is available through the Docker CLI plugin:
These are the most commonly used commands for multi-container applications.
Docker Watch
For most development workflows, Docker Watch is used through docker compose watch.
docker compose watchwatches the configured services for file changes.docker compose watch appwatches only theappservice.--no-upskips the initial build and startup step.--dry-runshows what would happen without applying changes.
Docker Watch is especially useful in development because it can sync changed files into the container or trigger a rebuild automatically.
Example compose.yml configuration:
What this example does:
- Changes in the project folder are synced into
/appin the container. - Changes to
package.jsontrigger an image rebuild. node_modules/is ignored to avoid unnecessary sync noise.
Typical workflow:
This is a good alternative to bind mounts when you want more controlled file sync behavior.
Practical Troubleshooting Examples
Check Why a Container Exits Immediately
Check Network Reachability Between Containers
Check Mounted Files
Check Real Startup Command
Recommended Daily Commands
If you only remember a short set, these are the most useful in daily work:
Notes
- Prefer named containers for easier management.
- Prefer volumes for persistent data.
- Prefer
--rmfor short-lived utility containers. - Use
inspect,logs, andexecfirst when debugging. - Use prune commands carefully in environments with important local data.