Back to Home
Published: Sun Aug 25 2024Updated: Mon Apr 27 2026EN
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

BASH
  • docker --version shows the installed CLI version.
  • docker version shows both client and server versions.
  • docker info shows engine details, storage driver, container count, and more.

Search and Download Images

BASH
  • Use search to discover images on Docker Hub.
  • Use pull to download a specific image and tag.

List Local Images

BASH

Run a Container

BASH
  • -d runs the container in detached mode.
  • -p 8080:80 maps host port 8080 to container port 80.
  • --name web gives the container a readable name.

List Running and All Containers

BASH

Stop, Start, Restart, Remove

BASH
  • rm -f force removes a running container.

Remove Images

BASH

Interactive Usage

Start a Shell in a New Container

BASH
  • -it combines interactive and TTY modes.

Open a Shell in an Existing Container

BASH

Use exec when the container is already running and you want to inspect or debug it.

Logs and Monitoring

View Container Logs

BASH
  • -f follows live log output.
  • --tail limits how many recent lines are shown.

View Resource Usage

BASH

This is useful for checking CPU, memory, network, and block I/O usage.

Inspecting Containers and Images

Detailed Inspection

BASH

This returns JSON with configuration, mounts, ports, IP address, and metadata.

Useful Inspect Filters

BASH

These filtered templates are much faster than reading the full JSON output.

View Port Mappings

BASH

Working with Files

Copy Files Between Host and Container

BASH

Mount a Host Directory

BASH

This mounts the current host directory into /app inside the container.

Volumes

Create and List Volumes

BASH

Use a Volume

BASH

Inspect and Remove Volumes

BASH

Volumes are preferred over container-local storage when data must survive container recreation.

Networks

List and Create Networks

BASH

Attach a Container to a Network

BASH

Inspect and Remove Networks

BASH

Custom networks are useful when multiple containers need to communicate by container name.

Building Images

Build from a Dockerfile

BASH

Tag and Push Images

BASH

Advanced but Useful Commands

Show Only Specific Fields with --format

BASH

This is very useful when you want readable CLI output without manual filtering.

Filter Results

BASH

Inspect Container Processes

BASH

This helps when debugging containers that appear healthy but are not doing the expected work.

See Container Changes

BASH

This shows filesystem changes inside a container since it was created.

Commit a Container as an Image

BASH

This is helpful for temporary debugging workflows, though rebuilding from a Dockerfile is usually better for long-term use.

Save and Load Images

BASH

Useful when moving images between environments without pulling from a registry.

Export and Import Container Filesystems

BASH

Unlike save and load, this works with container filesystems instead of image layers.

Run Commands in One-Off Containers

BASH
  • --rm removes the container automatically after the command finishes.

Set Environment Variables

BASH

Run PostgreSQL with Environment Configuration

BASH

This starts PostgreSQL with a database, user, password, port mapping, and a persistent volume.

Example using an environment file:

BASH

Example .env.postgres content:

ENV

Useful PostgreSQL follow-up commands:

BASH

If you want a temporary PostgreSQL container with no persisted data, remove the volume mount:

BASH

Limit CPU and Memory

BASH

This is useful for testing behavior under constrained resources.

Restart Policies

BASH

This is practical for local servers and lightweight long-running services.

Health Checks

BASH

Health checks help detect containers that are running but not actually ready.

Cleanup Commands

Remove Stopped Containers

BASH

Remove Unused Images

BASH

Remove Unused Volumes

BASH

Remove Unused Networks

BASH

Clean Everything Unused

BASH

Be careful with prune commands because they can remove data you still need.

Remove All Containers Shown by docker ps -a

BASH

This force-removes every container, including running ones.

If you want to remove all containers and also delete unused images, volumes, networks, and build cache:

BASH

If you want a more explicit cleanup flow:

BASH

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:

BASH

Docker Compose Essentials

If Docker Compose is available through the Docker CLI plugin:

BASH

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.

BASH
  • docker compose watch watches the configured services for file changes.
  • docker compose watch app watches only the app service.
  • --no-up skips the initial build and startup step.
  • --dry-run shows 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:

YAML

What this example does:

  • Changes in the project folder are synced into /app in the container.
  • Changes to package.json trigger an image rebuild.
  • node_modules/ is ignored to avoid unnecessary sync noise.

Typical workflow:

BASH

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

BASH

Check Network Reachability Between Containers

BASH

Check Mounted Files

BASH

Check Real Startup Command

BASH

If you only remember a short set, these are the most useful in daily work:

BASH

Notes

  • Prefer named containers for easier management.
  • Prefer volumes for persistent data.
  • Prefer --rm for short-lived utility containers.
  • Use inspect, logs, and exec first when debugging.
  • Use prune commands carefully in environments with important local data.
Previous TypeScript Types and Utility Types
Next Using JSON Schema in VS Code
Random Insta Public Archiver
An unhandled error has occurred. Reload