Introduction à Docker
Tim Sangster
Software Engineer @ DataCamp
Télécharger et décompresser un fichier avec des instructions Docker.
RUN curl http://example.com/example_folder.zip
RUN unzip example_folder.zip
Modifie le système de fichiers et ajoute :
/example_folder.zip
/example_folder/
example_file1
example_file2
Ce sont ces changements qui sont enregistrés dans l'image.
Chaque instruction du Dockerfile est liée aux changements qu'elle applique au système de fichiers de l'image.
FROM docker.io/library/ubuntu
=> Fournit un système de fichiers de base avec tout le nécessaire pour exécuter Ubuntu
COPY /pipeline/ /pipeline/
=> Crée le dossier /pipeline/
=> Copie plusieurs fichiers dans le dossier /pipeline/
RUN apt-get install -y python3
=> Ajoute python3 dans /var/lib/
--> Image Docker : tous les changements au système de fichiers produits par toutes les instructions du Dockerfile.
Pendant la construction d'un Dockerfile, Docker indique sur quelle couche il travaille :
=> [1/3] FROM docker.io/library/ubuntu
=> [2/3] RUN apt-get update
=> [3/3] RUN apt-get install -y python3
Les constructions successives sont beaucoup plus rapides, car Docker réutilise les couches inchangées.
Relancer une construction :
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
Relancer une construction avec des modifications :
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> [3/3] RUN apt-get install -y R
Savoir quand les couches sont en cache aide à comprendre pourquoi, parfois, les images ne changent pas après une reconstruction.
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
Aide à écrire des Dockerfiles qui se construisent plus vite, car toutes les couches n'ont pas à être reconstruites.
Dans le Dockerfile suivant, toutes les instructions doivent être reconstruites si le fichier pipeline.py change :
FROM ubuntu
COPY /app/pipeline.py /app/pipeline.py
RUN apt-get update
RUN apt-get install -y python3
=> [1/4] FROM docker.io/library/ubuntu
=> [2/4] COPY /app/pipeline.py /app/pipeline.py
=> [3/4] RUN apt-get update
=> [4/4] RUN apt-get install -y python3
Aide à écrire des Dockerfiles qui se construisent plus vite, car toutes les couches n'ont pas à être reconstruites.
Dans le Dockerfile suivant, seule l'instruction COPY devra être relancée.
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3
COPY /app/pipeline.py /app/pipeline.py
=> [1/4] FROM docker.io/library/ubuntu
=> CACHED [2/4] RUN apt-get update
=> CACHED [3/4] RUN apt-get install -y python3
=> [4/4] COPY /app/pipeline.py /app/pipeline.py
Introduction à Docker