DEV Community

Cover image for Mastering Docker CLI: Essential Commands for Container and Image Management
Anusha Kuppili
Anusha Kuppili

Posted on

Mastering Docker CLI: Essential Commands for Container and Image Management

Docker becomes truly powerful when you stop memorizing commands and start understanding the lifecycle behind them.

Most beginners know docker run.

Few understand what happens before, during, and after that command.

This article breaks Docker CLI into a practical sequence:

  • Pull image
  • Start container
  • Monitor container
  • Enter running container
  • Stop and remove resources
  • Clean images safely

Once this sequence clicks, Docker stops feeling abstract.


Docker Lifecycle in One Mental Model

Think of Docker in four stages:

  1. Blueprint
  2. Engine Start
  3. Monitoring
  4. Cleanup

Blueprint

This is the image stage.

An image is a reusable template.

Engine Start

A container is created from the image.

Monitoring

You inspect running and stopped containers.

Cleanup

You stop unused resources and reclaim storage.

Step 1: Pull an Image Before Running

You can explicitly download an image without starting a container.

docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode

This downloads the image from Docker Hub.

Check downloaded images:

docker images
Enter fullscreen mode Exit fullscreen mode

Example output:

REPOSITORY TAG IMAGE ID SIZE
ubuntu latest ccc7a11d65b1 120MB
centos latest 328edc84f1b 193MB

This helps when preparing environments before deployment.

Step 2: Understand docker run Properly

This command does three things:

docker run nginx
Enter fullscreen mode Exit fullscreen mode

It:

Checks local image availability

Pulls image if missing

Creates and starts container

If nginx is missing locally:

Unable to find image 'nginx:latest' locally
Pulling from library/nginx
Status: Downloaded newer image

After first download, future runs use cache.

Step 3: Foreground vs Detached Mode
Foreground Mode

docker run kodekloud/simple-webapp
Enter fullscreen mode Exit fullscreen mode

Terminal stays attached.

Logs remain visible.

But terminal becomes blocked.

Detached Mode

docker run -d kodekloud/simple-webapp
Enter fullscreen mode Exit fullscreen mode

Now container runs in background.

Terminal remains free.

Docker returns container ID:

a043d40f85fefa414254e477...

This is production style.

Step 4: Why Ubuntu Containers Exit Immediately

A common beginner confusion:

docker run ubuntu
Enter fullscreen mode Exit fullscreen mode

Container exits instantly.

Why?

Because Ubuntu image has no default long-running process.

Docker containers need one active foreground process.

Fix:

docker run ubuntu sleep 5
Enter fullscreen mode Exit fullscreen mode

Now container stays alive for 5 seconds.

Step 5: Interactive Mode for Shell Access

To enter a container interactively:

docker run -it centos bash
Enter fullscreen mode Exit fullscreen mode

Meaning of flags

-i keeps stdin open

-t allocates terminal

Now you are inside container shell.

Example:

cat /etc/*release*
Enter fullscreen mode Exit fullscreen mode

Useful for:

testing packages

inspecting OS

learning image internals

Step 6: Monitor Containers with docker ps

To see running containers:

docker ps
Enter fullscreen mode Exit fullscreen mode

You get:

Container ID

Image

Command

Status

Name

Example:

CONTAINER ID   IMAGE   STATUS   NAMES
796856ac413d   nginx   Up 6 sec silly_sammet
Enter fullscreen mode Exit fullscreen mode

Step 7: Reveal Hidden Containers

To see stopped containers too:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

This is critical because stopped containers still consume metadata.

Example:

Exited (0)
Exited (137)
Exit code meaning

0 = graceful exit

137 = forcefully terminated

These codes matter during debugging.

Step 8: Execute Commands Inside Running Containers

Suppose a container is already running:

docker exec c1a9d3a7ca7 cat /etc/*release*
Enter fullscreen mode Exit fullscreen mode

This runs commands inside active container without opening new shell.

Useful for:

debugging

checking files

verifying runtime state

Step 9: Reattach to Background Containers

Detached container logs can be reopened:

docker attach a043d
Enter fullscreen mode Exit fullscreen mode

This reconnects terminal to running process.

Useful when debugging web apps started with -d.

Step 10: Stop Containers Gracefully

First inspect:

docker ps
Enter fullscreen mode Exit fullscreen mode

Then stop:

docker stop silly_sammet
Enter fullscreen mode Exit fullscreen mode

You can use:

name

full ID

partial ID

Step 11: Remove Containers

Stopped containers can be deleted:

docker rm silly_sammet
Enter fullscreen mode Exit fullscreen mode

Multiple containers:

docker rm 345 e0a 773
Enter fullscreen mode Exit fullscreen mode

Important:

A running container must be stopped before removal.

Step 12: Remove Images Safely

List images:

docker images
Enter fullscreen mode Exit fullscreen mode

Delete image:

docker rmi ubuntu
Enter fullscreen mode Exit fullscreen mode

But Docker blocks deletion if a container depends on it.

You must remove dependent containers first.

Common Beginner Rule
Container depends on image
Image deletion requires container cleanup first

That dependency explains many deletion errors.

Fast Command Cheat Sheet

docker pull ubuntu
docker images
docker run nginx
docker run -d nginx
docker run -it centos bash
docker ps
docker ps -a
docker exec <id> <cmd>
docker attach <id>
docker stop <id>
docker rm <id>
docker rmi <image>
Enter fullscreen mode Exit fullscreen mode

Final Thought

Docker CLI is not a list of commands.

It is a lifecycle:

image → container → process → cleanup

Once you think in that sequence, everything becomes easier.

The strongest Docker engineers are not those who memorize flags.

They understand what Docker is doing internally.

Top comments (0)