Important docker commands

13 Important Docker Commands

Last Updated on March 15, 2024 by cscontents

Introduction

Docker is a containerization platform. It provides is OS-level virtualization. It helps us to package software and run them as container. In the ever-evolving landscape of modern software development, Docker has emerged as a game-changer, revolutionizing the way applications are built, deployed, and managed. We can deploy applications as docker container, for this we need to build a docker image and deploy the container. All those steps can be easily performed if we know the docker commands properly.

In this article we will see some important docker commands.

Docker Commands

We have segregated the docker commands based on their use.

  1. Command to build docker image
  2. Command to get the docker images that are on the host machine
  3. Command to tag any docker image
  4. Command to push any docker image into Docker hub
  5. Command to pull any docker image into local machine
  6. Command to run container using an image
  7. Command to get the docker containers
  8. Command to get the logs of a specific docker container
  9. Command to go inside a docker container
  10. Command to remove/delete docker images
  11. Command to stop and remove/delete docker container
  12. Command to save any docker image as zip/tar file
  13. Command to load docker image from zip/tar file.

To build docker image

To build docker image we need Dockerfile. Assuming we have a Dockerfile, we need to run the below command from the same directory where Dockerfile exist.

docker build -t tomcat-with-app:v1 .

To get the docker images 

To get the docker images that are on the docker host machine, we need to run the below command.

docker images

To tag docker image

To tag any docker image we need to run the below command.

docker tag <image id or image name> testuser/tomcat-with-app:v1

e.g. docker tag tomcat-with-app:v1 testuser/tomcat-with-app:v1

To push an image onto Docker hub

At first, we need to log into docker hub, for this we need to run the below command. Here, prerequisite is Docker hub account. If you don’t have an account, please create one.

docker login

Before pushing into docker hub we need to tag the image with docker hub username. We can use the below command to tag it.

docker tag <image id or image name> testuser/tomcat-with-app:v1

Now, to push the image

docker push testuser/tomcat-with-app:v1

To pull docker image from Docker hub

We need to run below command to pull any image from public image repository (on Docker hub)

docker pull <image name>

e.g. docker pull tomcat

To run container using a docker image

We need to run the below command to run a container from a given image.

docker run -d -p <port for outside access>:<port on which app is running> <image name>

-d: it is used to run the container in detach mode

-p: it is used to specify the port

e.g. docker run -d -p 80:8080 tomcat-with-app:v1 

To attach a host directory as volume in docker container

In some use case we might need to attach a host directory to docker container (as docker volume). And this volume will be used by the container. For example, you have some config files in a directory of docker host and you want to attach this directory to the container, in that you can run the below command.

docker run -d -v /home/ubuntu/config-files:/app/config-files tomcat-with-app:v1 -p 80:8080

In the above command,

-v flag is used to attach a volume with the container. In this case /home/ubuntu/config-files directory of the host machine will be attached as /app/config-files directory in the docker container.

To get the docker container

To get the running docker containers (in the docker host machine), we need to run the below command.

docker ps

The above command will show only the running containers.

If we want to get all the docker containers, we need to run the below command. It will show all the running and stopped docker containers.

docker ps -a

To get logs of a docker container

To get logs for a docker container, we need to run the below command.

docker logs <container_id>

To get the logs in real time, run the above command with -f

docker logs <container_id> -f

To login/enter inside a docker container

To enter inside a docker container, we need to run below command.

docker exec -it <container id>  /bin/bash

or

docker exec -it <container id>  /bin/sh

To remove/delete image

If we know the image ID or image name with tag, then we can delete/remove that image from the docker host by running the below command. We will take one example image nginx_with_app:v1

docker rmi -f <image id>

or

docker rmi -f nginx_with_app:v1

To delete/remove all the all unused image from the docker host, run the below command.

docker image prune
docker image prune -a
Image deletion using ID (Important for automation)

From CLI (Command Line Interface) it is very to delete the images using their ID or ‘name & tag’ since we can easily get their ID if we run the 'docker images' command. But if we want to do the same thing in automation (for e.g., in Jenkins pipeline) then we need to get the image ID dynamically and pass the image ID to the command, so that the concerned image can be deleted.

To get the ID of images

docker images -a -q

We can use the below command to remove all the images using their ID.

docker rmi $(docker images -a -q)

The below command is same as previous command, but here we’re using -f flag to force the deletion.

docker rmi -f $(docker images -a -q)
To delete specific docker image (Important for automation)

For example, at first we will get the ID of the image.

docker images -a -q testuser/tomcat-with-app2:v1

Output:

7a2f1e6fbbc3

The above command printed the ID of the image testuser/tomcat-with-app2:v1

To delete this image we can directly use the below command.

docker rmi -f $(docker images -a -q testuser/tomcat-with-app2:v1)

To stop and remove/delete container

To delete/remove any specific container (whether it is running or stopped), we need to run the below command.

docker rm -f <container ID>

To stop any specific container

docker stop <container ID>

To remove all stopped container

docker container prune

To save any docker image as zip/tar file

To save any docker image as zip/tar file, we need to run the below command. Taking one example of nginx image.

docker save nginx:latest > nginx_latest.tar

Now, this nginx_latest.tar file can be shared or it can be used later to load the nginx image.

To load docker images from a zip/tar file

Given a file zip/tar file of  a docker image, we can run the below command to load docker image from that file.

docker load --input nginx_latest.tar

or

docker load < nginx.tar

Conclusion

In conclusion, mastering Docker commands is not merely a skill, but a strategic advantage in today’s fast-paced world of software development. From simplifying deployment workflows to optimizing resource utilization, Docker commands empower developers to harness the full potential of containerization.

 

Thank You.

 

If you are interested in learning DevOps, please have a look at the below articles, which will help you greatly.