Efficiency in Docker comes from mastering the Command Line Interface (CLI). Here are the essential commands every DevOps engineer uses daily.
1. Lifecycle Commands
Run and Interactive Mode
Run a container and get a shell inside it.
Action:
docker run -it --name test-ubuntu ubuntu bashResult:
root@45eb2345a1b2:/#(You are now inside the container. Type exit to leave)
Execute a Command in a Running Container
Action:
docker exec -it my-web-server ls /etc/nginxResult:
conf.d fastcgi_params mime.types modules nginx.conf ...2. Inspection & Logging
View Container Logs
Crucial for troubleshooting why a container won't start.
Action:
docker logs --tail 5 my-web-serverResult:
2026/04/10 12:00:01 [notice] 1#1: using the "epoll" event method
2026/04/10 12:00:01 [notice] 1#1: nginx/1.25.1
...Inspect Container Details
Returns a massive JSON with every detail about the container (IP, Volumes, etc.).
Action:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-web-serverResult:
172.17.0.23. Cleanup Commands
Docker can quickly consume your disk space.
Remove Unused Data (The Prune)
Action:
docker system prune -fResult:
Deleted Containers:
...
Deleted Networks:
...
Total reclaimed space: 1.45GBCommand Quick Reference
| Command | Description |
|---|---|
docker build | Build an image from a Dockerfile |
docker pull | Download an image from a registry |
docker run | Create and start a container |
docker stop | Stop a running container |
docker rm | Remove a stopped container |
docker rmi | Remove an image |
docker logs | Fetch logs of a container |
docker exec | Run a command in a running container |
Summary
- Use
-itfor interactive shells. - Use
execfor running commands in existing containers. - Use
pruneregularly to save disk space.