Home > Enterprise >  Dockerfile: COPY/ADD and RUN with rm in a single layer
Dockerfile: COPY/ADD and RUN with rm in a single layer

Time:03-16

I'd like to execute a COPY/ADD command and a subsequent RUN in a single layer, so that the added file doesn't bloat the final image size. Something in the lines of:

WORKDIR /opt/install

# START I want this in a single layer
COPY install.sh /opt/install
RUN ./install.sh && rm install.sh
# END

I know I may do it with a --squash argument upon build, but I'd like to know if there is any way to do it directly in the Dockerfile.

CodePudding user response:

You can use buildkit's --mount option to mount your install.sh from the build context:

# syntax=docker/dockerfile:1
# ...
WORKDIR /opt/install
RUN --mount=type=bind,target=/opt/install/install.sh,source=install.sh ./install.sh

For more on buildkit's Dockerfile features, see https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md

  • Related