Docker Images

A Docker image is a packaged, standalone, and executable template used to create containers. It is often described as a read-only template because its core structure does not change after it is built. The template contains application code, runtime, libraries, configuration files, and environment variables needed to run a service. In practice, an image answers the question: what should this software require to start and operate?

It is important to distinguish an image from a container. A container is a runnable instance of an image. Users can start, stop, and remove containers, but the image itself remains as a reusable definition.

Layered Storage and Image Design

Docker images use a layered filesystem. Each layer records a change, such as adding a file, installing a package, or modifying settings. Layers are stacked, and later layers build on earlier ones. This approach creates several benefits. Images can share common base layers, which saves storage. Pulling an image transfers only missing layers, which speeds up distribution. Build caching can reuse unchanged layers, improving efficiency.

Anatomy of an Image

A typical image has base, intermediate, and metadata components. The base image provides an underlying operating system, commonly Alpine, Ubuntu, or Debian. Intermediate layers are produced by build instructions. Metadata stores configuration details, including the default command, entrypoint, environment variables, and exposed ports.

Managing and Building Images

Developers use command-line tools to manage images. They can search registries, pull images, list local images, and remove unused ones. For instance, a command such as docker pull nginx:latest fetches an image, while docker images displays local images and their details.

To create a custom image, developers often write a Dockerfile. The file uses instructions such as FROM, WORKDIR, COPY, RUN, EXPOSE, and CMD. A simple Node.js example might use FROM node:18-alpine, set the working directory, copy dependency files, install packages, copy source code, expose a port, and set the start command.

After writing the file, a build command such as docker build -t my-node-app:1.0 . creates the image. The tag helps organize and identify versions.

Distribution and Best Practices

Once built, an image can be pushed to a registry for sharing or deployment.