We all know the importance of Docker, and how Docker plays a significant role in The Software Development Life Cycle. Learning Docker is fun to be honest, at least in my opinion. It is not hard once you grasp the basics. What is hard is to remember all those commands. There are tons and tons of it, We can look in the docs or use the ‘help’ command, but it takes time to find what we need.
So in this blog, I have gathered all the commands or more precisely all the top most used commands. It will help you to remember those commands and to refer them back quickly.
I’ve added a bit about what each command does, but you can still check the docs for more details and flags.
docker run
Run a container from an image. The image needs to be specified in the command. Docker will look for that image locally if it is unable to find , then it will look in the docker registry for that image or any other registry if specified.
docker run node:16
docker build
Build an image from a Dockerfile. You need to run this command when you want to push the image to the registry because the Dockerfile cannot be pushed directly.
docker build -t my-app:2 .
here “.” means the docker file is in the current directory.
docker pull
As we learn above docker run search image locally if not found then will fetch that from the remote repo and make a container out of it. But what if we need to pull an image but do not want to create a container out of it. Then we use docker pull command.
docker pull node:16
docker push
If you have an image and you want to push it into the docker registry then we need this command. The default registry is set to docker hub but we can send it somewhere else refer to the docker stopdoc for more information.
docker push myuser/my-app:2
docker images
This command will list all available images on your machine.
docker images
docker ps
Similar to images command this command will list all containers on your machine.
docker ps
docker stop
This command will stop a running container
docker stop <containerId or name>
// you can also pass multiple container id or name separated by space to stop all
docker start
It will help you to start a stop container.
docker start <containerId or containerName>
docker restart
As the name suggests it will restart a container.
docker start <containerId or containerName>
docker kill
Forcefully stopping a container.
docker restart <containerId or containerName>
docker rm/docker rmi
These are 2 different commands one is used for removing a container from the machine and the other is used for removing images. In order to remove an image you must stop the container running from that image or delete it.
docker rm <containerId or containerName>
docker rmi <imageId or imageName>
docker exec
You will use this command a lot. It basically helps you to run a command inside a running container. You will widely use this command for debugging purposes.
docker exec -it containerName bash
“-it” comprises two flags directing Docker to run the subsequent command in interactive mode and provide a shell for interaction. Without these flags, interacting with the container via a shell would not be possible.
docker logs
Fetch logs from a container.
docker logs <container name>
docker inspect
Display detailed information on one or more containers or images.
docker inspect <container name>
docker cp
Copy files/folders between a container and the local filesystem.
docker cp example.txt CONTAINER_ID:/path/in/container/
docker system prune
Remove all unused containers, networks, images, and optionally, volumes.
docker system prune
CONCLUSION
In short, this quick guide to key commands is handy for managing containers and building images. Knowing these basics makes handling Docker a lot less tricky!
If you found this content helpful, please show your support by giving it a clap, following me on LinkedIn and YouTube, and considering making a small contribution to Buy Me a Coffee☕️. Your support is greatly appreciated!