Docker Community Forums

Share and learn in the Docker community.

Using multi-stage builds to reduce image size

Hi, I’m trying to figure out how to use multi-stage builds to accomplish the following:

  1. Download a .deb package
  2. Install the .deb
  3. Delete the .deb

However, since the package is rather large, I would like it if the final image size does not include the .deb that was downloaded. Without multi-stage builds I would do something like

RUN \
  curl -o /tmp/pkg.deb "$URI" && \
  dpkg -i /tmp/pkg.deb && \
  rm /tmp/pkg.deb

I believe with a multi-stage build, I could create a smaller final image, while still leaving things more flexible than the above approach. So my first thought was:

FROM debian AS download
RUN curl -o /pkg.deb "$URI"
RUN dpkg -i /pkg.deb

FROM debian
COPY --from=download ???

But what would I copy? The whole root, except for /tmp? Really, what I would love to be able to do is mount one image into another, so I could do something like

FROM debian AS download
RUN curl -o /pkg.deb "$URI"

FROM debian
RUN mount ? /mnt
RUN dpkg -i /mnt/pkg.deb
RUN umount /mnt

Anyway, I guess my question is, is there any way to use multi-stage builds to copy a file from a previous image, run a command, and then delete it (or have an equivalent effect, such as mounting a previous image)?