dockerdocker-layer

Layers between docker builds can't be shared


I want to reuse the layers from a docker image on two different machines in the following way:

Therefore,

Machine 1:

I build this following image:

FROM node:13-slim

COPY package.json package.json

Machine 2

If I run docker build . on this machine the layers are not reused from the pulled image.

Is there a way to reuse the layers from the pulled image in the docker build?

Similar issue:

There is the following thread on GitHub that describes something similar, but this describes the issue between sharing layers between docker build and docker-compose build. https://github.com/docker/compose/issues/883


Solution

  • To trust the layers of a pulled image that wasn't built locally, you need -cache-from, e.g.:

    docker build --cache-from=<registry>/test-docker-image:latest -t newimg:latest .
    

    Docker won't trust pulled images by default to avoid a malicious image that claims to provide layers for an image you may build while actually including malicious content in that layer.

    For more details on args to docker build, see: https://docs.docker.com/engine/reference/commandline/build/