Table of contents
Introduction
Welcome DevOps enthusiasts and learners. In this blog we're gonna get our hands dirty and sum up all the concepts that we have learnt in the past few days about docker. We are going to create our very own cheat sheet for our future revisions and references.
From Day 16 to Day 19, we're gonna sum up every concepts and create a comprehensive cheat-sheet for ourselves. So without delaying, let's just begin.
Commands
Here's the cheat-sheet of Docker commands according to the tasks they perform:
To pull an Image from Docker Hub:
docker pull <image_name>:version
To run a container:
docker run <image_name>
Flags for running a container:
-d: to run in background
To inspect a container:
docker inspect <container_name>
List of Running Containers:
docker ps
List all the containers (Running or stopped):
docker ps -a
List of images on the system:
docker images
Docker Port Mapping:
docker run -p host_port:container_port <image_name>
Map a volume:
docker run -v HOSTDIR:TARGETDIR <image_name>
To assign a name to the container while running:
docker run --name Name <Image_name>
To stop a container:
docker stop <container_name_or_id>
To start a stopped container:
docker start <container_name_or_id>
To kill a container:
docker kill <container_name_or_id>
To remove a container:
docker rm <container_name_or_id>
Delete a running container:
docker rm -f <container_name_or_id>
Copy files from container to host:
docker cp <container_name>:<file_name> <file_name>
Copy files from host to container:
docker cp <file_name> <container_name>:<file_source>
Run a command inside a running container:
docker exec -it <container_name> <name_of_shell>
Create an image out of a container:
docker commit <container_name_or_id>
To delete an image:
docker rmi <image_name>
To delete all images:
docker image prune -a
To remove unused resources (container, Images, etc.):
docker system prune
Build an image from Dockerfile:
docker build DOCKERFILE
Build and tag the image at the same time:
docker build -t <Name> <DockerFile> .
Tag an image:
docker tag <image_id_or_name> <new_tag>
To connect with your Docker Hub account:
docker login <registry_url>/<repository_name>:<tag>
Push an image to Docker Hub:
docker push <registry_url>/<repository_name>:<tag>
See Logs of container:
docker logs <Container_name_or_id>
See Modified files in a container:
docker diff <Container_name>
See the stats of the running container:
docker stats
Delete Stopped containers:
docker containers prune
Rename a container:
docker rename <Old_name> <New_Name>
Save an image to a tar file:
docker save <Image> > FILE.tar
Load an image from a tar file:
docker load -i FILE.tar
Conclusion
In this cheat sheet, we covered the extensive set of Docker commands needed to manage containers, images, volumes, and more. Whether you're a beginner Docker installer or an experienced user looking to simplify your workflow, these commands provide intuitive tutorials for tasks ranging from running containers to images managing types to connectivity and troubleshooting.
By getting these commands right, you can effectively deploy, monitor, and maintain them, empowering you to use the full power of Docker in your development and deployment planning. With the knowledge in this cheat sheet, you're ready to enter the world of containerization with confidence.
I hope this blog added some Value to your learning. Do curate and share your own chat-sheet on LinkedIn and don't forget to tag me.
Ending this with a quote
Every great developer you know got there by solving problems they were unqualified to solve until they actually did it. - Patrick McKenzie
Happy Learning.