Essential Docker Commands with Examples

Docker Image Commands
πΉ docker pull β Download an Image
Pulls an image from Docker Hub.
docker pull nginx
This downloads the official nginx image locally.
docker images β List Images
docker images
Shows all images available on your system.
πΉ docker rmi β Remove an Image
docker rmi nginx
Deletes the image if no container is using it.
Docker Container Commands
πΉ docker run β Create & Start a Container
docker run nginx
This command:
Pulls the image (if not present)
Creates a container
Starts it
πΉ Run Container in Detached Mode
docker run -d nginx
Runs the container in the background.
πΉ Run Container with Port Mapping
docker run -d -p 8080:80 nginx
Access nginx in browser:
http://localhost:8080
π Container Management Commands
πΉ docker ps β List Running Containers
docker ps
πΉ List All Containers (Including Stopped)
docker ps -a
πΉ docker stop β Stop a Container
docker stop <container_id>
πΉ docker start β Start a Stopped Container
docker start <container_id>
πΉ docker restart β Restart a Container
docker restart <container_id>
πΉ docker rm β Remove a Container
docker rm <container_id>
To force remove:
docker rm -f <container_id>
π Inspecting Containers
πΉ docker logs β View Container Logs
docker logs <container_id>
Very useful for debugging application issues.
πΉ docker exec β Access Running Container
docker exec -it <container_id> /bin/bash
Now youβre inside the container.
Exit:
exit
πΉ docker inspect β Detailed Container Info
docker inspect <container_id>
Shows:
IP address
Volumes
Network details
Configuration
π§Ή Cleanup Commands (Very Important)
πΉ Remove All Stopped Containers
docker container prune
πΉ Remove Unused Images
docker image prune
πΉ Remove Everything (Danger β οΈ)
docker system prune -a
Deletes:
All stopped containers
Unused images
Networks
Cache
π Use carefully.
π§ Understanding Container Lifecycle
A Docker container goes through these stages:
Created β Running β Stopped β Deleted
Commands mapping:
Create & start β
docker runStop β
docker stopStart again β
docker startDelete β
docker rm
Understanding this lifecycle helps in troubleshooting.
π Real DevOps Scenario
Run a Temporary Debug Container
docker run -it ubuntu /bin/bash
Use cases:
Debug networking
Test commands
Check OS behavior
Exit β container stops automatically.
Must-Know Commands Summary
| Command | Purpose |
docker pull | Download image |
docker run | Create & start container |
docker ps | List containers |
docker images | List images |
docker logs | View logs |
docker exec | Access container |
docker stop/start | Control containers |
docker rm/rmi | Cleanup |
Conclusion
Docker commands are the foundation of container management.
Once youβre comfortable with these commands, Docker stops feeling complex and starts feeling powerful.


