0

I have already created an image locally and it contains two layers

$ docker images inspect existingimagename

"RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f",
                "sha256:3ff73e68714cf1e9ba79b30389f4085b6e31b7a497f986c7d758be51595364de"
            ]
        },

Now i am building another image and want to save space. The first layer of the previous image is the main file system. So i decided to use it

FROM  sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f
ENV LANG=en_US.UTF-8
CMD ["/usr/bin/bash"]

Then i try to build the new image

$ docker build -t newimage -f Dockerfile .
Sending build context to Docker daemon  443.5MB
Step 1/3 : FROM sha256:e21695bdc8e8432b1a119d610d5f8497e2509a7d040ad778b684bbccd067099f
pull access denied for sha256, repository does not exist or may require 'docker login'

it gives error.

So how to deal with this.

2
  • 1
    ADD command has different purpose, see docs.docker.com/engine/reference/builder/#add for more details. For referencing layers you have to use FROM instruction. Can you add Dockerfile of existingimagename and describe what exactly do you want to reuse? Commented Mar 3, 2019 at 16:56
  • I modified the question. Added the sha256 to FROM Commented Mar 3, 2019 at 17:21

1 Answer 1

1

An easy way to profit from image layer cache is to create a base image with just the first layer.

Then use FROM <base image> in your other Dockerfiles.

This way, disk space will be spared as multiple images will share the same layer and also builds will be faster.

Dockerfile-base:

FROM scratch
ADD ./system.tar.gz /
docker build -f Dockerfile-base -t base .

Dockerfile-1:

FROM base
COPY ./somefiles /
docker build -f Dockerfile-1 -t image1 .

Dockerfile-2:

FROM base
COPY ./otherfiles /
docker build -f Dockerfile-2 -t image2 .

Recommended reads

Best practices for writing Dockerfiles § Leverage build cache

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thats the only way i think. But i have already created an image. So for this purpose i have to again create any image. If i were pulling from repo then its no extra space is used. Here i have to create an first layered fresh again
If you take the exact first lines of your first Dockerfile and just remove lines from the bottom, then when building that image docker will be clever and reuse already existing layers. It should go fast. Just keep the top of the Dockerfile identicals

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.