Useful Docker Commands
Useful Docker Commands Gathered in One Place
Run A Docker Image (Creates a Container)
You don’t have to pull the image first and then run “docker run” command. You can directly run with “docker run” command and if it finds an existing local version, it creates a new container out of it. If not, it automatically pulls the image first and then starts a container up.
docker run -d -p 9411:9411 openzipkin/zipkin
Docker Container Run (with Parameters)
Several parameters are provided here:
“-d” is to run it in detached mode so that when you close your terminal, your container will still be running.
“ — restart” has different options (default value is “no”) for restart policy, here we use “always” so that if Docker Desktop restarts, this container will also start up.
“-m” is for max memory provided to that container.
“ — cpu-quota” is for max CPU provided to that container. “100.000” is the max, here we use “5000” which is 5%.
You can use “docker container run” or directly “docker run”:
docker container run -p 5000:5000 -d --restart=always -m 512m --cpu-quota 5000
Find Your Container
See a list of running containers by “ls” command
docker container ls
Here you can either use the name under “NAMES” (here, my container name is “cn”, you can see it as the last value in the output) or min “4” characters of “CONTAINER ID” for your operations with your container (as you will see soon below)
To Pause a Running Container
docker container pause 88787
Unpause a Paused Container
docker docker unpause 88787
Stop a Running Container
This performs a graceful shutdown (SIGTERM):
docker container stop 88787
Kill a Running Container
This forces a running container to stop (SIGKILL):
docker container kill 88787
Grep Images List In Your Local
To list your images that includes a given string (here our string is “nilseri”), you can use “grep” command combined with “images”.
docker images | grep nilseri
Tag Your Images
You should tag your images as “latest” manually. Docker / Docker Hub does not automatically tag an image as “latest”. After tagging, the two images’ ids are now the same but they have different tags.
docker tag nilseri/score-segment:latest nilseri/score-segment:latestdocker tag nilseri/score-segment:latest nilseri/score-segment:0.0.1-SNAPSHOT
If you are planning to push your image to your Docker hub, you should first tag them in “<hub-user>/<repo-name>[:<tag>]” format
When you try to “docker run” an image that doesn’t exist in your local, it searches for the image in Docker Hub.
Search For Images Globally
You can make a global search for an image you are looking for:
docker search logstash
I searched for “logstash” and here are the results:
See An Image’s History
You can use id’s first 4 letters to search (as we did for containers):
With “history” command, you will be able to see the list of commands used to create our Docker image (to learn how it has been created):
docker image history 3568
Get Details About An Image Using Inspect Command
To get more details about an image, like with which configs it uses, environment variables and which image it is built on, etc., you can use “inspect” command:
docker image inspect 65f8
Clean Up Unused Containers
To get a full list of all your (running and stopped) containers, you can run this command:
docker container ls -a
To remove all stopped containers:
docker container prune
To Remove a Docker Image
You can use its full name or part of its image id like ‘d263’ (as we did with containers).
docker image rm nilseri/naming-server:0.0.1-SNAPSHOT
If you remove a container which is one of differently tagged but same containers (like we did under “Tag Your Images” section), it just removes its tag. You can fully remove when you also remove the other container.
When you perform a full list of your Docker images, there may appear untagged and unnamed images in your image list.
docker images --filter "dangling=true"
From Docker’s official docs:
This will display untagged images that are the leaves of the images tree (not intermediary layers). These images occur when a new build of an image takes the
repo:tagaway from the image ID, leaving it as
To clean them up (Docker will show a warning if they are being used in a running container):
docker rmi $(docker images -f "dangling=true" -q)
Or you may use your Docker Desktop application’s “Images” tab as well:
You can right click on an image to “Remove”.
Docker System Commands
Docker Engine Events
You can trace Docker logs when you start / stop a container, etc with “events” command.
Here, I stopped my container in one terminal windows while “docker events” command was running in another (if you just only run events command, no event will be printed; it will be just waiting there like we entered a “tail -f” command):
Gives process info about your running containers:
Docker Daemon Details (All Images/Containers)
A general view of images, containers, local volume and cache information:
docker system df
Top Processes Running (htop) In Your Image
docker top cn