3

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

  • Build image (1)
  • Push image to registry (1)
  • Pull image from registry (2)
  • Build same docker image and reuse layers from pulled image (2)

Therefore,

Machine 1:

I build this following image:

FROM node:13-slim

COPY package.json package.json
  • Build this image with the following command: docker build . -t <registry>/test-docker-image:latest
  • Push the image to a registry: docker push <registry>/test-docker-image:latest

Machine 2

  • Pull the image from the registry: docker pull <registry>/test-docker-image:latest

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

1 Answers1

4

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/

BMitch
  • 231,797
  • 42
  • 475
  • 450