Docker Image Explained

  • An image consists of a collection of files (or layers) that pack together all the necessities — such as dependencies, source code, and libraries
  • A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform.
  • A Docker Image is a set of layers where each layer represents an instruction from the Dockerfile.
  • It provides a convenient way to package up applications.
  • Each new layer is only a set of differences from the previous one.
  • Docker creates container images using layers. Each command that is found in a Dockerfile creates a new layer.
  • Each command that is found in a Dockerfile creates a new layer.
  • Docker uses a layered cache to optimize the process of building Docker images and make it faster.
  • At each occurrence of a RUN, COPY and ADD command in the Dockerfile, Docker will create and commit a new layer to the image that comprises a Docker image.

Let’s take the example of building a docker file in three states.

  • During a new build, all of these file structures have to be created and written to disk — this is where Docker stores base images.
  • When we build the same dockerfile a second time, it will reuse the cache of a previous build on the host to reduce the build time.
  • None of those file structures have to be created and written to disk at this time.
  • However, that cache may cause issues sometimes.
  • To solve this issue, we need to invalidate the cache on the docker host.
  • Docker build with the “— no-cache” option, which completely ignores all cache and thus makes every build take as much time as the first.
  • e.g — docker build — no-cache -t nginx1 .
  • When you execute docker image ls or docker images, the column size is the size of the docker image.
  • It is also not necessary that it will be the size of the current image but it may be the size of the current image + all these parent images.
  • Here is a MySQL image that has been downloaded from Docker Hub.
  • A Docker image is made up of multiple layers. The MySQL image above consists of a series of layers such as 07aded7c29c6, f68b8cbd22de, etc, and others.
  • Tag (latest) —It identifies the image by its tag, such as version number.
  • Image ID (2fe463762680) — It is a unique image identity.
  • Created (4 days ago) — It is the period of time since it was created.
  • Size (514MB) — It is the image’s virtual size.
  1. To see the list of Docker images on the system, you can issue the following command.
$ docker image ls 

2. Check the history of the docker image.

$ docker image history nginx

3. The Docker images on the system can be removed via the docker rmi command.

$ docker rmi mysql

4. This command is used to return only the Image ids of the images.

$ docker images -q

5. This command is used to see the details of an image or container.

$ docker images inspect alpine

Docker command reference

Cloud and DevOps Enthusiast