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
-d
to 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
-a
to 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
-f
to force removal.
12. Remove an Image:
docker rmi <image_id_or_name>
- Use
-f
to 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 nginx
List Running Containers:
docker ps
List All Containers (including stopped):
docker ps -a
Stop a Running Container:
docker stop CONTAINER_ID or CONTAINER_NAME
Start a Stopped Container:
docker start CONTAINER_ID or CONTAINER_NAME
Remove a Container:
docker rm CONTAINER_ID or CONTAINER_NAME
Image Management:
List Images:
docker images
Pull an Image from Docker Hub:
docker pull IMAGE_NAME[:TAG]
Example:
docker pull ubuntu:20.04
Build an Image from Dockerfile:
docker build -t IMAGE_NAME[:TAG] PATH_TO_DOCKERFILE
Example:
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_NAME
Example:
docker run -p 8080:80 nginx
Inspect Container Network:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' CONTAINER_ID or CONTAINER_NAME
Container Logs:
View Container Logs:
docker logs CONTAINER_ID or CONTAINER_NAME
Tail 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_NAME
List Volumes:
docker volume ls
Mount a Volume in a Container:
docker run -v VOLUME_NAME:CONTAINER_PATH IMAGE_NAME
Example:
docker run -v my-volume:/app/data my-app
Docker Compose:
Run Compose:
docker-compose up -d
Stop 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/Dockerfile
Build an image with a specific build context:
docker build -t image_name:tag -f path/to/Dockerfile path/to/build/context
Build an image with build arguments:
docker build --build-arg key=value -t image_name:tag path/to/Dockerfile
Listing Images:
List all images:
docker images
List images with additional details:
docker images -a
Removing Images:
Remove a specific image:
docker rmi image_name:tag
Remove all unused images:
docker image prune
Forcefully remove an image:
docker rmi -f image_name:tag
Pulling/Pushing Images:
Pull an image from Docker Hub:
docker pull image_name:tag
Push 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:tag
Load an image from a tarball:
docker load -i image_name.tar
Misc:
Search for an image on Docker Hub:
docker search image_name
Display detailed information about system usage:
docker system df
Clean 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 version
Docker System Information:
docker info
Docker Disk Usage:
docker system df
Container Information
List Running Containers:
docker ps
List All Containers (including stopped ones):
docker ps -a
Container Statistics:
docker stats [container_name or container_id]
Inspect Container Details:
docker inspect [container_name or container_id]
Image Information
List Downloaded Images:
docker images
Inspect 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 ls
Inspect Network Details:
docker network inspect [network_name or network_id]
Volume Information
List Volumes:
docker volume ls
Inspect Volume Details:
docker volume inspect [volume_name or volume_id]
Resource Usage
Show Resource Usage by Containers:
docker stats
Display CPU Usage in Container:
docker exec -it [container_name or container_id] top
Docker Compose
Docker Compose Version:
docker-compose version
Docker Compose Build:
docker-compose build
Docker Compose Up (Start Services):
docker-compose up -d
Docker Compose Down (Stop Services):
docker-compose down
Cleanup
Remove Stopped Containers:
docker container prune
Remove Unused Images:
docker image prune
Remove Unused Volumes:
docker volume prune
Remove 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 : )