1. Managing files in your image
We've seen the basics of building images, now we'll learn how to add files to our images.
2. COPYing files into an image
The RUN instruction allowed us to execute bash commands to create an image, but we can't use it to move files from our local file system onto the image we're building.
To copy local files to our image we use the COPY instruction. The COPY instruction needs two parameters: first, we pass to it the path of the file we want to copy, including the name of the file we want to copy. The second parameter is the destination path inside the image.
We can choose whether to end the destination path with a filename. If we do not pass a filename, the file will get its original name.
3. COPYing folders
If we don't specify a filename in the source path, then instead of just a single file, the entire contents of the folder will be copied, including sub-folders.
For example, if we have a folder called pipeline underscore v3 with two files and a sub-folder with one file, we can copy both files and the subfolder with its file using the COPY instruction ending in pipeline v3 slash.
4. Copy files from a parent directory
It is not possible to copy files from a parent directory when building a Dockerfile.
For example, let's say we are in the projects folder when we run docker build. A COPY instruction in the Dockerfile that tries to copy the init dot py file from the parent directory of the current directory into the image will fail with the not found message we can see on the slide.
5. Downloading files
Another common way to include files in an image is to download them during the image build. While there is an instruction that allows us to do this, the ADD instruction, best practice is to use several RUN instructions and bash commands to download and unzip files.
First, use curl to download a file to a local directory. Then unzip it using the unzip command if it is an archive. Finally, once we don't need the zip file anymore, we can remove it with the rm command.
6. Downloading files efficiently
Any instruction in a Dockerfile that downloads files will add to the size of the image. Even if the files are removed in a later instruction.
To ensure images don't become unnecessarily big, we should download, unzip and remove the original file in a single RUN instruction.
This can be done by chaining the commands using a backslash and double ampersand. The backslash makes it so bash commands can span multiple lines allowing us to keep our Dockerfile readable. The double ampersand tells the shell to execute the commands one after the other. Combining them allows us to create a single RUN instruction that is still easy to read over multiple lines.
By using this best practice on downloading and unpacking archives, we ensure our image is as small as possible, making it easier to share and faster to run.
7. Summary
Here is a summary of the new commands and instructions you can refer back to when completing the exercises.
8. Let's practice!
Now that we know how to handle files in images let's get some practice!