Managing files in your image

Introduction to Docker

Tim Sangster

Software Engineer @ DataCamp

COPYing files into an image

The COPY instruction copies files from our local machine into the image we're building:

COPY <src-path-on-host> <dest-path-on-image>
COPY /projects/pipeline_v3/pipeline.py /app/pipeline.py
docker build -t pipeline:v3 .
...
[4/4] COPY ./projects/pipeline_v3/pipeline.py /app/pipeline.py

If the destination path does not have a filename, the original filename is used:

COPY /projects/pipeline_v3/pipeline.py /app/
Introduction to Docker

COPYing folders

Not specifying a filename in the src-path will copy all the file contents.

COPY <src-folder> <dest-folder>
COPY /projects/pipeline_v3/ /app/

COPY /projects/pipeline_v3/ /app/ will copy everything under pipeline_v3/:

/projects/
    pipeline_v3/
        pipeline.py
        requirements.txt
        tests/
            test_pipeline.py
Introduction to Docker

Copy files from a parent directory

/init.py
/projects/
    Dockerfile
    pipeline_v3/
        pipeline.py

If our current working directory is in the projects/ folder.

We can't copy init.py into an image.

docker build -t pipeline:v3 .
 => ERROR [4/4] COPY ../init.py /     0.0s
failed to compute cache key: "../init.py" not found: not found
Introduction to Docker

Downloading files

Instead of copying files from a local directory, files are often downloaded in the image build:

  • Download a file

RUN curl <file-url> -o <destination>

  • Unzip the file

RUN unzip <dest-folder>/<filename>.zip

  • Remove the original zip file

RUN rm <copy_directory>/<filename>.zip

Introduction to Docker

Downloading files efficiently

  • Each instruction that downloads files adds to the total size of the image.
  • Even if the files are later deleted.
  • The solution is to download, unpack and remove files in a single instruction.
RUN curl <file_download_url> -o <destination_directory>/<filename>.zip \
&& unzip <destination_directory>/<filename>.zip -d <unzipped-directory> \
&& rm <destination_directory>/<filename>.zip
Introduction to Docker

Summary

Usage Dockerfile Instruction
Copy files from host to the image COPY <src-path-on-host> <dest-path-on-image>
Copy a folder from host to the image COPY <src-folder> <dest-folder>
We can't copy from a parent directory where we build a Dockerfile COPY ../<file-in-parent-directory> /

Keep images small by downloading, unzipping, and cleaning up in a single RUN instruction:

RUN curl <file_download_url> -o <destination_directory> \
&& unzip <destination_directory>/<filename>.zip -d <unzipped-directory> \
&& rm <destination_directory>/<filename>.zip
Introduction to Docker

Let's practice!

Introduction to Docker

Preparing Video For Download...