Docker Community Forums

Share and learn in the Docker community.

Image cache on multi-stage builds


(Andy Neff) #1

I’m experimenting with the docker 17.05.0-ce-rc1 and trying out multi stage builds.

One thing I’ve noticed is that the previous stages become dangling, and thus docker image prune will remove those build steps. I understand sometimes this may be desired if you are trying to clean up much needed disk space, and don’t plan on rebuilding that image anytime soon… However I’d like to keep that cache around. Any idea how I can (easily) make it not dangling? Right now I can only think of a manual process where I try to determine the sha of a stage and tag it. But I do not think that a good solution of many reasons.

For example

FROM alpine:3.5 AS stage1

RUN apk --no-cache add python openssl

RUN apk --no-cache add py-virtualenv gcc python-dev openssl-dev libffi-dev musl-dev

RUN virtualenv /opt/certbot && \
    . /opt/certbot/bin/activate && \
    pip install certbot

FROM alpine:3.5

RUN apk --no-cache add python openssl

COPY --from=stage1 /opt/certbot /opt/certbot

The last two command in stage1 will be marked as dangling.


(Gryff) #2

Late reply but I’ve just encountered this error and here’s my workaround which I think isn’t too much work.

I make a separate image with --target stage1, e.g. docker build -t my-dev-image --target stage1 .

Then the image is not dangling. And docker building without the target specified still uses the cache.