Essential Docker CLI Commands: Real-World Guide for Beginners and Developers
Docker CLI (Command Line Interface) is the primary way developers interact with Docker. Almost every Docker operation such as downloading images, creating containers, viewing logs, checking resource usage, and debugging applications is done through CLI commands.
In real projects, developers use Docker CLI daily while working on Spring Boot applications, microservices, databases, Redis, Kafka, CI/CD pipelines, and cloud deployments. Understanding these commands practically is more important than memorizing syntax.
Before learning Docker commands deeply, it is recommended to understand Docker Installation, Docker Architecture, Containerization vs Virtualization, Docker Images and Containers, and Dockerfile Tutorial.
How Docker CLI Works Internally
When you run a Docker command, the Docker Client sends the request to Docker Daemon. Docker Daemon performs the actual work such as pulling images, creating containers, starting services, or removing resources.
[ Developer ]
|
v
[ Docker CLI ]
|
v
[ Docker Daemon ]
|
+------------------> [ Images ]
|
+------------------> [ Containers ]
|
+------------------> [ Networks ]
|
+------------------> [ Volumes ]
In production systems, this architecture becomes very important while debugging deployment issues, scaling containers, or monitoring services.
Basic Docker Workflow
Before learning commands individually, understand the typical Docker lifecycle.
[ Docker Registry ]
|
| Pull Image
v
[ Local Docker Images ]
|
| Run Container
v
[ Running Container ]
|
| Stop / Remove
v
[ Stopped Container ]
In simple words:
- Download image
- Create container from image
- Run application inside container
- Stop or remove container when no longer needed
System Information Commands
Check Docker Version
docker version
This command shows Docker Client and Docker Server versions.
Real-world use case:
In enterprise applications, version mismatch can cause issues. For example, a developer may use Docker version 26 locally while production servers use older versions. Some features may behave differently.
Check Docker System Information
docker info
This command displays detailed Docker system information:
- Number of containers
- Number of images
- Storage driver
- Memory usage
- Docker root directory
- CPU information
In production debugging, docker info helps identify storage issues, memory limits, and runtime configuration.
Working with Docker Images
Docker images are templates used to create containers.
Simple analogy:
Image = Blueprint
Container = Running object created from blueprint
Download Docker Image
docker pull nginx
This command downloads the Nginx image from Docker Hub.
Realistic example:
Suppose your frontend team needs a local web server quickly for testing static files. Instead of manually installing Nginx, developers can simply pull and run the Nginx image.
List Downloaded Images
docker images
Displays all downloaded images stored locally.
Example output may include:
REPOSITORY TAG IMAGE ID SIZE
nginx latest abc123 187MB
mysql 8.0 def456 620MB
openjdk 17 ghi789 480MB
Remove Docker Image
docker rmi nginx
Removes Docker image from the local machine.
Real-world use case:
Docker images consume disk space. Development machines and CI/CD servers often clean unused images regularly.
Working with Docker Containers
Create and Run Container
docker run --name my-nginx -d -p 8080:80 nginx
Explanation:
docker runโ Create and start container--name my-nginxโ Assign container name-dโ Run in detached/background mode-p 8080:80โ Map host port to container portnginxโ Image name
Realistic example:
Suppose a frontend React application needs Nginx locally. Developers can quickly create a local web server using this command without installing Nginx manually.
Open browser:
http://localhost:8080
List Running Containers
docker ps
Shows currently running containers.
Real-world example:
In microservices development, developers may run multiple containers:
- Spring Boot API
- MySQL database
- Redis cache
- Kafka broker
- Nginx reverse proxy
docker ps helps check whether all services are running properly.
List All Containers
docker ps -a
Shows running and stopped containers.
This is useful when debugging failed containers because stopped containers still appear here.
View Container Logs
docker logs my-nginx
Displays application logs from the container.
Realistic production example:
Suppose a Spring Boot application container fails during startup because database connection properties are incorrect. Developers usually check container logs first.
docker logs payment-service
Common log examples:
- Database connection failure
- Port already in use
- Memory issues
- Missing environment variables
Stop Running Container
docker stop my-nginx
Gracefully stops the container.
Real-world example:
During deployment, old containers are stopped before new application versions are started.
Remove Container
docker rm my-nginx
Removes container permanently.
Containers consume disk space and system resources. Cleaning unused containers is an important maintenance task.
Container Lifecycle
Create Container
|
v
Running State
|
v
Stopped State
|
v
Deleted
Realistic example:
During CI/CD deployments, old containers may be stopped and deleted automatically before new containers are created.
Running Commands Inside Containers
docker exec -it my-nginx /bin/bash
This command opens terminal access inside the running container.
Real-world debugging example:
Suppose a Spring Boot application container cannot connect to MySQL. Developers may enter the container to test:
- Environment variables
- DNS/networking
- Installed files
- Configuration
Example:
docker exec -it user-service /bin/bash
Check Resource Usage
docker stats
Displays CPU and memory usage of running containers.
Real production example:
During high traffic, one microservice may consume excessive memory and restart repeatedly. Docker stats helps identify such issues quickly.
CONTAINER CPU % MEM USAGE
payment-api 80% 1.5GB
redis-cache 12% 120MB
mysql-db 55% 2GB
Clean Unused Docker Resources
docker system prune
Removes unused:
- Stopped containers
- Unused images
- Unused networks
- Build cache
Real-world example:
CI/CD build servers often accumulate many old images and containers. Without cleanup, disk space may become full.
Practical Example for Java Developers
Instead of installing Java manually, Docker can run Java directly:
docker run -it --rm openjdk:17 java -version
Realistic use case:
Suppose one project requires Java 8 while another requires Java 17. Docker allows developers to test multiple Java versions without changing system-wide installation.
Practical Example: Running MySQL Database
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=root \
-p 3306:3306 \
mysql:8.0
This creates a MySQL database container locally.
Real-world example:
Backend developers often use Docker containers for databases instead of installing MySQL directly on laptops.
Practical Example: Spring Boot Application
docker run -d -p 8080:8080 my-springboot-app
This runs a Spring Boot application container.
In microservices architecture, each service usually runs in a separate container:
[ API Gateway ] --> Port 9090
[ User Service ] --> Port 8081
[ Payment Service ] --> Port 8082
[ Notification ] --> Port 8083
[ MySQL ] --> Port 3306
[ Redis ] --> Port 6379
This is common in Spring Boot Microservices and Microservices Architecture
Common Docker CLI Mistakes
1. Confusing Images and Containers
Images are templates. Containers are running instances.
2. Forgetting Detached Mode
Without -d, containers may block the terminal.
3. Port Conflicts
Running multiple containers on the same host port causes errors.
Bind for 0.0.0.0:8080 failed: port is already allocated
4. Not Cleaning Old Containers
Too many unused containers and images consume disk space.
5. Storing Important Data Inside Containers
Data inside containers may disappear after deletion. Use Docker volumes for persistence.
Realistic Production Troubleshooting
Suppose users report that the payment API is down.
Typical debugging flow:
Step 1: Check running containers
docker ps
Step 2: Check logs
docker logs payment-service
Step 3: Check resource usage
docker stats
Step 4: Enter container
docker exec -it payment-service /bin/bash
Step 5: Restart service if required
docker restart payment-service
This is how Docker CLI commands are actually used in production support.
Interview Questions
What is docker run?
Creates and starts a container from a Docker image.
Difference between docker stop and docker kill?
docker stop performs graceful shutdown.
docker kill forcefully terminates the container immediately.
How do you check running containers?
docker ps
How do you check container logs?
docker logs container-name
How do you access terminal inside running container?
docker exec -it container-name /bin/bash
Interview Trap Questions
If a container is removed, will image also be deleted?
No. Images and containers are separate. Removing a container does not automatically remove the image.
Can two containers use the same host port?
No. Host ports must be unique. Otherwise Docker throws a port allocation error.
Does docker stop immediately terminate the application?
No. Docker first sends a graceful shutdown signal and waits before forcefully stopping.
Recommended Learning Path
- Docker Installation
- Docker Architecture
- Docker Images and Containers
- Dockerfile Tutorial
- Docker Compose Guide
- Spring Boot Microservices
- Kubernetes Introduction
Conclusion
Docker CLI commands are the foundation for working with containers. Developers use these commands daily to create containers, manage services, debug applications, monitor resource usage, and deploy systems.
Understanding Docker commands practically with realistic examples is more useful than simply memorizing syntax. In real projects, Docker CLI becomes essential for DevOps workflows, CI/CD pipelines, cloud deployment, and microservices management.
After learning Docker CLI, continue with Docker Images and Containers, Dockerfile Tutorial, and Docker Compose Guide