Important docker commands

7 important docker commands

Introduction

Docker is a containerization platform. It provides is OS-level virtualization. It helps us to package software and run them as container. We can deploy an application as docker container, but for this we need to build a docker image. 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 tag any docker image
  3. Command to push any docker image into Docker hub
  4. Command to pull any docker image into local machine
  5. Command to run container using an image
  6. Command to remove/delete docker images
  7. Command to remove/delete docker container

1. 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 .

2. 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

3. 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

4. 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

5. 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 8080:8080 tomcat-with-app:v1 

6. To remove/delete image

It will delete all unused images

docker image prune
docker image prune -a
Image deletion using ID

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

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

ubuntu@Linux01:~$ 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)

7. To remove/delete container

To stop any specific container

docker stop <container ID>

To remove all stopped container

docker container prune

 

Thank You.

 

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