The Top Docker Commands you must know

What is Docker

Docker is a set of platform-as-a-service products that uses OS-level virtualization to deliver software in packages called containers. Containers are usually isolated from one another and bundled with their own software libraries and configuration files, they can communicate with each other through well-defined channels.

Benefits of Docker

  • Docker makes it possible to get more apps running on the same old servers and also makes it easy to package and ship programs.
  • It allows users to package an application with all of its dependencies into a standard unit for software development.
  • Containers don’t have a high overhead i.e it doesn’t consume more resources such as computing power, memory, and storage, unlike virtual machines.

To install Docker in your system refer to this article How to install Docker-CE on Ubuntu 22.04. After you have installed it and Docker is up and running come back and continue.

Docker commands to use on a daily basis

Docker Run

Before you can continue, make sure that Docker is up and running, you can use the following command to check if it is running docker run hello-world

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

From the above, we can see that Docker is up and running. You have seen how to use docker run command, whenever you run this command, the Docker client first finds the image in the Docker registry and then loads the container, and lastly runs it.

Docker Pull

Docker pull downloads images from the Docker registry and saves them on our local machine. I know you might be wondering what might be docker images, give me a second I will explain to you in a moment. I want to use an example of a Redis image here, you can use anything you want such as PostgreSQL, Mysql, ubuntu, python images, etc. To pull Redis from the registry we use docker pull redis

$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
8740c948ffd4: Already exists 
a2271c958e57: Pull complete 
495af11a3eac: Pull complete 
18b045ddb54d: Pull complete 
f49c2d6d086c: Pull complete 
14ed0c386119: Pull complete 
Digest: sha256:325d5a448d8f6c1d30a0a0fb26090343279d4cf23258b26b1745862f332e9479
Status: Downloaded newer image for redis:latest

So now Redis has been downloaded into our system, you can check its existence now with the next command.

Docker Images

A docker image is a read-only template that contains a set of instructions for creating a container. From our example, Redis is built from the instructions given by its creator. Let’s see how to check the existence of images from our local system.

docker images

Here is the screenshot of our image Redis, it has the name, tag, image id, created day, and size.

nextgentips: docker images
nextgentips: docker images

To use this image, you docker run command as we have stated above. So let’s run this image and see what happens.

$ docker run redis
1:C 26 Jan 2023 05:28:46.464 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 26 Jan 2023 05:28:46.464 # Redis version=7.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 26 Jan 2023 05:28:46.464 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 26 Jan 2023 05:28:46.465 * monotonic clock: POSIX clock_gettime
1:M 26 Jan 2023 05:28:46.465 * Running mode=standalone, port=6379.
1:M 26 Jan 2023 05:28:46.465 # Server initialized
1:M 26 Jan 2023 05:28:46.465 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 26 Jan 2023 05:28:46.466 * Ready to accept connections

From the above output, take note of Ready to accept connections, It tells us that the image runs successfully and is ready to be used, Redis act as a cache, database as well as message broker. So am not going to tackle the ins and out of Redis.

Docker ps

Whenever you want to check the running containers, you use docker ps command, it’s a very handy command when it comes to docker. So let’s check if Redis is up and running:

docker ps
nextgentips: docker ps
nextgentips: docker ps

From the output, you can see that Redis is up and running with status and also given ports. So Redis is running out of the box, we didn’t hustle with the installation, so this is the benefit of running docker containers.

Whenever you want to remove the containers, always stop them first with docker stop <id>

docker stop <id>

But sometimes you have many running containers which you don’t need anymore, you can forcibly stop them and remove them all at once with the following command.

$ docker rm -f $(docker ps -aq)   
  • -f means force removal
  • $(docker ps -aq) means to list all the containers with their container IDs only

If you run docker ps -aq will list all the container IDs

$ docker ps -aq

Docker volumes

Volumes are used in persisting data generated by and used by the Docker containers. So when we run our Redis above data is persisted and whenever we want to run Redis those data are used without necessarily pulling the new Redis image. To check running volumes we use docker volumes ls

$ docker volumes ls
local     3c5bb87879555163a6ab96ab12ba40f3de5a9182f4fb8024f13ed62554e887a4
local     339040a520a17349a1a834a72e73b4dcdb915dacaf327a3d2391c52ad35ce6bc
local     cff002a049f37e9e3458c8fed4502197df5f636a959293945d24da2b7ca98e38

If you want to remove a certain volume use docker volume rm <volume-name>. Let’s remove the following volume

docker volume rm 3c5bb87879555163a6ab96ab12ba40f3de5a9182f4fb8024f13ed62554e887a4

You will get the name as the output meaning it’s successfully removed.

Sometimes you have very many running volumes and you want to remove them all then use the following command.

$ docker volume rm -f $(docker volume ls)  
  • -f means you want to forcibly remove the volumes
  • $(docker volume ls) means list all the volumes

Docker build

To create an image from the specification given in a template, use docker build . -t <username>/<image_name>:<version>

docker build . -t <username>/<image_name>:<version>

Docker push

It’s always advisable to save your images, so that whenever you want others to use they can easily pull them from the registries such as DockerHub. I know windows and Mac users have Docker Desktop that can save the images automatically but for Linux, you have to push the images.

docker push <username>/<image_name>:<version> 

Docker prune

Let’s say you no longer need certain containers, images, networks, and volumes then you can remove them with docker system prune. This will remove all the images, volumes, and networks except the volumes because it stores important data. Whenever you want to remove volumes prepend --volumes to the command.

$ docker system prune

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N]

# with volumes
$ docker system prune --volumes
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all volumes not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] 

Click yes if you want to remove it and No if not.

Docker rmi

When you want to remove a certain image use docker rmi command, this will remove one image at a time and only if the image isn’t running. You will get an error whenever you are removing a running image.

$ docker rmi redis
Error response from daemon: conflict: unable to remove repository reference "redis" (must force) - container 12f7db5f5fae is using its referenced image 19c51d4327cf

I am getting an error here because Redis is running, whenever you want to force prepend -f to force remove.

$ docker rmi -f redis
Untagged: redis:latest
Untagged: [email protected]:325d5a448d8f6c1d30a0a0fb26090343279d4cf23258b26b1745862f332e9479

But this is not recommended you can stop the running container first and then initiate removal. To stop a container run docker stop 0435d9faf66f

$ docker stop 0435d9faf66f 

0435d9faf66f this is the image ID, it’s always good to reference the containers with their IDs because if you use an image name you might remove the wrong container.

To remove all the images at once then we use docker rmi -f $(docker images -aq)

$ docker rmi -f $(docker images -aq) 
  • -f means force removal
  • $(docker images -aq) means to list all the images specifying the image IDs only.


Here we have learned various useful Docker commands one should learn in order to become Docker-ready. I hope it’s been informative and it opens up your mind. For further reading on docker check Docker documentation.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published. Required fields are marked *