It’s very common to copy a local file into the container when build docker image. In general, we use
COPY command. But it creates a new layer and increase the final image size. If this is a temporal file and we don’t want users waste their storage space, how can we remove it? Here are some approaches.
Download the File Dynamically Link to heading
If the file can be download from URL or you can create a local HTTP server to share the file, you can download the file, use it and delete it in one
RUN command. For example:
RUN wget xxxx && unzip xxx && rm xxx
RUN –mount Command Link to heading
You can also mount file when build image if your file can’t be download from Internet or the file is secret. Use it to bind files or directories to the build container. A bind mount is read-only by default, add
rw parameter to make it writable.
If you bind the directory, the changes in the build container does not reflect to host.
The mounted folder are kept in the image, but the files are gone. Don’t forget to delete the empty folder if you want to keep image clean.
Mount folder Link to heading
Mount file Link to heading
RUN --mount=type=bind,target=/azure-cli.rpm,source=./docker/azure-cli.rpm tdnf install ca-certificates /azure-cli.rpm -y && tdnf clean all
–squash Option Link to heading
You can also use
--squash to reduce image size.
Once the build is complete, Docker creates a new image loading the diffs from each layer into a single new layer and references all the parent’s layers. So the extra space created by
COPY command can be freed by