Here’s a comprehensive guide to Docker container commands with explanations and examples. I’ll organize them by category so it’s easy to reference.
1. Running Containers
Command | Purpose | Example |
---|
docker run <image> | Run a container from an image | docker run nginx |
docker run -d <image> | Run container in detached mode (background) | docker run -d nginx |
docker run -it <image> | Run container interactively with terminal | docker run -it ubuntu bash |
docker run --name <name> <image> | Assign a custom name | docker run --name mynginx -d nginx |
docker run -p <host>:<container> <image> | Map ports from host to container | docker run -p 8080:80 nginx |
docker run -v <host>:<container> <image> | Mount a volume | docker run -v $(pwd):/app -it ubuntu bash |
docker run --rm <image> | Remove container after exit | docker run --rm ubuntu echo "Hello" |
docker run --env <VAR>=<value> <image> | Set environment variables | docker run -e ENV=dev ubuntu env |
2. Listing Containers
Command | Purpose | Example |
---|
docker ps | List running containers | docker ps |
docker ps -a | List all containers (running + stopped) | docker ps -a |
docker ps -q | List only container IDs | docker ps -q |
3. Managing Containers
Command | Purpose | Example |
---|
docker start <container> | Start a stopped container | docker start mynginx |
docker stop <container> | Stop a running container | docker stop mynginx |
docker restart <container> | Restart container | docker restart mynginx |
docker pause <container> | Pause container processes | docker pause mynginx |
docker unpause <container> | Resume paused container | docker unpause mynginx |
docker kill <container> | Kill container immediately | docker kill mynginx |
docker rm <container> | Remove container | docker rm mynginx |
docker rm -f <container> | Force remove running container | docker rm -f mynginx |
docker rename <old> <new> | Rename a container | docker rename mynginx nginx1 |
4. Accessing & Executing in Containers
Command | Purpose | Example |
---|
docker exec -it <container> <command> | Run a command in a running container | docker exec -it mynginx bash |
docker attach <container> | Attach terminal to a running container | docker attach mynginx |
docker logs <container> | View logs of a container | docker logs mynginx |
docker logs -f <container> | Stream logs in real-time | docker logs -f mynginx |
docker top <container> | Show running processes inside container | docker top mynginx |
docker stats <container> | Real-time resource usage | docker stats mynginx |
5. Inspecting Containers
Command | Purpose | Example |
---|
docker inspect <container> | Detailed JSON info | docker inspect mynginx |
docker diff <container> | Show changes in filesystem | docker diff mynginx |
docker port <container> | Show port mappings | docker port mynginx |
6. Copying Files
Command | Purpose | Example |
---|
docker cp <src> <container>:<dest> | Copy from host to container | docker cp index.html mynginx:/usr/share/nginx/html/ |
docker cp <container>:<src> <dest> | Copy from container to host | docker cp mynginx:/usr/share/nginx/html/index.html ./ |
7. Committing & Exporting Containers
Command | Purpose | Example |
---|
docker commit <container> <image> | Save container state as new image | docker commit mynginx mynginx:v1 |
docker export <container> -o file.tar | Export container filesystem as tar | docker export mynginx -o nginx.tar |
docker import <file.tar> <image> | Import tar as image | docker import nginx.tar mynginx:imported |
8. Cleaning Up Containers
Command | Purpose | Example |
---|
docker container prune | Remove all stopped containers | docker container prune |
docker rm $(docker ps -aq) | Remove all containers | docker rm $(docker ps -aq) |
✅ Tips for Working with Containers
- Use
--name
for easy reference instead of container ID.
- Always clean stopped containers (
docker container prune
) to save space.
- Use
docker exec -it
to debug running containers instead of starting a new one.
- Map host ports carefully to avoid conflicts (
-p
).
- For dev, use
--rm
to automatically remove containers when done.