🐳 Docker Q4 / 20

What is a Docker image?

AI-Powered Answer ✓ Answered

A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run a piece of software, including the code, a runtime, system tools, system libraries and settings. It serves as a read-only template from which Docker containers are created.

What is a Docker Image?

At its core, a Docker image is a snapshot of an application and its environment. Think of it as a blueprint for creating a Docker container. It's a static, immutable, and versioned file that bundles an application's code, a runtime (like Python or Node.js), system libraries, dependencies, and an operating system (like Alpine Linux or Ubuntu).

Images are built from a series of layers, each representing an instruction in a Dockerfile. This layered architecture makes images incredibly efficient, as layers can be shared between different images, reducing disk space usage and speeding up builds. For example, many images might share a common base layer for Ubuntu, only adding their unique application layers on top.

Once an image is built, it becomes a complete and self-contained environment. You can then use this image to launch one or more Docker containers, each a running instance of that image. The image itself never changes; instead, a new writable layer is added on top when a container is run, allowing for changes specific to that container without altering the base image.

Key Characteristics of Docker Images

  • Read-only Templates: Images are static and cannot be modified once created. Any changes to the running container are stored in a new, writable layer on top of the image.
  • Layered Architecture: Built from multiple layers, each representing an instruction in a Dockerfile. This promotes reusability, caching, and efficient distribution.
  • Immutable: An image is always the same every time it's used, ensuring consistency across different environments.
  • Portable: Can be easily shared and run on any Docker-enabled system, guaranteeing that your application will run exactly the same way everywhere.
  • Versioned: Images can be tagged with versions (e.g., myapp:1.0, myapp:latest), allowing developers to manage different releases of their application.
  • Self-contained: Includes all necessary dependencies, libraries, and configurations, eliminating 'it works on my machine' problems.

Creating and Using Docker Images

Docker images are typically built using a Dockerfile, which is a simple text file containing a set of instructions. The docker build command reads these instructions and executes them to create a new image. Once an image is created, it can be pushed to a Docker registry (like Docker Hub) for sharing, or used locally to create containers with the docker run command.

Example Dockerfile

dockerfile
FROM alpine:latest
WORKDIR /app
COPY . .
RUN echo "Hello from Docker!" > message.txt
CMD ["cat", "message.txt"]

Building an Image

bash
docker build -t my-hello-app:1.0 .

Running a Container from the Image

bash
docker run my-hello-app:1.0