Introduction to Docker Images
Docker Images consist of one or more read-only layers that include our application code, libraries, and dependencies needed to run the application. In other words, Images are immutable files that mean we cannot make changes to the existing image directly, either we need to re-write the Dockerfile and re-build the Docker Image with new Dockerfile or run a container using the image and then make changes inside the container and commit the running container as a Docker Image. There are official Images available for popular OS and software such as Ubuntu, CentOS, nginx, nodejs, etc. on hub.docker.com which is a public repository for Docker Images provided by Docker. Anyone can pull Docker image from it, however, you must have an account on hub.docker.com, in order to push images to hub.docker.com.
How do Images Work in Docker?
As mentioned above, Image consists of multiple layers, let’s understand it with an example. We want to create our own nginx Docker Image. It will consist of the below layers: –
- Base OS image(for ex. Ubuntu)
- Update the base image
- Install nginx on Ubuntu
- Configure CMD or entrypoint to run nginx when any container gets created with this image.
docker build -t <name of the docker image>:<tag> <path of the Dockerfile>
docker build -t my_nginx .
Here is the snippet of Dockerfile to create the above image: –
The above command will build a new Image with name my_nginx and daemon consider ‘latest’ tag as it is not specified and check the current working directory for Dockerfile.
- It will download ubuntu image first and run an intermediate container using ubuntu Image
- Update the ubuntu image and commit a new intermediate Image and remove the intermediate container.
- Run a new intermediate container using Image committed in step 2.
- Same process will continue till the last line of the Dockerfile.
- Last committed Image will be the final Image and daemon will give it a name and tag.
Below is the syntax to manage image: –
docker image <command>
snapshot of all commands that we can use with Docker Image: –
How to Display Docker Images?
Commands to display images those are available on host locally, all below commands will give us the same output: –
docker image ls
docker image list
There are multiple options that we can use with the above command. For example, if we want to list all images including intermediate images, we need to use -a option with the above commands: –
Here is a snippet of all options available to display Images: –
How to Download Docker Images?
We use pull command to download Images locally.
docker pull <image_name>
docker pull ubuntu
The above command will pull ubuntu Image from hub.docker.com to localhost. It will pull the image that has the latest tag as shown in the below snapshot: –
If we have to download a specific tag or version of any Image, we need to specify the tag while pulling the Image.
docker pull <image_name>:tag
docker pull ubuntu:xenial
Above example will download ubuntu image which has xenial tag with it as shown in below snapshot: –
If we have private registry then we need to give the registry name most probably hostname on which registry container is running. Let’s understand it with an example.
1. First, we need to create a private registry using below command: –
docker run -d -p 5000:5000 --restart always --name registry registry:2
Explanation: – above command creates a container with image registry and expose it to port 5000 on the host as well as inside the container and set the restart option to always that means daemon will restart the container every time if the container is not running.
2. Retag existing Docker image with localhost:5000 as below: –
docker image tag ubuntu localhost:5000/ubuntu
Explanation: – above command retags the exiting ubuntu image with localhost:5000/ubuntu as we have exposed the container to localhost on 5000 port.
Note: – Docker image name must start with localhost:5000 and after / we can name it as we want, here we keep ubuntu itself.
3. Let’s push the image to our private registry: –
docker push localhost:5000/ubuntu
Explanation: – above command pushes the newly tagged Docker image to our private registry that is running as a container.
4. Now we remove the Docker image available locally: –
docker image rm localhost:5000/ubuntu
Explanation: – above command will untag the ubuntu Images that we have just tagged in above step however we have already pushed this image to our private registry so we can run a container using ‘localhost:5000/ubuntu’.
5. Run a container using ‘localhost:5000/ubuntu’ as shown below: –
docker run -d localhost:5000/ubuntu sleep 3000
Explanation: – In the above snapshot, you can see daemon is pulling the image from ubuntu instead of library/ubuntu. Below are the containers that are running currently. We can see one of them is running using ‘localhost:5000/ubuntu’ image.
How to Remove Docker Images?
We can remove any Docker Images using ‘rm’ command if it is not referenced by any container however we can force remove the Images even if it referenced by any container.
docker image rm <image_name>
docker image rm <image_id>
docker image rm alpine
docker image rm 82f67be598eb
Explanation: – In the above snapshot, the alpine Docker image has been removed with the latest tag. Remember, if we don’t specify the tag, it will take the latest tag. In below snapshot, we have alpine image with tag 3.9 if we try to remove it with command ‘docker image rm alpine’, it will throw an error ‘No such image: alpine’, either we need to specify the tag or remove using Image ID as below: –
docker image rm alpine:3.9
docker image rm 82f67be598eb
As mentioned above, we can remove the Images even if it is referenced by any container that might be in running or exited state. We have to use -f to forcefully remove any Docker Image as below: –
docker image rm -f <image_name>or<image_id>
docker image rm -f ubuntu
In the above snapshot, we can see, there is a container referencing ubuntu image and if we try to simply remove the image, the daemon is complaining about conflict however in the next command using -f option able to remove the ubuntu Image.
1. Portability – Images are portable as it includes all the required dependencies and libraries to the application, so it runs exact same way on any platform like Linux, Windows, Cloud and even Raspberry Pi.
2. Lightweight – It has a multi-layer file system that makes images extremely lightweight because each layer includes only differences from other layers.
3. Consistency – Images have powerful characteristics of inability to change that’s called immutability. It is very useful while testing new features of any application as it is not going to break our existing image. If something goes wrong we can run the application again using the existing image as that is unchanged. Immutability also provides the consistency that means if the application is running as expected in the test environment then it is going to run in the same manner in other environments as well.
4. Sharing – We can store Images in a repository. We have public repository hub.docker.com provided by Docker itself however we can create our own repository on hub.docker.com and keep our Images privately if we want. We can create our own private registry as well. It helps us to share images more easily
5. Secure – Docker Images are secure. Every Image has its hash value. We can also digitally sign the Images so if someone tries to run a container using an unsigned image, daemon is not going to start the container.
Docker Images are the core object of Docker. We cannot imagine Docker without images. It is the initial thing that we need to work with Docker after installing it. Images are like template or snapshot of a VM but not limited to as it has more advantages over template or snapshot.
This is a guide to Docker Images. Here we discuss How do Images Work in Docker along with How to Display, Download and Remove images. You may also have a look at the following articles to learn more –