Docker 04 - Docker Image

Let us start with the official definition of a Docker image from the Docker documentation: “A container image is a standardized package that includes all of the files, binaries, libraries, and configurations to run a container.”

According to the documentation, Docker images follow two essential principles:

  1. Immutable: Once an image is created, it does not change. This means that the image can be used to create multiple containers, and the containers will all be identical.
  2. Layered: Images are composed of layers. When you change an image, such as updating software or adding files, a new layer is created. This allows for efficient use of disk space and reduces the time to transfer images.

Docker images contain application binaries, dependencies, and metadata but do not include a full operating system. Typically, a Docker image is built from a Dockerfile, which provides instructions on how to construct the image.

How to Build a Docker Image

To build a Docker image, follow these general steps:

  1. Create a Dockerfile: This file contains the instructions for building your image.
  2. Write the instructions: Use Docker commands like FROM, RUN, COPY, and CMD to define your image.
  3. Build the image: Use the docker build command in the directory containing your Dockerfile.

Here’s a simple example:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y nginx
COPY ./my-nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

To build this image, you would run:

docker build -t my-nginx-image .

Image Layers

Docker images are constructed using a layered architecture. Each layer represents an instruction in the image’s Dockerfile. Layers are stacked sequentially, with each layer building upon the previous one.

Key points about image layers:

  • Read-only: Once a layer is created, it cannot be modified. If changes are needed, a new layer is added on top.
  • Caching: Docker caches layers to speed up subsequent builds and reduce disk usage.
  • Sharing: Layers are shared between images, saving storage space and network bandwidth when transferring images.

A Docker container consists of a single read/write layer on top of the image layers.

To view the layers of an image, use the docker image history command. For example, to see the layers of the nginx image:

docker image history nginx

This command displays the history of the image layers, including the commands used to create each layer.

IMAGE          CREATED       CREATED BY                                       SIZE      COMMENT
443d199e8bfc   3 weeks ago   CMD ["nginx" "-g" "daemon off;"]                 0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   STOPSIGNAL SIGQUIT                               0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   EXPOSE map[80/tcp:{}]                            0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   ENTRYPOINT ["/docker-entrypoint.sh"]             0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   COPY 30-tune-worker-processes.sh /docker-ent…   4.62kB    buildkit.dockerfile.v0
<missing>      3 weeks ago   COPY 20-envsubst-on-templates.sh /docker-ent…   3.02kB    buildkit.dockerfile.v0
<missing>      3 weeks ago   COPY 15-local-resolvers.envsh /docker-entryp…   336B      buildkit.dockerfile.v0
<missing>      3 weeks ago   COPY 10-listen-on-ipv6-by-default.sh /docker…   2.12kB    buildkit.dockerfile.v0
<missing>      3 weeks ago   COPY docker-entrypoint.sh / # buildkit           1.62kB    buildkit.dockerfile.v0
<missing>      3 weeks ago   RUN /bin/sh -c set -x     && groupadd --syst…   95.9MB    buildkit.dockerfile.v0
<missing>      3 weeks ago   ENV PKG_RELEASE=2~bookworm                       0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   ENV NJS_RELEASE=2~bookworm                       0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   ENV NJS_VERSION=0.8.4                            0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   ENV NGINX_VERSION=1.27.0                         0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   LABEL maintainer=NGINX Docker Maintainers <d…   0B        buildkit.dockerfile.v0
<missing>      3 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                  0B
<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:cbda549b25cd4337c…   97.1MB

To obtain detailed information about an image, including its layers, configuration, and metadata, use the docker image inspect command:

docker image inspect nginx



    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • Dependency Injection
  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory