Docker Images
Chapter objectivesβ
- Understand what a Docker image is
- Download and manage images
- Understand tags and layers
- Use Docker Hub effectively
1 - What is a Docker image?β
Definitionβ
A Docker image is a read-only template containing:
- A filesystem (rootfs)
- The application's dependencies
- The runtime configuration
- The metadata
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β Image Docker β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Layer 4: Application code β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Layer 3: Dependencies (npm, pip...) β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Layer 2: Runtime (Node.js, Python...) β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Layer 1: Base OS (Ubuntu, Alpine...) β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Image vs Containerβ
| Aspect | Image | Container |
|---|---|---|
| Nature | Static template | Running instance |
| State | Read-only | Read/write |
| Storage | Shared layers | Own writable layer |
| Lifetime | Permanent | Ephemeral |
2 - Search for imagesβ
On Docker Hubβ
# Rechercher une image
docker search nginx
# Rechercher avec filtres
docker search --filter=stars=100 nginx
docker search --filter=is-official=true nginx
# Limiter les rΓ©sultats
docker search --limit 5 nginx
Search resultβ
NAME DESCRIPTION STARS OFFICIAL
nginx Official build of Nginx 18000 [OK]
bitnami/nginx Bitnami nginx 150
linuxserver/nginx ... 120
Image typesβ
| Type | Example | Description |
|---|---|---|
| Official | nginx | Maintained by Docker |
| Verified | bitnami/nginx | Verified publisher |
| Community | user/image | Created by the community |
Warning
Always prefer official or verified images for security.
3 - Download imagesβ
The docker pull commandβ
# Télécharger la dernière version
docker pull nginx
# TΓ©lΓ©charger une version spΓ©cifique
docker pull nginx:1.25
# Télécharger une image Alpine (légère)
docker pull nginx:alpine
# TΓ©lΓ©charger depuis un autre registre
docker pull gcr.io/google-containers/nginx
Anatomy of an image nameβ
[registre/][utilisateur/]image[:tag][@digest]
Exemples:
nginx # Docker Hub, officiel, latest
nginx:1.25 # Tag spΓ©cifique
bitnami/nginx:latest # Utilisateur/organisation
gcr.io/project/app:v1 # Registre privΓ©
nginx@sha256:abc123... # Digest (hash)
Common tagsβ
| Tag | Description |
|---|---|
latest | Latest version (default) |
1.25, 1.25.3 | Semantic version |
alpine | Based on Alpine Linux (lightweight) |
slim | Slimmed-down version |
bullseye, bookworm | Debian version |
Best practice
Avoid latest in production. Use versioned tags for reproducibility.
4 - List and inspect imagesβ
List the imagesβ
# Lister toutes les images
docker images
# OU
docker image ls
# Format personnalisΓ©
docker images --format "{{.Repository}}:{{.Tag}} - {{.Size}}"
# Filtrer par nom
docker images nginx
# Afficher les images intermΓ©diaires
docker images -a
# Afficher uniquement les IDs
docker images -q
Typical outputβ
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest a8758716bb6a 2 days ago 187MB
nginx alpine 5685937b6bc1 2 days ago 40.7MB
ubuntu 22.04 3db8720ecbf5 3 weeks ago 77.9MB
Inspect an imageβ
# Informations dΓ©taillΓ©es
docker image inspect nginx
# Extraire des informations spΓ©cifiques
docker image inspect nginx --format '{{.Config.ExposedPorts}}'
docker image inspect nginx --format '{{.Architecture}}'
docker image inspect nginx --format '{{json .Config.Env}}'
Layer historyβ
# Voir les couches d'une image
docker history nginx
# Format dΓ©taillΓ©
docker history nginx --no-trunc
5 - Understanding layersβ
Layered architectureβ
βββ ββββββββββββββββββββββββββββββββββββββββββββββββ
β Conteneur (Layer R/W) β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββββββββββββββββββββββββββββββ β
β β Layer: COPY app.js β β
β βββββββββββββββββββββββββββββββββββββββββββ€ β
β β Layer: RUN npm install β β Image
β βββββββββββββββββββββββββββββββββββββββββββ€ β (R/O)
β β Layer: WORKDIR /app β β
β βββββββββββββββββββββββββββββββββββββββββββ€ β
β β Layer: Base image (node:18-alpine) β β
β βββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Benefits of layersβ
1. Sharing between images
# Deux conteneurs basΓ©s sur la mΓͺme image partagent les couches
docker run -d nginx # TΓ©lΓ©charge les couches
docker run -d nginx # RΓ©utilise les couches (instantanΓ©)
2. Build cache
# Couches mises en cache si non modifiΓ©es
FROM node:18
WORKDIR /app
COPY package*.json ./ # Cache si inchangΓ©
RUN npm install # Cache si inchangΓ©
COPY . . # Invalide le cache si modifiΓ©
3. Optimized transfer
# Seules les couches manquantes sont tΓ©lΓ©chargΓ©es
docker pull nginx:1.25
# Already exists: sha256:abc123...
# Pull complete: sha256:def456...
6 - Manage imagesβ
Delete imagesβ
# Supprimer une image
docker rmi nginx
# OU
docker image rm nginx
# Supprimer par ID
docker rmi a8758716bb6a
# Forcer la suppression
docker rmi -f nginx
# Supprimer plusieurs images
docker rmi nginx ubuntu python
# Supprimer toutes les images non utilisΓ©es
docker image prune
# Supprimer TOUTES les images
docker image prune -a
Tag imagesβ
# CrΓ©er un nouveau tag
docker tag nginx:latest mon-nginx:v1
# Taguer pour un registre privΓ©
docker tag nginx:latest mon-registre.io/nginx:v1
# VΓ©rifier
docker images
Save and load imagesβ
# Exporter une image vers un fichier tar
docker save nginx > nginx.tar
docker save -o nginx.tar nginx:latest
# Importer une image depuis un fichier tar
docker load < nginx.tar
docker load -i nginx.tar
7 - Docker Hubβ
Log inβ
# Connexion interactive
docker login
# Connexion avec identifiants
docker login -u username -p password
# VΓ©rifier la connexion
docker info | grep Username
Push an imageβ
# 1. Taguer l'image avec votre username
docker tag mon-app:latest username/mon-app:v1
# 2. Pousser vers Docker Hub
docker push username/mon-app:v1
# 3. Pousser toutes les versions
docker push username/mon-app --all-tags
Private registriesβ
# Se connecter Γ un registre privΓ©
docker login registry.example.com
# Pousser vers un registre privΓ©
docker tag mon-app registry.example.com/mon-app:v1
docker push registry.example.com/mon-app:v1
# Tirer depuis un registre privΓ©
docker pull registry.example.com/mon-app:v1
8 - Disk space managementβ
Analyze usageβ
# Vue d'ensemble
docker system df
# Vue dΓ©taillΓ©e
docker system df -v
Typical outputβ
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 5 2.5GB 1.8GB (72%)
Containers 8 2 500MB 450MB (90%)
Local Volumes 10 3 1.2GB 800MB (66%)
Build Cache 25 0 512MB 512MB (100%)
Clean upβ
# Nettoyer les images non utilisΓ©es
docker image prune
# Nettoyer les images danglings (sans tag)
docker image prune -f
# Nettoyer TOUT (images, conteneurs, volumes, cache)
docker system prune -a --volumes
# Nettoyer les images de plus de 24h
docker image prune -a --filter "until=24h"
9 - Multi-architecture imagesβ
Understanding architecturesβ
| Architecture | Description | Examples |
|---|---|---|
amd64 | Intel/AMD 64-bit | PC, Servers |
arm64 | ARM 64-bit | Mac M1/M2, Raspberry Pi 4 |
arm/v7 | ARM 32-bit | Raspberry Pi 3 |
Download for a specific architectureβ
# TΓ©lΓ©charger pour une plateforme spΓ©cifique
docker pull --platform linux/amd64 nginx
docker pull --platform linux/arm64 nginx
# VΓ©rifier l'architecture d'une image
docker image inspect nginx --format '{{.Architecture}}'
Multi-architecture manifestsβ
# Voir les architectures disponibles
docker manifest inspect nginx
# Docker sΓ©lectionne automatiquement la bonne architecture
docker pull nginx # TΓ©lΓ©charge pour votre plateforme
Summaryβ
| Command | Description |
|---|---|
docker pull | Download an image |
docker images | List the images |
docker image inspect | Inspect an image |
docker history | View the layers |
docker rmi | Delete an image |
docker tag | Create a new tag |
docker push | Push to a registry |
docker image prune | Clean up images |
Key points
- Images are read-only templates
- Layers enable sharing and caching
- Use versioned tags in production
- Regularly clean up unused images
Hands-on exercisesβ
- Download the
python:3.11-alpineimage and inspect it - List all the layers of the nginx image
- Create a new tag for an existing image
- Calculate the disk space used by Docker