Docker Image: Commands And Their Usage
If you haven’t, then I would suggest that you go through Introduction to Docker to have a better understanding of the basics of docker and images in general.
Now, let’s get started with some of the most used and important commands that you would need to use when working with images.
docker container build
So if you want to build your own docker image, which at some point you would need to do, this is the command for you.
It is accompanied by many flags, as any important command would be. Let’s go through some them 1 by 1.
So you have a Dokerfile, a file used to create new images, and that file needs some arguments. You would then simply need to go
docker image build --build-arg <arguments> -f <name of dockerfile>
— file or -f
Well you must have understood it by having a glance at the above code snippet, it is used to tell the name of the Dockerfile while building an image
docker image build -file <Dockerfile>
To understand this command, you’ll first need to understand what an intermediate container is.
So, whenever a Dockerfile is being executed and a new image is being made, docker uses a layered approach.
What this does is that, whenever a RUN, etc command is executed, the pre-existing image is now a container with a top level R/W image.
This R/W layer is then packed into others to form a read only set of layers forming an image.
Further instructions are run on this image, forming another layer and thus container, to be packed again to form an image.
This process leads to a set of intermediate image and intermediate containers created in the process.
When you want to delete these intermediate containers, you use this flag
docker image build --rm -f <Dockerfile>
--quiet or -q
Building an image is a noisy process that often has a lot of lines printed out on the terminal.
If you want to keep it neat and tidy and don’t want any output on the screen, use -q.
docker image build --quiet -f <Dockerfile>
-- tag or -t
It must have happened that whenever you would have pulled an image, it states that no tag mentioned, and it pulls the image with the tag latest.
Well tags are used to simply tag the images to differentiate between different versions, environments, etc.
docker image build -t <name>:<tag> -f <Dockerfile>
Docker images contain in them alot of information like
- a list of all the layers
- Config details
and much more.
To get a list of all the details about an image, we simply use
docker image inspect <id or name of image>
docker image ls
is again a command that you’ll use a lot.
In general it is used to list all the images. By default this does not include intermediate images.
But then, it again comes with a lot of flags.
-a or --all
This flag is used when you want to see all the images, including the intermediate images
docker image ls --all
digests flag is used to list all the images and add one more column to the output, the sha digest of the image
docker image ls --digests
This command enables you to remove the dangling images. It often leads to a confirmation.
Dangling images are those images which are simply staying and occupying space without any specific purpose and you won’t regret removing them. (In most cases)
docker image prune
Although you can use -f or --force flag to specify that you don’t want to be prompted.
docker image prune --force
Now is the time to let you know about registry.
Registries are nothing but docker objects that store docker images. The most popular and commonly used is dockerhub.
So whenever you need to pull an image from a registry, you use the pull command.
docker image pull <image name:<tag>>
By default it pulls the one with the latest tag, if not explicitly mentioned.
But if you want to download all the tagged images, you simply use -a or --all-tags flag
docker image pull -a <image name>
It’s just the opposite of pull and simply pushes the image to the registry.
docker image push <name:<tag>>
Now that you know how to add images, you should also know how to remove them. It’s through rm command.
docker image rm <image name or id>
That are some of the most commonly used docker image commands that you will use.