Docker Image Basics
Understanding Docker Images
Docker images are fundamental components in container technology, serving as read-only templates that contain everything needed to run an application. These images include the application code, runtime, libraries, environment variables, and configuration files.
Image Structure and Layers
Docker images are constructed using a layered architecture, which enables efficient storage and transfer. Each layer represents a set of filesystem changes.
graph TD
A[Base Image Layer] --> B[Application Layer]
B --> C[Configuration Layer]
C --> D[Runtime Layer]
Key Image Components
Component |
Description |
Purpose |
Base Image |
Foundational operating system |
Provides core system libraries |
Application Files |
Source code and dependencies |
Defines application content |
Metadata |
Image configuration |
Controls container startup |
Creating a Docker Image: Practical Example
Here's a comprehensive Dockerfile demonstrating image creation on Ubuntu 22.04:
## Use official Ubuntu base image
FROM ubuntu:22.04
## Set working directory
WORKDIR /app
## Update system packages
RUN apt-get update && apt-get install -y \
python3 \
python3-pip
## Copy application files
COPY . /app
## Install dependencies
RUN pip3 install -r requirements.txt
## Define startup command
CMD ["python3", "app.py"]
Image Layer Mechanism
When building images, Docker creates intermediate layers for each instruction. This approach enables:
- Efficient storage
- Faster build times
- Simplified version management
Image Identification
Docker images are uniquely identified by:
- Repository name
- Tag
- Image ID (hash)
Example: ubuntu:22.04
represents a specific Ubuntu image version.