Day 20: Docker Cheatsheet ๐

๐ Aspiring DevOps Engineer | Automation Enthusiast ๐ค ๐ง Bridging Dev and Ops to streamline software delivery and boost productivity. ๐ Learning CI/CD, containerization, and infrastructure as code for better software workflows. ๐ก Passionate about optimizing processes and reducing manual tasks for top-notch software quality. ๐ ๏ธ Exploring DevOps tools: Docker, Kubernetes, Jenkins, Terraform, and more. ๐ Let's connect, learn, and grow together in the exciting world of DevOps! ๐ฑ
Today is the 20th day of #90daysofdevops and now is the time to share all the learning with all of you. so today I will provide you all with a cheat sheet of Docker so you can easily understand the commands and what the command does : )
Running a new Container
1. Search for an Image:
docker search <image_name>
2. Pull an Image:
docker pull <image_name>:<tag>
# Example:
docker pull ubuntu:latest
3. Run a Container:
docker run [options] <image_name>
# Example:
docker run -it ubuntu:latest
-it: Interactive mode with a pseudo-TTY.Use
-dto run in detached mode (in the background).
4. Name the Container:
docker run --name <container_name> <image_name>
# Example:
docker run --name my_container -it ubuntu:latest
5. Map Ports:
docker run -p <host_port>:<container_port> <image_name>
# Example:
docker run -p 8080:80 nginx:latest
6. Map Volumes:
docker run -v <host_path>:<container_path> <image_name>
# Example:
docker run -v /host/path:/container/path ubuntu:latest
7. Environment Variables:
docker run -e <variable_name>=<value> <image_name>
# Example:
docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest
8. View Running Containers:
docker ps
- Use
-ato show all containers.
9. Access Container Shell:
docker exec -it <container_id_or_name> /bin/bash
10. Stop a Running Container:
docker stop <container_id_or_name>
11. Remove a Container:
docker rm <container_id_or_name>
- Use
-fto force removal.
12. Remove an Image:
docker rmi <image_id_or_name>
- Use
-fto force removal.
13. Show Container Logs:
docker logs <container_id_or_name>
14. Inspect Container Details:
docker inspect <container_id_or_name>
15. Clean Up (Remove All Stopped Containers):
docker container prune
16. Clean Up (Remove All Unused Images):
docker image prune
17. Clean Up (Remove All Unused Resources):
docker system prune
18. Build an Image from Dockerfile:
docker build -t <image_name>:<tag> <path_to_dockerfile>
# Example:
docker build -t custom_image:latest .
Feel free to customize these commands based on your specific requirements and Docker setup.
Docker Cheat sheet to Manage Containers
Basic Commands:
Run a Container:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]Example:
docker run -d -p 8080:80 --name my-container nginxList Running Containers:
docker psList All Containers (including stopped):
docker ps -aStop a Running Container:
docker stop CONTAINER_ID or CONTAINER_NAMEStart a Stopped Container:
docker start CONTAINER_ID or CONTAINER_NAMERemove a Container:
docker rm CONTAINER_ID or CONTAINER_NAME
Image Management:
List Images:
docker imagesPull an Image from Docker Hub:
docker pull IMAGE_NAME[:TAG]Example:
docker pull ubuntu:20.04Build an Image from Dockerfile:
docker build -t IMAGE_NAME[:TAG] PATH_TO_DOCKERFILEExample:
docker build -t my-custom-image:1.0 .Remove an Image:
docker rmi IMAGE_ID or IMAGE_NAME[:TAG]
Container Networking:
Expose Ports:
docker run -p HOST_PORT:CONTAINER_PORT IMAGE_NAMEExample:
docker run -p 8080:80 nginxInspect Container Network:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINER_ID or CONTAINER_NAME
Container Logs:
View Container Logs:
docker logs CONTAINER_ID or CONTAINER_NAMETail Container Logs:
docker logs -f CONTAINER_ID or CONTAINER_NAME
Executing Commands Inside Containers:
Execute a Command Inside a Running Container:
docker exec -it CONTAINER_ID or CONTAINER_NAME COMMAND [ARG...]Example:
docker exec -it my-container bash
Volume Management:
Create a Volume:
docker volume create VOLUME_NAMEList Volumes:
docker volume lsMount a Volume in a Container:
docker run -v VOLUME_NAME:CONTAINER_PATH IMAGE_NAMEExample:
docker run -v my-volume:/app/data my-app
Docker Compose:
Run Compose:
docker-compose up -dStop Compose:
docker-compose down
This cheat sheet covers some common Docker commands for container management. Feel free to customize and add more commands based on your specific use cases and requirements.
Docker Images Cheat sheet
Building Images:
Build an image from a Dockerfile:
docker build -t image_name:tag path/to/DockerfileBuild an image with a specific build context:
docker build -t image_name:tag -f path/to/Dockerfile path/to/build/contextBuild an image with build arguments:
docker build --build-arg key=value -t image_name:tag path/to/Dockerfile
Listing Images:
List all images:
docker imagesList images with additional details:
docker images -a
Removing Images:
Remove a specific image:
docker rmi image_name:tagRemove all unused images:
docker image pruneForcefully remove an image:
docker rmi -f image_name:tag
Pulling/Pushing Images:
Pull an image from Docker Hub:
docker pull image_name:tagPush an image to Docker Hub:
docker push image_name:tag
Tagging Images:
Tag an image:
docker tag source_image:source_tag new_image:new_tag
Inspecting Images:
Inspect image details:
docker inspect image_name:tag
Image History:
Display the history of an image:
docker history image_name:tag
Saving/Loading Images:
Save an image to a tarball:
docker save -o image_name.tar image_name:tagLoad an image from a tarball:
docker load -i image_name.tar
Misc:
Search for an image on Docker Hub:
docker search image_nameDisplay detailed information about system usage:
docker system dfClean up unused resources (images, containers, networks):
docker system prune
Remember to replace "image_name" and "tag" with your actual image name and tag. Adjust the commands based on your specific use case and requirements.
Docker Info and Stats Cheat sheet
General Information
Docker Version:
docker versionDocker System Information:
docker infoDocker Disk Usage:
docker system df
Container Information
List Running Containers:
docker psList All Containers (including stopped ones):
docker ps -aContainer Statistics:
docker stats [container_name or container_id]Inspect Container Details:
docker inspect [container_name or container_id]
Image Information
List Downloaded Images:
docker imagesInspect Image Details:
docker image inspect [image_name or image_id]Display Image History:
docker history [image_name or image_id]
Network Information
List Networks:
docker network lsInspect Network Details:
docker network inspect [network_name or network_id]
Volume Information
List Volumes:
docker volume lsInspect Volume Details:
docker volume inspect [volume_name or volume_id]
Resource Usage
Show Resource Usage by Containers:
docker statsDisplay CPU Usage in Container:
docker exec -it [container_name or container_id] top
Docker Compose
Docker Compose Version:
docker-compose versionDocker Compose Build:
docker-compose buildDocker Compose Up (Start Services):
docker-compose up -dDocker Compose Down (Stop Services):
docker-compose down
Cleanup
Remove Stopped Containers:
docker container pruneRemove Unused Images:
docker image pruneRemove Unused Volumes:
docker volume pruneRemove Unused Networks:
docker network prune
Remember to replace [container_name or container_id], [image_name or image_id], [network_name or network_id], and [volume_name or volume_id] with the actual names or IDs.
Feel free to customize the commands based on your specific needs and preferences.
Follow me on my Socials and stay tuned with more such amazing content : )
Socials:
Github: https://github.com/dipen006
LinkedIN: https://www.linkedin.com/in/dipenr06/
Twitter: https://twitter.com/dipenr06
~Dipen : )






