14/03/2024

0. Le cache de construction

Lu 1441 fois Licence Creative Commons

Construction d'une image mon-ubuntu à partir du Dockerfile suivant, installant le paquet nécessaire pour la commande ping:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y \
	iputils-ping

WORKDIR /home

Construction:

docker build -t mon-ubuntu .
[output] 
[output] Sending build context to Docker daemon  156.7kB
[output] Step 1/3 : FROM ubuntu:latest
[output]  ---> 549b9b86cb8d
[output] Step 2/3 : RUN apt-get update && apt-get install -y 	iputils-ping
[output]  ---> Running in 88ae3721feda
[output] [...]
[output] Removing intermediate container 88ae3721feda
[output]  ---> 5eda182ce772
[output] Step 3/3 : WORKDIR /home
[output]  ---> Running in 7281081197ee
[output] Removing intermediate container 7281081197ee
[output]  ---> 6e12eb99a261
[output] Successfully built 6e12eb99a261
[output] Successfully tagged mon-ubuntu:latest

Le cache de construction

Chaque instruction d'un Dockerfile crée une couche (une "image intermédiaire") qui est gardée en cache par Docker. Son utilisation permet de gagner du temps dans la construction d'images.


Si les instructions n'ont pas changé, Docker utilise le cache:

docker build -t mon-ubuntu .
[output] 
[output] Sending build context to Docker daemon  156.7kB
[output] Step 1/3 : FROM ubuntu:latest
[output]  ---> 549b9b86cb8d
[output] Step 2/3 : RUN apt-get update && apt-get install -y 	iputils-ping
[output]  ---> Using cache
[output]  ---> 5eda182ce772
[output] Step 3/3 : WORKDIR /home
[output]  ---> Using cache
[output]  ---> 6e12eb99a261
[output] Successfully built 6e12eb99a261
[output] Successfully tagged mon-ubuntu:latest

Au lieu d'utiliser des conteneurs intermédiaires (Running in 88ae3721feda), le cache est utilisé (Using cache).


Invalidation du cache

Modifier le Dockerfile précédent pour installer l'éditeur de texte nano:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y \
	iputils-ping \
	nano

WORKDIR /home

Si une instruction change, le cache sera invalidé pour celle-ci et les suivantes:

docker build -t mon-ubuntu .
[output] 
[output] Sending build context to Docker daemon  156.7kB
[output] Step 1/3 : FROM ubuntu:latest
[output]  ---> 549b9b86cb8d
[output] Step 2/3 : RUN apt-get update && apt-get install -y 	iputils-ping 	nano
[output]  ---> Running in 037fa9500ebd
[output] [...]
[output] Removing intermediate container 037fa9500ebd
[output]  ---> 4a737ea2cd3b
[output] Step 3/3 : WORKDIR /home
[output]  ---> Running in 8e31758cd996
[output] Removing intermediate container 8e31758cd996
[output]  ---> 8103ddaca8a2
[output] Successfully built 8103ddaca8a2
[output] Successfully tagged mon-ubuntu:latest


La 2e étape a été modifiée, le cache n'est donc pas utilisé pour les étapes 2 & 3.


Ordre des instructions

Pour éviter d'invalider le cache inutilement, il est préférable de positionner les instructions peu susceptibles de changer au début du Dockerfile. Souvent, les instructions RUN sont les plus modifiées et donc à positionner vers la fin lorsque c'est possible.


Réorganisation du Dockerfile précédent:

FROM ubuntu:latest

WORKDIR /home

RUN apt-get update && apt-get install -y \
	iputils-ping \
	nano

En cas d'ajout de commandes dans la dernière instruction RUN, le cache est utilisé pour les étapes précédentes: