Docker Images Basics
Understanding Docker Images
Docker images are fundamental to container technology, serving as read-only templates that contain a pre-configured operating system and application environment. These images consist of multiple layers that define the complete filesystem for a container.
Image Structure and Layers
graph TD
A[Base Image] --> B[Application Layer]
A --> C[Configuration Layer]
A --> D[Dependency Layer]
Key characteristics of Docker images include:
Layer Type |
Description |
Example |
Base Image |
Foundational operating system layer |
Ubuntu 22.04 |
Dependency Layer |
Required libraries and packages |
Python runtime |
Application Layer |
Actual application code |
Web application |
Creating a Docker Image
Here's a practical example of creating a Docker image for a Python application:
## Create a new directory for the project
mkdir python-app
cd python-app
## Create Dockerfile
touch Dockerfile
## Edit Dockerfile with basic configuration
echo "FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
WORKDIR /app
COPY . /app
RUN pip3 install -r requirements.txt
CMD ['python3', 'app.py']" > Dockerfile
## Build the Docker image
docker build -t my-python-app .
This Dockerfile demonstrates key steps in image creation:
- Selecting a base image (Ubuntu 22.04)
- Installing system dependencies
- Setting working directory
- Copying application files
- Installing application requirements
- Defining the default command
Image Management Commands
## List local images
docker images
## Pull an image from Docker Hub
docker pull ubuntu:22.04
## Remove an image
docker rmi my-python-app
Docker images provide a consistent and reproducible environment across different computing platforms, enabling efficient application deployment and scaling.