Docker Image Fundamentals
What is a Docker Image?
A Docker image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries, and settings. It serves as a blueprint for creating Docker containers.
Key Components of Docker Images
Image Layers
Docker images are composed of multiple read-only layers that are stacked on top of each other. Each layer represents a set of filesystem changes:
graph TD
A[Base Layer: Ubuntu] --> B[Install Python]
B --> C[Copy Application Code]
C --> D[Set Entrypoint]
Image Anatomy
A typical Docker image consists of several important elements:
Component |
Description |
Example |
Base Image |
Foundation layer |
Ubuntu, Alpine Linux |
Metadata |
Image configuration |
Exposed ports, environment variables |
Filesystem Layers |
Incremental changes |
Package installations, file modifications |
Entrypoint |
Default command to run |
Python script, web server |
Creating Docker Images
Dockerfile Basics
A Dockerfile is a text document containing instructions to build a Docker image:
## Example Dockerfile for a Python application
FROM ubuntu:22.04
## Set working directory
WORKDIR /app
## Install dependencies
RUN apt-get update && \
apt-get install -y python3 python3-pip
## Copy application files
COPY . /app
## Install Python dependencies
RUN pip3 install -r requirements.txt
## Set default command
CMD ["python3", "app.py"]
Building an Image
To build a Docker image from a Dockerfile:
## Build command
docker build -t myapp:latest .
## Verify image creation
docker images
Image Management
Pulling Images
Retrieve images from Docker Hub or private registries:
## Pull an official Ubuntu image
docker pull ubuntu:22.04
## Pull a specific version
docker pull python:3.9-slim
Image Storage
Docker stores images in a local registry, typically located at /var/lib/docker/images
on Ubuntu systems.
Best Practices
- Use minimal base images
- Minimize layer count
- Leverage build cache
- Avoid installing unnecessary packages
- Use multi-stage builds for optimization
LabEx Learning Tip
Explore Docker image fundamentals through hands-on labs in the LabEx platform to gain practical experience with container technologies.