How to set up the same Docker cache behavior during build on Ubuntu as it is on Windows?

  build, caching, docker, linux, windows

We have such Dockerfile:

FROM alpine:latest as step1
RUN echo "STEP1"

FROM alpine:latest as step2
RUN echo "STEP2"

FROM step1 as step3
RUN echo "STEP3"

On Windows (or on WSL2) we have such results for building multi-stage images.

Building all steps on Windows:

$ docker build .
[+] Building 0.2s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 32B                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                            0.1s
 => => transferring context: 2B                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                             0.0s
 => [step1 1/2] FROM docker.io/library/alpine:latest                                                                                                                         0.0s
 => CACHED [step1 2/2] RUN echo "STEP1"                                                                                                                                      0.0s
 => CACHED [step3 1/1] RUN echo "STEP3"                                                                                                                                      0.0s
 => exporting to image                                                                                                                                                       0.1s
 => => exporting layers                                                                                                                                                      0.0s
 => => writing image sha256:38ba33425558f5373099ad59aa187456c38b3f5eab14a60fdbb3cb7b1c484392                                                                                 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

Building Step 2 on Windows:

$ docker build --target step2 .
[+] Building 0.5s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 32B                                                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                             0.0s
 => CACHED [step2 1/2] FROM docker.io/library/alpine:latest                                                                                                                  0.0s
 => [step2 2/2] RUN echo "STEP2"                                                                                                                                             0.3s
 => exporting to image                                                                                                                                                       0.0s
 => => exporting layers                                                                                                                                                      0.0s
 => => writing image sha256:855f35039a5a902e1d33111fc781f748955f8b3cad7fb56d6eb420fc5d2fa55b                                                                                 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

Docker version on Windows:

$ docker version
Client:
 Cloud integration: 1.0.17
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.16.4
 Git commit:        f0df350
 Built:             Wed Jun  2 12:00:56 2021
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:58 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

While on Ubuntu we have slightly different results.

Building all steps on Ubuntu:

$ docker build .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM alpine:latest as step1
 ---> d4ff818577bc
Step 2/6 : RUN echo "STEP1"
 ---> Using cache
 ---> ca7b2711269c
Step 3/6 : FROM alpine:latest as step2
 ---> d4ff818577bc
Step 4/6 : RUN echo "STEP2"
 ---> Using cache
 ---> 50668d3ea1c2
Step 5/6 : FROM step1 as step3
 ---> ca7b2711269c
Step 6/6 : RUN echo "STEP3"
 ---> Using cache
 ---> f529239cd29a
Successfully built f529239cd29a

Building Step 2 on Ubuntu:

$ docker build --target step2 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM alpine:latest as step1
 ---> d4ff818577bc
Step 2/4 : RUN echo "STEP1"
 ---> Using cache
 ---> ca7b2711269c
Step 3/4 : FROM alpine:latest as step2
 ---> d4ff818577bc
Step 4/4 : RUN echo "STEP2"
 ---> Using cache
 ---> 50668d3ea1c2
Successfully built 50668d3ea1c2

Docker version on Ubuntu:

$ docker version
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:38 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:50 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

As you can see Docker Desktop (Windows) skips unneeded stages while Docker on Ubuntu builds them. The Docker Engine versions are the same.

For complex multi-stage images, the caching behavior the Docker on Windows can save a lot of build time comparing to the one on Ubuntu.

How to make Docker on Ubuntu behave the same way with cache as on Windows?

Source: Docker Questions

LEAVE A COMMENT