Docker Image Fundamentals
What are Docker Images?
Docker images are lightweight, standalone, executable packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings. They serve as the fundamental building blocks of container technology, enabling consistent and portable software deployment across different computing environments.
Key Components of Docker Images
graph TD
A[Docker Image] --> B[Base Layer]
A --> C[Application Layer]
A --> D[Configuration Layer]
B --> E[Operating System]
B --> F[System Libraries]
C --> G[Application Code]
C --> H[Dependencies]
D --> I[Environment Variables]
D --> J[Startup Commands]
Image Structure and Layers
Layer Type |
Description |
Example |
Base Layer |
Foundational operating system |
Ubuntu 22.04 |
Intermediate Layers |
System dependencies |
Python runtime |
Application Layer |
Source code and application files |
Web application |
Configuration Layer |
Runtime settings |
Port mappings |
Creating a Docker Image: Practical Example
## Create a project directory
mkdir my-docker-app
cd my-docker-app
## Create a simple Python application
echo "print('Hello, Docker!')" > app.py
## Create Dockerfile
cat > Dockerfile << EOL
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3
COPY app.py /app/app.py
WORKDIR /app
CMD ["python3", "app.py"]
EOL
## Build Docker image
docker build -t my-python-app .
## Run the container
docker run my-python-app
This example demonstrates creating a Docker image with a minimal Ubuntu base, installing Python, and running a simple Python script. The Dockerfile defines each layer of the image, ensuring reproducibility and consistency across different environments.
Image Characteristics
Docker images are immutable, meaning once created, they remain unchanged. Each image consists of multiple read-only layers that can be shared across different images, promoting efficiency in storage and download times.