Introducción a Docker
Tim Sangster
Software Engineer @ DataCamp
Descargar y descomprimir un archivo con instrucciones de Docker.
RUN curl http://example.com/example_folder.zip
RUN unzip example_folder.zip
Cambia el sistema de archivos y añade:
/example_folder.zip
/example_folder/
example_file1
example_file2
Estos cambios son los que se guardan en la imagen.
Cada instrucción del Dockerfile queda vinculada a los cambios que hizo en el sistema de archivos de la imagen.
FROM docker.io/library/ubuntu
=> Nos da un sistema de archivos inicial con todo lo necesario para ejecutar Ubuntu
COPY /pipeline/ /pipeline/
=> Crea la carpeta /pipeline/
=> Copia varios archivos en la carpeta /pipeline/
RUN apt-get install -y python3
=> Añade python3 a /var/lib/
--> Imagen de Docker: todos los cambios en el sistema de archivos por todas las instrucciones del Dockerfile.
Al compilar un Dockerfile, Docker muestra en qué capa está trabajando:
=> [1/3] FROM docker.io/library/ubuntu
=> [2/3] RUN apt-get update
=> [3/3] RUN apt-get install -y python3
Las compilaciones consecutivas son mucho más rápidas porque Docker reutiliza capas que no han cambiado.
Volver a compilar:
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
Volver a compilar con cambios:
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> [3/3] RUN apt-get install -y R
Saber cuándo hay capas en caché ayuda a entender por qué a veces las imágenes no cambian tras recompilar.
=> [1/3] FROM docker.io/library/ubuntu
=> CACHED [2/3] RUN apt-get update
=> CACHED [3/3] RUN apt-get install -y python3
Nos ayuda a escribir Dockerfiles que compilan más rápido porque no hace falta reconstruir todas las capas.
En el siguiente Dockerfile, se deben rehacer todas las instrucciones si cambia el archivo pipeline.py:
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
Nos ayuda a escribir Dockerfiles que compilan más rápido porque no hace falta reconstruir todas las capas.
En el siguiente Dockerfile, solo habrá que volver a ejecutar la instrucción COPY.
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
Introducción a Docker