Docker Images Essentials
What are Docker Images?
Docker images are read-only templates used to create containers. They contain pre-configured software environments, application code, dependencies, and runtime settings. Docker images serve as blueprints for deploying consistent and reproducible application environments across different systems.
Key Components of Docker Images
graph TD
A[Docker Image] --> B[Base Layer]
A --> C[Application Layer]
A --> D[Configuration Layer]
Component |
Description |
Example |
Base Layer |
Fundamental operating system |
Ubuntu 22.04 |
Application Layer |
Software and dependencies |
Python 3.9, nginx |
Configuration Layer |
Runtime settings |
Environment variables |
Creating a Basic Docker Image
Here's an example of creating a simple Python web application image:
## Create project directory
mkdir docker-demo
cd docker-demo
## Create Dockerfile
touch Dockerfile
## Dockerfile content
cat > Dockerfile << EOL
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
WORKDIR /app
COPY app.py .
RUN pip3 install flask
EXPOSE 5000
CMD ["python3", "app.py"]
EOL
## Create sample Flask application
cat > app.py << EOL
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Docker Image Example"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
EOL
## Build Docker image
docker build -t python-web-app .
Image Layers and Storage
Docker images are composed of multiple read-only layers. Each instruction in the Dockerfile creates a new layer, enabling efficient storage and quick image creation. When an image is built, Docker caches these layers to optimize build times and reduce disk space usage.
Image Identification
Docker images are uniquely identified by:
- Repository name
- Tag
- Image ID
Example: ubuntu:22.04
or python-web-app:latest