Essential Docker CLI Commands for Beginners (With Examples and Explanation)
The Docker Command Line Interface (CLI) is the main way developers interact with Docker. If you understand Docker CLI commands, you can easily create, run, and manage containers.
This guide explains Docker commands in a simple and practical way with real examples and syntax explanations so beginners can understand easily. :contentReference[oaicite:0]{index=0}
Related internal topics: /docker-installation, /docker-images-containers, /dockerfile-tutorial, /docker-architecture.
1. How Docker Works (Simple Flow)
Before learning commands, understand the workflow:
[ Docker Hub / Registry ]
|
| (download image)
v
[ Local Machine (Images) ]
|
| (run command)
v
[ Running Container ]
|
| (stop/remove)
v
[ Stopped Container ]
In simple words:
- You download an image
- You run it as a container
- You stop or delete it when done
2. System Information Commands
Command 1: Check Docker Version
docker version
Explanation: Shows Docker client and server versions.
Command 2: Check Docker Details
docker info
Explanation: Displays system details like number of containers, images, memory usage.
Example
If Docker is not running, this command will fail. So it helps verify installation.
3. Working with Docker Images
Images are like templates (similar to Java class). Containers are instances (like objects).
Command: Pull Image
docker pull nginx
Explanation:
- Downloads nginx image from Docker Hub
- Stores it locally
Command: List Images
docker images
Explanation: Shows all downloaded images.
Command: Remove Image
docker rmi nginx
Explanation: Deletes an image.
Example
If you no longer need nginx:
docker rmi nginx
This frees disk space.
4. Working with Containers
Command: Run Container
docker run --name my-nginx -d -p 8080:80 nginx
Explanation:
docker run→ Creates and starts container--name my-nginx→ Custom container name-d→ Run in background-p 8080:80→ Map port 8080 (host) to 80 (container)nginx→ Image name
Real Example
After running the above command:
- Open browser
- Go to
http://localhost:8080 - You will see nginx welcome page
Command: List Running Containers
docker ps
Explanation: Shows currently running containers.
Command: List All Containers
docker ps -a
Explanation: Shows running + stopped containers.
Command: View Logs
docker logs my-nginx
Explanation: Shows output logs of container.
Command: Stop Container
docker stop my-nginx
Explanation: Gracefully stops container.
Command: Remove Container
docker rm my-nginx
Explanation: Deletes container permanently.
5. Useful Advanced Commands
Run Command Inside Container
docker exec -it my-nginx /bin/bash
Explanation:
- Access container terminal
-it→ interactive mode
Check Resource Usage
docker stats
Explanation: Shows CPU, memory usage of containers.
Clean Unused Resources
docker system prune
Explanation: Removes unused containers, images, cache.
6. Real-World Use Case (Java Developer)
Instead of installing Java manually, you can use Docker:
docker run -it --rm openjdk:17 java -version
Explanation:
-it→ interactive terminal--rm→ auto delete container after runopenjdk:17→ Java image
This is useful for testing Java versions without installing them.
7. Flow Chart: Container Lifecycle
Create Container (docker run)
|
v
Running State
|
v
Stopped State (docker stop)
|
v
Deleted (docker rm)
8. Common Mistakes to Avoid
1. Not Cleaning Containers
Too many unused containers consume disk space.
2. Confusing Image and Container
Image = template, Container = running instance.
3. Port Conflicts
Running multiple containers on same port causes error.
4. Not Using Names
Without names, managing containers becomes difficult.
5. Forgetting Detached Mode
Without -d, terminal gets blocked.
9. Interview Notes
Difference between docker stop and docker kill?
docker stop → Graceful shutdown
docker kill → Force stop immediately
How to access running container?
docker exec -it container_name /bin/bash
How to check container logs?
docker logs container_name
How to check running containers?
docker ps
What is docker run?
Creates and starts a container from image.
10. Summary
Docker CLI is the main tool to manage containers. By learning basic commands like docker pull, docker run, docker ps, and docker stop, you can control the entire container lifecycle.
With Docker CLI, developers can quickly create environments, test applications, and deploy services efficiently. It simplifies development and is widely used in modern DevOps and microservices architecture.
Next step: Learn /docker-images-containers to understand how images and containers work internally.