π Day 20 Task: ππΌπ°πΈπ²πΏ π³πΌπΏ ππ²ππ’π½π ππ»π΄πΆπ»π²π²πΏπ: A Must-Have Cheat Sheet for Your Workflow π
DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab
Here's a Docker and Docker Compose Cheat Sheet that summarizes key commands and their usage. This cheat sheet is designed to be a handy reference for both beginners and experienced users.
Docker Cheat Sheet
1. Basic Docker Commands
List Docker Version
docker --version
Displays the installed version of Docker.List Running Containers
docker ps
Shows all running containers.List All Containers (including stopped)
docker ps -a
Displays all containers, regardless of state.Start a Stopped Container
docker start <container_name_or_id>
Starts a container that is currently stopped.Stop a Running Container
docker stop <container_name_or_id>
Stops a running container.Remove a Container
docker rm <container_name_or_id>
Removes a stopped container.Remove All Stopped Containers
docker container prune
Removes all stopped containers.View Container Logs
docker logs <container_name_or_id>
Displays logs for a container.
2. Docker Image Commands
List Docker Images
docker images
Lists all images on your system.Pull Image from Docker Hub
docker pull <image_name>
Downloads an image from Docker Hub.Build Docker Image from Dockerfile
docker build -t <image_name>:<tag> <path>
Builds an image from a Dockerfile in the specified path.Remove an Image
docker rmi <image_name_or_id>
Removes a Docker image from your system.Remove All Unused Images
docker image prune
Removes all dangling (unused) images.Remove All Unused Images (including tagged ones)
docker image prune -a
Removes all images that are not in use by containers.Save Docker Image to Tar Archive
docker save -o <filename>.tar <image_name>
Saves an image to a tarball.Load Docker Image from Tar Archive
docker load -i <filename>.tar
Loads a Docker image from a tarball.
3. Docker Container Commands
Run a Container
docker run <image_name>
Runs a container from a specified image.Run a Container in the Background (Detached Mode)
docker run -d <image_name>
Runs a container in the background.Run a Container with a Specific Name
docker run --name <container_name> <image_name>
Starts a container with a custom name.Run a Container with Environment Variables
docker run -e VAR_NAME=value <image_name>
Pass environment variables to the container at runtime.Run a Container with Port Mapping
docker run -p <host_port>:<container_port> <image_name>
Maps a port on your host to a port in the container.Run a Container Interactively (with a Shell)
docker run -it <image_name> /bin/bash
Runs the container interactively with a bash shell.
4. Docker Volumes
Create a Volume
docker volume create <volume_name>
Creates a named volume for persistent storage.List Volumes
docker volume ls
Lists all volumes on your system.Inspect a Volume
docker volume inspect <volume_name>
Displays detailed information about a volume.Remove a Volume
docker volume rm <volume_name>
Removes a specified volume.Remove All Unused Volumes
docker volume prune
Removes all unused volumes.
5. Docker Network Commands
List Networks
docker network ls
Lists all Docker networks.Create a Network
docker network create <network_name>
Creates a custom Docker network.Connect a Container to a Network
docker network connect <network_name> <container_name>
Connects a container to a network.Disconnect a Container from a Network
docker network disconnect <network_name> <container_name>
Disconnects a container from a network.Remove a Network
docker network rm <network_name>
Removes a specified network.
6. Docker Cleanup Commands
Clean Up Stopped Containers
docker container prune
Removes all stopped containers.Clean Up Unused Images
docker image prune
Removes dangling images.Clean Up Everything
docker system prune
Removes all unused containers, networks, and images.Clean Up Everything (Including Unused Images)
docker system prune -a
Removes unused containers, networks, and images (including tagged images).Clean Up Build Cache
docker builder prune
Removes build cache that is no longer used by any images.
Docker Compose Cheat Sheet
1. Basic Docker Compose Commands
Start Services Defined in
docker-compose.yml
docker-compose up
Starts all services defined in thedocker-compose.ymlfile.Start Services in Detached Mode
docker-compose up -d
Starts services in the background (detached mode).Stop Services
docker-compose down
Stops all services and removes containers, networks, and volumes.View Logs of Services
docker-compose logs
Displays logs for all services.View Logs of a Specific Service
docker-compose logs <service_name>
Displays logs for a specific service.Run a One-Off Command in a Service
docker-compose run <service_name> <command>
Runs a one-time command (like a shell or script) inside a service container.
2. Managing Docker Compose Services
View Running Services
docker-compose ps
Lists the running containers for the Compose project.Scale a Service
docker-compose up --scale <service_name>=<num_instances>
Scales a service to the desired number of replicas.Rebuild and Restart Services
docker-compose up --build
Rebuilds and restarts all services, even if no changes are made.Stop a Specific Service
docker-compose stop <service_name>
Stops a specific service.Remove Stopped Containers and Volumes
docker-compose down -v
Stops services and removes associated containers and volumes.
3. Docker Compose Volumes and Networks
Create Volumes and Networks
docker-compose up -d
Creates and starts volumes and networks as defined in thedocker-compose.ymlfile.Remove Volumes and Networks
docker-compose down -v
Stops services and removes both containers and volumes.
4. Docker Compose Health Check
- View Container Health Status
docker-compose ps
Shows the health status of all services defined in thedocker-compose.ymlfile.
Useful Docker Compose YAML Configuration
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
networks:
- my_network
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- my_network
networks:
my_network:
driver: bridge
This cheat sheet covers essential Docker and Docker Compose commands for everyday use. Keep it handy to improve your workflow and stay efficient when managing containers and services.
Feel free to share this with your team or the community! π


