docker / docker/cli

The cache does not work in a particular case

Open
#3,921 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/builder area/builder/classic-builder kind/bug version/20.10
Dominant language
Go
Stars
6.1k
Forks
2.2k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

Description

Consider a test project (in a test directory) with any source file and a .dockerignore file. The Dockerfile file is not in the test folder. Example :

$ ls -AlR
.:
total 8
-rw-rw-r-- 1 sbe sbe   29 déc.  17 14:49 Dockerfile
drwxrwxr-x 2 sbe sbe 4096 déc.  17 14:58 test

./test:
total 8
-rw-rw-r-- 1 sbe sbe 13 déc.  17 14:49 .dockerignore
-rw-rw-r-- 1 sbe sbe 21 déc.  17 14:48 the_code.md

$ cat Dockerfile 
FROM alpine:latest

COPY . .

If we build the project twice in a row:

$ docker build -f Dockerfile test/
Sending build context to Docker daemon  2.589kB
Step 1/2 : FROM alpine:latest
 ---> 49176f190c7e
Step 2/2 : COPY . .
 ---> be5cb22677e3
Successfully built be5cb22677e3

$ docker build -f Dockerfile test/
Sending build context to Docker daemon  2.589kB
Step 1/2 : FROM alpine:latest
 ---> 49176f190c7e
Step 2/2 : COPY . .
 ---> cf6e9713b17b
Successfully built cf6e9713b17b

Why the second time the COPY does not use the cache? (different hash!)

Reproduce
  1. mkdir test
  2. echo 'test' >test/test.txt
  3. echo 'nothing' >test/.dockerignore
  4. echo -e "FROM alpine:latest\nCOPY . ." >Dockerfile
  5. docker build -f Dockerfile test
  6. docker build -f Dockerfile test
Expected behavior

We only expect the second build during the COPY . . we use the cache.
but in the second build the cache is not used.

$ docker build -f Dockerfile test
Sending build context to Docker daemon  2.588kB
Step 1/2 : FROM alpine:latest
 ---> 49176f190c7e
Step 2/2 : COPY . .
 ---> fa1dc8b47a36
Successfully built fa1dc8b47a36

$ docker build -f Dockerfile test
Sending build context to Docker daemon  2.588kB
Step 1/2 : FROM alpine:latest
 ---> 49176f190c7e
Step 2/2 : COPY . .
 ---> 62d70fdb2c32
Successfully built 62d70fdb2c32
docker version
Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:28:08 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:25:58 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.13
  GitCommit:        78f51771157abb6c9ed224c22013cdf09962315d
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.9.1-docker)
  compose: Docker Compose (Docker Inc., v2.2.2)
  scan: Docker Scan (Docker Inc., v0.23.0)

Server:
 Containers: 7
  Running: 0
  Paused: 0
  Stopped: 7
 Images: 65
 Server Version: 20.10.22
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 78f51771157abb6c9ed224c22013cdf09962315d
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 5.14.0-1054-oem
 Operating System: Linux Mint 20.3
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 15.36GiB
 Name: serge-ThinkPad-T15-Gen-2i
 ID: CJL7:2ZDM:444W:KXHU:JD54:MDKK:BXQK:OBY3:CNSO:P7PD:TPRT:3UJB
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
Additional Info

If there is no .dockerignore file in the project directory, then the cache is working!
If the Dockerfile file is in the project directory, then the cache is working!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two docker build -f Dockerfile test commands with a .dockerignore outside the Dockerfile directory. Trace the CLI's build-context, .dockerignore, and cache-key handling, then verify that repeated builds reuse the COPY . . cache in this layout.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, go
Domain
build-system, cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.