Skip to content

02. Docker CLI

The Docker Command Line Interface (CLI) is your primary tool for interacting with the Docker Daemon. Mastering the CLI is essential for managing containers, images, volumes, and networks efficiently.

Modern Docker commands follow a strict, object-oriented syntax:

Terminal window
docker <object> <command> [options] [arguments]

(Historically, it was just docker <command>, but it was refactored for clarity).

For example:

  • docker container run nginx (Modern) vs docker run nginx (Legacy, but still widely used).
  • docker image ls (Modern) vs docker images (Legacy).

Most developers still use the legacy shorthand for speed, but the object-oriented structure is best for learning.

These commands manage the entire lifecycle of a container.

Creates and starts a container in one step. It is the command you will use most often.

Terminal window
docker run -d -p 8080:80 --name my-web nginx

Options:

  • -d: Run in detached mode (background).
  • -p: Publish a port (HostPort:ContainerPort).
  • --name: Assign a custom name to the container.

Creates a container but does not start it. Useful when you need to prep a container but start it later.

Terminal window
docker create --name my-app ubuntu

Starts an existing stopped container, or gracefully stops a running one.

Terminal window
docker start my-app
docker stop my-app

Restarts a container (essentially a stop followed by a start).

Terminal window
docker restart my-web

Suspends all processes inside a container without stopping it, freeing up CPU.

Terminal window
docker pause my-web
docker unpause my-web

Forcefully kills a running container by sending a SIGKILL signal (use stop instead unless the container is frozen).

Terminal window
docker kill my-web

Removes a stopped container from the host completely.

Terminal window
docker rm my-web
# Add -f to force remove a running container:
docker rm -f my-web

Lists running containers.

Terminal window
docker ps
# To see ALL containers (including stopped ones):
docker ps -a

Once a container is running, you often need to interact with it, check its output, or modify its files.

Executes a new command inside an already running container. Extremely useful for debugging.

Terminal window
# Opens an interactive bash shell inside the container
docker exec -it my-web /bin/bash

Attaches your terminal’s standard input/output directly to the main process running inside the container.

Terminal window
docker attach my-web

Fetches the logs of a container (anything the container prints to stdout/stderr).

Terminal window
docker logs my-web
# To follow/tail the logs live:
docker logs -f my-web

Copies files or folders between a container and the local filesystem.

Terminal window
# Copy from host to container
docker cp index.html my-web:/usr/share/nginx/html/
# Copy from container to host
docker cp my-web:/var/log/nginx/error.log ./error.log

Displays the running processes inside the container, similar to the Linux top command.

Terminal window
docker top my-web

Lists the port mappings for a specific container, showing which host ports map to which container ports.

Terminal window
docker port my-web

When troubleshooting, you need deep visibility into how a container is configured.

Returns low-level detailed information on Docker objects in JSON format.

Terminal window
docker inspect my-web

The JSON output contains critical information, including:

  • Container Metadata: ID, Name, State (Running, Paused, OOMKilled), and creation timestamps.
  • Container Configuration: Environment variables, entrypoints, and command arguments.
  • Network Information: The container’s internal IP address, MAC address, and gateway.
  • Mount Information: Details on attached volumes and bind mounts from the host machine.

Tip: You can use grep or jq to filter the massive JSON output:

Terminal window
docker inspect my-web | grep "IPAddress"

Commands to check the health and status of the overall Docker Engine on your host.

Displays system-wide information regarding the Docker installation (number of containers, images, storage driver, and memory limits).

Terminal window
docker info

Shows the installed version of both the Docker Client and the Docker Server (Daemon).

Terminal window
docker version

Shows Docker disk usage. Over time, old images and stopped containers consume massive amounts of disk space. This command shows exactly what is eating up your storage.

Terminal window
docker system df

When managing Docker servers, disk space runs out quickly due to dangling images, stopped containers, and unused volumes. These commands help you keep your system clean and manage the Docker daemon itself.

The ultimate cleanup command. It removes all stopped containers, unused networks, dangling images, and build cache.

Terminal window
docker system prune

Note: To also remove unused volumes, add the --volumes flag:

Terminal window
docker system prune --volumes

Removes only “dangling” images (images that have no tag and are not referenced by any container).

Terminal window
docker image prune
# To remove ALL unused images (not just dangling):
docker image prune -a

Removes all stopped containers in one go. Much faster than deleting them one by one with docker rm.

Terminal window
docker container prune

Removes all anonymous and unused local volumes. Critical for freeing up heavy disk space if databases are constantly rebuilt.

Terminal window
docker volume prune

Removes all custom networks that are not currently used by at least one container.

Terminal window
docker network prune

On Linux hosts, you often need to manage the Docker background service itself, not just the containers.

Terminal window
# Check if the Docker daemon is running
sudo systemctl status docker
# Restart the Docker engine (will stop all running containers!)
sudo systemctl restart docker
# Enable Docker to start automatically on system boot
sudo systemctl enable docker