Docker provides us with various tools and utilities to create, manage, and share applications in isolated and packaged environments called containers. It uses multi-layered read-only templates called images to define the container environment which will run our application. We can use some important Docker image commands to maintain and manipulate Docker images easily.
After having worked with Docker for a considerable amount of time, you might have several Docker images already in your system. If you don’t know the basic image commands, it might be very difficult to manage such a huge number of images. To make this easier, Docker allows us to use simple image commands in the command-line to easily manage tons of images simultaneously.
We have created a curated list of important Docker image commands and have explained them in detail in our free Docker Tutorial. In this article, we will enlist all the important Docker Image commands that you can use to simplify the process of managing images.
Before we move ahead, it’s important to understand what is Docker? and other important concepts such as –
So without any further ado, let’s learn some important Docker Image commands.
Important Docker Image Commands
Docker Build Image Command
We can use the Docker build command to build Docker images using Dockerfiles. Dockerfiles are simple text files where we can specify instructions to create our container environment. Using the Dockerfile, we can create images by execution the Docker build command on it. This will start executing the instructions inside the Dockerfile step by step and each instruction will create a new image layer on top of the previous one.
Docker build syntax –
$ docker image build [OPTIONS] PATH | URL | -
The Docker build command uses the build context containing the Dockerfile and the source code of your application to build container environments. Once the image is built, you can create containers from it.
To get a detailed explanation of the Docker build command, you can check out this article on How to build Docker Images?. This article has an in-depth explanation of Docker build, Dockerfiles, and how to create your first Docker image.
List Docker Image
If you have lots of images in your host, it might be difficult to keep track of all these images. You can use the Docker list image command to list all the images along with parameters such as repository, image name, date of creation, size, image ID, tags, etc. There are two different commands to achieve the same result.
$ docker image ls
$ docker images
Along with this, you can also use several options to modify the output of the docker image ls command. For example, by default, the docker images command does not list the intermediate images. You can use the –all option to do so. You can also use the –filter option to filter your output according to the parameters that you provide.
To know more about the Docker Image List command, you can check out this article on How to list Docker images?. This article explains how to list images in detail along with several examples such as filtering the output, listing digests of images, etc.
Docker Image Pull Command
Docker allows you to pull images directly from Docker registries such as Docker hub. It contains several pre-built official and vendor-specific images such as Ubuntu, Apache, Nginx, Python, Java, Node, etc. You can use the Docker pull command to do so. On executing the Docker pull command, the daemon first checks for a similar image that exists on your system. If a match is found, it will not pull the new image and simply create a copy of the existing one. However, if not, it will start pulling it from the Docker hub.
To pull a Docker Image, you can use the following command.
$ docker pull [OPTIONS] NAME[:TAG|@DIGEST]
You can use the -a option to pull all the tagged versions of the particular image. You can also use the –quiet option to pull images without any verbose. If you want to know more about the Docker pull command, you can refer to our article on How to pull Docker Images?. You will be able to find several examples with all the options and screenshots here. It will walk you step by step starting from logging in to the Docker hub using command-line to using advanced options to modify the output.
Push Image to Docker hub
You can create your own repository in Dockerhub and push your Docker images there. This allows you to manage and share your images with your team and organization members. Pushing an image in the Docker hub is quite simple. We can use the Docker push image command after logging in to the Docker hub through the command line.
You will first need to tag the source image with the repository name in which you want to push the image. You can then push this target image to the Docker hub. The syntax of the Docker push command is –
$ docker push [OPTIONS] NAME[:TAG]
Options such as –all-tags can be used to push all the tagged version of an image to Docker hub. You can even use this command to push images to your private registry in your local machine.
Our article on How to push Docker images to Docker hub? explains the process in great detail with hands-on examples to tag and push images.
Docker Tag Image
We can use image tags to provide metadata or labels to images. This helps us to keep track of the versions of the images. Docker hub stores images in repositories which are just a set of similar images uniquely identified by tags. Tags can be anything – a version of the application, metadata, use of the application, technologies used to build it, etc. If we don’t specify any tag, Docker automatically appends the latest tag to the image.
The format of the image name in Docker is –
The last component specifies the tag given to the image. There are multiple scenarios where Docker image tags come into the picture. These are –
- When using FROM instruction inside a Dockerfile.
- While building a Docker image using Docker build command.
- Using Docker tag command to explicitly tag a Docker image.
- Tag Docker images for a private registry.
The syntax of the Docker tag command is –
$ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
You can refer to our article on How to tag Docker images? to learn about all the use-cases in greater detail.
Docker Remove Image
You can use the Docker image rm, Docker rmi, and Docker image prune commands to remove images from your local machine. There are two sets of commands to do so. The first one is to remove specific images one by one. The other one is to remove all Docker images together.
The syntax to delete or remove a Docker image is –
$ docker image rm [OPTIONS] IMAGE [IMAGE...]
$ docker rmi [OPTIONS] IMAGE [IMAGE...]
You can use any of the two syntaxes mentioned above.
To remove a Docker image, it’s important that you first remove all the containers associated with that image. If not, the daemon will throw an error. To overcome this, you can use the –force option to delete one or more images forcefully.
You can also use the Docker image prune command to delete all the unsued Docker images. To remove all images including unused and dangling ones, you can use the –all option.
$ docker image prune [OPTIONS]
To remove all the Docker images together, you can use the following set of commands.
$ docker rm -f $(docker ps -a -q) $ docker rmi -f $(docker images -q)
The first command removes all the containers and then, you can use the second command to delete all the images.
To know more about deleting images, you can check out our article on How to remove Docker images?. You will get access to all the different commands to remove images along with examples.
Docker provides us with lots of important Docker image commands that we can execute in the command-line to manage multiple images easily. Some of these image commands include – push, pull, build, remove, list, tag, etc.
You can learn about all these commands along with all the other important Docker concepts in our free Docker Tutorial. If you have any queries or suggestions, please let us know in the comment box and we will have our experts get back to you.