Cloud and Container Basics

FROM alpine
COPY data.tar.gz /tmp/
RUN tar -xzf /tmp/data.tar.gz -C /opt && rm /tmp/data.tar.gz

To unpack a 500 MB archive, data.tar.gz, an image was built with the Dockerfile above. Although the archive was deleted in the same RUN after unpacking, the image remained larger by the size of the archive. Which of the following is the most appropriate pair of reason and remedy?

  1. AThe rm after && is not executed during the build. Write the rm as a separate RUN instruction
  2. BDeleted files remain in the image as build cache. Build again with --no-cache added to docker build
  3. CThe archive remains in the layer created by the COPY instruction. Use a multi-stage build and copy only the unpacked files into the final image
Show answer and explanation

Answer: C. The archive remains in the layer created by the COPY instruction. Use a multi-stage build and copy only the unpacked files into the final image

Each Dockerfile instruction that changes the file system (such as COPY or RUN) becomes one layer, and an image is these layers stacked together. Deleting a file in a later layer only records a deletion marker in the upper layer, so the 500 MB archive that went into the COPY layer remains in the image. With a multi-stage build that unpacks in another stage and brings only the unpacked files into the last stage with COPY --from, the archive is not included in the final image. Downloading, unpacking and deleting within one RUN is another approach, but it cannot be used for a file brought in with COPY.

  • A. The rm after && also runs within the same RUN. Moving it to a separate RUN just adds a layer with the deletion marker and does not reduce the size.
  • B. --no-cache only stops the results of earlier builds from being reused; the layer contents are the same, so the size does not change.
  • C. Correct. The contents of a lower layer remain even after deletion, so keep the archive out of the final image.
Practice this topic Back to topic

Books on Cloud and Container Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Cloud and Container Basics questions

  1. There are five images, each of which stacks its own 30 MB layer on the same base image (120 MB). Layer compression and the size of metadata are ignored. Which of the following is the disk space used by the images when all five are pulled to one server?
  2. For the Dockerfile above, which of the following correctly pairs the command executed when the image is built with docker build and the command executed when a container is started with docker run?
  3. An image named pinger, built from the Dockerfile above, is started with docker run pinger example.com. Which of the following is the command executed in the container?
  4. A container is started with the command above from the image nginx, a web server that listens on port 80. The host's IP address is 192.0.2.10, and there are no restrictions such as a firewall. Which of the following is the URL to use to reach this web server from a browser on another PC on the same network?
  5. Dockerfile (B) above was made from Dockerfile (A), which had RUN npm ci after COPY . ., by changing the order so that only the dependency files are copied first. npm ci takes 120 seconds, npm run build 30 seconds and COPY . . 2 seconds, and the time of the other instructions is negligible. The image is rebuilt 20 times a day after changing only the source code under src (package.json and package-lock.json are unchanged). When the cache from the previous build can be used, which of the following is how much shorter B is than A in total build time per day?