Docker CLI Essentials

Learn the most important Docker CLI commands while understanding what they actually do internally.

Docker is heavily controlled through the command line.

At first, Docker commands can feel overwhelming because there are many of them.

But most daily Docker workflows rely on a relatively small set of essential commands.

The goal of this chapter is not command memorization.

The goal is understanding:

  • what Docker commands actually do
  • how they relate to images and containers
  • how they fit into container lifecycle behavior
  • how Docker is typically managed in real environments

Docker CLI Philosophy

Docker commands usually follow this pattern:

docker <object> <action>

For example:

docker container ls
docker image pull nginx
docker volume create

The command structure is intentionally organized around Docker resources.

Common Docker objects include:

  • containers
  • images
  • volumes
  • networks

Running Containers

The most famous Docker command is:

docker run nginx

Internally this command combines multiple operations:

Pull Image
Create Container
Start Container

This is why docker run feels so powerful.

It automates several lifecycle steps at once.


Listing Running Containers

To view running containers:

docker ps

Example:

CONTAINER ID   IMAGE    STATUS
a1b2c3d4       nginx    Up 5 minutes

This shows:

  • container ID
  • image used
  • current status

Only active containers appear here.


Listing All Containers

To show all containers:

docker ps -a

This includes:

  • running containers
  • stopped containers
  • exited containers

This helps visualize lifecycle behavior.


Starting and Stopping Containers

To stop a container:

docker stop web

To start it again:

docker start web

Simplified lifecycle:

Running
   ↓ stop
Stopped
   ↓ start
Running

These commands operate on existing containers.

They do not create new ones.


Restarting Containers

To restart:

docker restart web

Simplified behavior:

Stop Container
Start Container

This is commonly used after configuration changes or troubleshooting.


Removing Containers

To remove containers:

docker rm web

This permanently deletes the container.

However:

the image still remains locally

This distinction is very important.


Working with Images

Docker images can also be managed directly.

To list images:

docker images

Example:

REPOSITORY   TAG       IMAGE ID
nginx        latest    abc123
ubuntu       latest    def456

Images are reusable templates stored locally.


Downloading Images

To download an image manually:

docker pull nginx

Simplified workflow:

Docker Registry
Download Image
Store Locally

This process is called:

pulling an image

Removing Images

To delete an image:

docker rmi nginx

However, Docker usually prevents deleting images currently used by containers.

This protects running workloads.


Viewing Logs

Containers generate logs.

To inspect them:

docker logs web

Logs are critical for:

  • debugging
  • monitoring
  • troubleshooting

In many environments, container logs replace traditional server logging approaches.


Inspecting Containers

To view detailed container metadata:

docker inspect web

This provides information such as:

  • IP addresses
  • mounted volumes
  • environment variables
  • networking configuration
  • runtime metadata

Docker internally stores large amounts of container state information.


Executing Commands Inside Containers

To run commands inside a running container:

docker exec -it web bash

This opens an interactive shell session inside the container.

Common use cases include:

  • debugging
  • inspecting files
  • testing connectivity

However, modern production workflows usually avoid manual container modification.


Detached Mode

Containers often run in detached mode.

Example:

docker run -d nginx

The -d flag means:

run in background

This is extremely common in production environments.


Port Mapping

Containers are isolated by default.

To expose services externally:

docker run -p 8080:80 nginx

Simplified flow:

Browser
Host Port 8080
Container Port 80
Nginx Container

Port mapping becomes extremely important later when learning networking.


Naming Containers

Docker automatically generates random names.

Example:

focused_turing
epic_einstein

To assign a custom name:

docker run --name web nginx

This makes container management easier.


Viewing Resource Usage

Docker can display container resource usage.

Example:

docker stats

This shows:

  • CPU usage
  • memory usage
  • network traffic
  • disk activity

This becomes useful for monitoring and troubleshooting.


Docker Help System

Docker includes built-in documentation.

Examples:

docker --help
docker run --help

The CLI help system is extremely useful when learning Docker.

Even experienced engineers use it frequently.


Common Beginner Mistake

One common beginner mistake is treating Docker commands like isolated magic commands.

In reality, most commands manipulate:

  • images
  • containers
  • networks
  • volumes
  • lifecycle states

Understanding these relationships is more important than memorizing syntax.


Infrastructure Thinking

Modern infrastructure increasingly manages systems through APIs and automation.

Docker CLI commands are essentially human-friendly interfaces for interacting with container infrastructure.

For example:

Developer
Docker CLI
Docker API
Docker Engine

This architecture enables:

  • automation
  • orchestration
  • CI/CD integration
  • cloud-native tooling

Why This Matters

Docker CLI understanding is necessary before learning:

  • Docker Compose
  • networking
  • volumes
  • Dockerfiles
  • Kubernetes
  • orchestration systems

Almost all container workflows rely on CLI interaction at some level.


Key Takeaways

  • Docker commands manage containers, images, volumes, and networks
  • docker run combines multiple operations internally
  • Containers can be started, stopped, restarted, and removed
  • Images are reusable templates stored locally
  • Logs and inspection tools help troubleshoot containers
  • Port mapping exposes container services externally
  • Docker CLI commands interact with the Docker Engine
  • Understanding concepts is more important than memorizing syntax