Introduction to Docker
Tim Sangster
Software Engineer @ DataCamp
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/
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
/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
Instead of copying files from a local directory, files are often downloaded in the image build:
RUN curl <file-url> -o <destination>
RUN unzip <dest-folder>/<filename>.zip
RUN rm <copy_directory>/<filename>.zip
RUN curl <file_download_url> -o <destination_directory>/<filename>.zip \
&& unzip <destination_directory>/<filename>.zip -d <unzipped-directory> \
&& rm <destination_directory>/<filename>.zip
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 |
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