Skip to main content

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​

AspectImageContainer
NatureStatic templateRunning instance
StateRead-onlyRead/write
StorageShared layersOwn writable layer
LifetimePermanentEphemeral

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​

TypeExampleDescription
OfficialnginxMaintained by Docker
Verifiedbitnami/nginxVerified publisher
Communityuser/imageCreated 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​

TagDescription
latestLatest version (default)
1.25, 1.25.3Semantic version
alpineBased on Alpine Linux (lightweight)
slimSlimmed-down version
bullseye, bookwormDebian 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​

ArchitectureDescriptionExamples
amd64Intel/AMD 64-bitPC, Servers
arm64ARM 64-bitMac M1/M2, Raspberry Pi 4
arm/v7ARM 32-bitRaspberry 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​

CommandDescription
docker pullDownload an image
docker imagesList the images
docker image inspectInspect an image
docker historyView the layers
docker rmiDelete an image
docker tagCreate a new tag
docker pushPush to a registry
docker image pruneClean 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​

  1. Download the python:3.11-alpine image and inspect it
  2. List all the layers of the nginx image
  3. Create a new tag for an existing image
  4. Calculate the disk space used by Docker

← Installation | Docker Containers β†’