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.
2.1 Docker Command Structure
Section titled “2.1 Docker Command Structure”Modern Docker commands follow a strict, object-oriented syntax:
docker <object> <command> [options] [arguments](Historically, it was just docker <command>, but it was refactored for clarity).
For example:
docker container run nginx(Modern) vsdocker run nginx(Legacy, but still widely used).docker image ls(Modern) vsdocker images(Legacy).
Most developers still use the legacy shorthand for speed, but the object-oriented structure is best for learning.
2.2 Container Commands
Section titled “2.2 Container Commands”These commands manage the entire lifecycle of a container.
docker run
Section titled “docker run”Creates and starts a container in one step. It is the command you will use most often.
docker run -d -p 8080:80 --name my-web nginxOptions:
-d: Run in detached mode (background).-p: Publish a port (HostPort:ContainerPort).--name: Assign a custom name to the container.
docker create
Section titled “docker create”Creates a container but does not start it. Useful when you need to prep a container but start it later.
docker create --name my-app ubuntudocker start / docker stop
Section titled “docker start / docker stop”Starts an existing stopped container, or gracefully stops a running one.
docker start my-appdocker stop my-appdocker restart
Section titled “docker restart”Restarts a container (essentially a stop followed by a start).
docker restart my-webdocker pause / docker unpause
Section titled “docker pause / docker unpause”Suspends all processes inside a container without stopping it, freeing up CPU.
docker pause my-webdocker unpause my-webdocker kill
Section titled “docker kill”Forcefully kills a running container by sending a SIGKILL signal (use stop instead unless the container is frozen).
docker kill my-webdocker rm
Section titled “docker rm”Removes a stopped container from the host completely.
docker rm my-web# Add -f to force remove a running container:docker rm -f my-webdocker ps
Section titled “docker ps”Lists running containers.
docker ps# To see ALL containers (including stopped ones):docker ps -a2.3 Container Interaction
Section titled “2.3 Container Interaction”Once a container is running, you often need to interact with it, check its output, or modify its files.
docker exec
Section titled “docker exec”Executes a new command inside an already running container. Extremely useful for debugging.
# Opens an interactive bash shell inside the containerdocker exec -it my-web /bin/bashdocker attach
Section titled “docker attach”Attaches your terminal’s standard input/output directly to the main process running inside the container.
docker attach my-webdocker logs
Section titled “docker logs”Fetches the logs of a container (anything the container prints to stdout/stderr).
docker logs my-web# To follow/tail the logs live:docker logs -f my-webdocker cp
Section titled “docker cp”Copies files or folders between a container and the local filesystem.
# Copy from host to containerdocker cp index.html my-web:/usr/share/nginx/html/
# Copy from container to hostdocker cp my-web:/var/log/nginx/error.log ./error.logdocker top
Section titled “docker top”Displays the running processes inside the container, similar to the Linux top command.
docker top my-webdocker port
Section titled “docker port”Lists the port mappings for a specific container, showing which host ports map to which container ports.
docker port my-web2.4 Container Inspection
Section titled “2.4 Container Inspection”When troubleshooting, you need deep visibility into how a container is configured.
docker inspect
Section titled “docker inspect”Returns low-level detailed information on Docker objects in JSON format.
docker inspect my-webThe 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:
docker inspect my-web | grep "IPAddress"2.5 Docker Information
Section titled “2.5 Docker Information”Commands to check the health and status of the overall Docker Engine on your host.
docker info
Section titled “docker info”Displays system-wide information regarding the Docker installation (number of containers, images, storage driver, and memory limits).
docker infodocker version
Section titled “docker version”Shows the installed version of both the Docker Client and the Docker Server (Daemon).
docker versiondocker system df
Section titled “docker system df”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.
docker system df2.6 Cleanup & Admin Commands
Section titled “2.6 Cleanup & Admin Commands”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.
docker system prune
Section titled “docker system prune”The ultimate cleanup command. It removes all stopped containers, unused networks, dangling images, and build cache.
docker system pruneNote: To also remove unused volumes, add the --volumes flag:
docker system prune --volumesdocker image prune
Section titled “docker image prune”Removes only “dangling” images (images that have no tag and are not referenced by any container).
docker image prune# To remove ALL unused images (not just dangling):docker image prune -adocker container prune
Section titled “docker container prune”Removes all stopped containers in one go. Much faster than deleting them one by one with docker rm.
docker container prunedocker volume prune
Section titled “docker volume prune”Removes all anonymous and unused local volumes. Critical for freeing up heavy disk space if databases are constantly rebuilt.
docker volume prunedocker network prune
Section titled “docker network prune”Removes all custom networks that are not currently used by at least one container.
docker network prunesystemctl Daemon Commands
Section titled “systemctl Daemon Commands”On Linux hosts, you often need to manage the Docker background service itself, not just the containers.
# Check if the Docker daemon is runningsudo systemctl status docker
# Restart the Docker engine (will stop all running containers!)sudo systemctl restart docker
# Enable Docker to start automatically on system bootsudo systemctl enable docker