Docker Cheatsheet โ All Essential Commands for DevOps Engineers

๐ ๐๐๐จ๐ฎ๐ญ ๐๐ "Hi, I'm Ali masiu zama, a DevOps student passionate about cloud computing and automation. I'm currently learning AWS, Linux, Docker, and CI/CD pipelines, with a focus on automating workflows and building scalable solutions !
Table of contents
Show less
Table of Contents
โ๏ธ Install & Setup Docker
# Install Docker on Ubuntu
sudo apt update
sudo apt install docker.io -y
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Check version
docker --version
# Check Docker service status
sudo systemctl status docker
# Run hello-world container to test
sudo docker run hello-world
๐ Docker Basics
docker version # Show Docker version
docker info # Display system-wide information
docker help # Show all Docker commands
๐ฆ Container Management
# Run a new container
docker run -d nginx # Run in background
docker run -it ubuntu bash # Run interactively
docker run --name myapp -d nginx # Assign a custom name
docker run -p 8080:80 nginx # Map ports (host:container)
docker run -v /host/data:/container/data nginx # Mount volume
docker run -e VAR=value nginx # Set environment variable
# List containers
docker ps # List running containers
docker ps -a # List all containers
# Container operations
docker stop container_id # Stop container
docker start container_id # Start stopped container
docker restart container_id # Restart container
docker rm container_id # Remove container
docker kill container_id # Kill running container
docker pause container_id # Pause container
docker unpause container_id # Resume container
docker rename old_name new_name # Rename container
docker attach container_id # Attach to running container
docker exec -it container_id bash # Run command inside container
docker logs container_id # Show container logs
docker inspect container_id # Detailed info about container
# Export & import container
docker export container_id > mycontainer.tar
docker import mycontainer.tar myimage:latest
๐งฑ Image Management
# List and pull images
docker images # List local images
docker pull nginx # Download image
docker search ubuntu # Search image from Docker Hub
# Build and tag image
docker build -t myimage:1.0 . # Build image from Dockerfile
docker tag myimage:1.0 myrepo/myimage:latest # Tag image
# Remove & clean images
docker rmi image_id # Remove image
docker rmi -f image_id # Force remove
docker image prune # Remove unused images
docker image inspect image_id # Show image details
# Save & load images
docker save -o nginx.tar nginx:latest # Save image as tar
docker load -i nginx.tar # Load image from tar
๐งพ Dockerfile Commands
Below are the main Dockerfile instructions used to build custom images.
# Dockerfile Example
FROM ubuntu:20.04
MAINTAINER ali_masiu_zama
RUN apt update && apt install -y nginx
COPY index.html /var/www/html/
WORKDIR /app
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
๐น Common Dockerfile Instructions
Command | Description |
| Base image |
| Author name |
| Execute command during build |
| Copy files from host to container |
| Copy files (with URL/tar support) |
| Set working directory |
| Expose a port |
| Set environment variables |
| Define default command |
| Define executable command |
๐ Volumes & Data Management
# Create volume
docker volume create myvolume
# List volumes
docker volume ls
# Inspect a volume
docker volume inspect myvolume
# Run container with volume
docker run -d -v myvolume:/data nginx
# Remove unused volumes
docker volume prune
๐ Networking in Docker
# List networks
docker network ls
# Inspect network
docker network inspect bridge
# Create custom network
docker network create mynetwork
# Connect/Disconnect container
docker network connect mynetwork container_id
docker network disconnect mynetwork container_id
# Run container in specific network
docker run -d --name app1 --network mynetwork nginx
๐งฉ Docker Compose
Use
docker-compose.ymlto manage multi-container applications.
# Start containers in background
docker-compose up -d
# Stop containers
docker-compose down
# Restart containers
docker-compose restart
# View logs
docker-compose logs -f
# List running services
docker-compose ps
Example docker-compose.yml:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: root
๐งพ Docker Logs & Inspect
docker logs container_id # View logs
docker logs -f container_id # Follow live logs
docker inspect container_id # Inspect details
docker events # Show real-time Docker events
docker stats # Monitor resource usage
๐งน System Cleanup & Info
docker system df # Show disk usage
docker system prune # Remove unused containers, networks, images
docker system prune -a # Remove everything not in use
docker info # Display system information
docker version # Show Docker version details
โก Bonus: Docker Useful Shortcuts
# Remove all stopped containers
docker rm $(docker ps -aq)
# Remove all unused images
docker rmi $(docker images -q)
# Stop all running containers
docker stop $(docker ps -aq)
# Remove all volumes
docker volume rm $(docker volume ls -q)
Docker simplifies containerization โ and this cheatsheet gives you a one-stop reference to all major Docker commands used by DevOps engineers.

Thank you for reading. Happy Learning ๐!!!




