Top 24 Docker Commands Explained With Examples

In my previous blog post , I have explained in detail on how you can Install Docker and Docker-compose on Ubuntu

In this guide , I have explained Top 24 Docker Commands with examples.

Make sure you have sudo or root privileges to the system.

Docker Commands

1)The command to check the version of Docker installed.

docker --version

2)To look/search for available docker images from the Docker registry.

docker search nginx

nginx is the image name we are searching for , from the official docker registry.The image will have the basic nginx configuration.

3)To pull docker images from the Docker registry.

After searching for an image , If it is available you can pull it from the docker registry.

docker pull nginx

The above command will pull the nginx – docker image from the docker registry to the local system / server.

4)Listing all the docker images

docker images

The above command will list all the docker images stored locally on the system.

5)Creating / Running docker container from Docker image.

After pulling the image from the docker registry , We can run a docker container from that image.

docker run -it -d nginx

-d (detached mode)- It will run the docker container in the background.

-it (interactive mode) – means You can execute commands inside the docker container while it is running.

6)To list the actively running docker containers.

docker ps

The above command will list all the containers which are currently running.

7)To list all the docker containers

docker ps -a

The above command will list all the actively running as well the containers in the stopped state.

8)To stop a Container

If you want to stop a running docker container ,

docker stop containerID

9)To start a Container

To start a container which is in stopped state.

docker start containerID

10)To restart a Docker container

The command to restart the running docker container.

docker restart containerID

11)To login to running Docker container

If you want to execute some bash commands or make configuration changes within the docker container , You can access it using the below command.

docker exec -it containerID /bin/bash

12)To delete the stopped Docker containers

docker rm containerID

The above command will delete the docker container which is in stopped state from the system.

13)To delete Docker images from the Local system

docker rmi imageID

If you’are deleting an image , But If there are containers running with that images , You will get the below error.

Error response from daemon: conflict: unable to delete 0901fa9da894 (cannot be forced) - image is being used by running container 3e98722264b4

If you’re not running an containers from the docker image on the local system , You should be able to delete it.

14)To check logs of a running Docker container

docker logs -f containerID

-f (–follow) – To follow docker output logs or tail continuously

15)Killing docker containers

The docker stop command will give sometime for the container to shutdown gracefully , But sometimes It takes so much of time for the container to go to stopped state , In this case we will run docker kill to stop it immediately.

docker kill containerID

16)Login to Docker Hub registry (hub.docker.com)

docker login is a command which is used to login to docker hub registry from the local system.

Once logged in you can push or pull images from local system to private docker repository.

Before running docker login command , Make sure you have the credentials in place.

docker login

It will ask for username and password , Once validated , You should get a message as Login Succeeded

17)Removing docker hub registry login from the system.

In the previous command , We used docker login to login to docker hub registry on the local system.

Run the below command , If you want to remove the docker login from the system.

docker logout

And If you check /root/.docker/config.json file , You can see that the auth is removed from the system.

18)Check active resource usage by each containers

docker stats

The above command displays the active Memory and CPU usage of all the containers on the system.

19)Rename a Docker container

When you pull a image from the docker registry and run a container out of it , It will automatically set a name for a container.

If you want to change the name of the container at any time , Run the below command.

From the below image , You can see the name of the container is nervous_khayyam

To change the name of the container ,

docker rename nervous_khayyam newname

With the help of above command , We have changed the name of the container from nervous_khayyam to nodejs

20)To display system wide information of Docker

docker info

The above command will display information such as number of total container , Container running , containers stopped , number of images available on the system , kernel version , plugins , volumes , etc.

21)Inspecting a Docker container

The docker inspect command will list the complete information of the docker container in JSON format.

docker inspect containerID

You can also truncate the output and check based on what needs to be verified. For example , You can get the information only about the networking part (IP address) of the container as shown below.

docker inspect --format='{{ .NetworkSettings.IPAddress }}' containerID

22)Building docker images from Docker file

The docker build command builds docker images from a Dockerfile.

docker build -t imagename .

. represents the directory where Dockerfile is present.

23)Creating new docker images from a Container

If you have made any changes to the containers , You can create a new docker image from that docker using the below command.

docker commit containerID username/imagename:tag

username – the username of the hub.docker.com

24)Pushing Docker images from Local to Docker registry.

docker push username/imagename

Now If I login to Docker Hub , I can see a repository created with the image tagged as v1.1.

We have successfully pushed docker image to docker repository.

Conclusion

We have successfully learnt about the most useful docker commands with some examples.

Thanks for reading.Please do check out my other articles.