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.
- Command to build docker image
- Command to tag any docker image
- Command to push any docker image into Docker hub
- Command to pull any docker image into local machine
- Command to run container using an image
- Command to remove/delete docker images
- 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.
- Kubernetes Series: Part 1 – Introduction to Kubernetes | Background of Kubernetes
- Kubernetes Series: Part 2 – Components of Kubernetes cluster | Kubernetes cluster in detail
- Kubernetes Series: Part 3 – What is Minikube and How to create a Kubernetes cluster (on Linux) using Minikube?
- Introduction to Ansible | High Level Understanding of Ansible
- Basics of automation using Ansible | Automate any task
- Automation of Java installation – using Ansible
- Automation of JBoss EAP installation – using ansible
- Jenkins Pipeline as code – High level information
- Jenkins pipeline script to build Java application and push artifacts into repository
- Jenkins pipeline script to build & deploy application on web server
- What is End-to-End Monitoring of any web application, and Why do we need it?
- What is “Monitoring” in DevOps? Why do we need to Monitor App/DB servers, Transactions etc.?
- DevOps Engineer or Software Developer Engineer which is better for you?- Let’s discuss
- How To Be A Good DevOps Engineer?
- How to do git push, git pull, git add, git commit etc. with Bitbucket