title: Is there a way to determine the docker cache SHA for a COPY?
body: <p>We're running a CI system that maintains several copies of the same Git repo in different directories, each tied to a CI agent. When the agent receives a job it checks out a specific commit and runs <code>docker build</code>. There are a few COPY steps in the Gemfile:</p>
  <pre><code>COPY Gemfile* /tmp/
  COPY engines /tmp/engines
  COPY gems /tmp/gems
  COPY .npmrc /tmp/
  COPY package.json /tmp/
  COPY yarn.lock /tmp/
  </code></pre>
  <p>I expect that when a Gemfile/Gemfile.lock change happens in Git, the first time a job runs on this host afterwards it will checkout the new code, run <code>docker build</code> and that will cause a cache miss at this point because the file content has changed. I expect that if no subsequent change is made to these files then every subsequent <code>docker build</code> will use the cache layer from this first build. This is not what I see in practice, and I cannot tell why.</p>
  <p>Here is an example from an agent's working directory that has been used today. Note, the last change to these files is Thursday, March 29th.</p>
  <pre><code>git log --pretty=format:&quot;%h%x09%ad%x09&quot; -n 1
  228c1c2ac24     Fri Apr 1 11:30:38 2022 +0100
  
  docker build . -f .buildkite/Dockerfile
  Sending build context to Docker daemon  57.18MB
  …
  Step 18/30 : COPY Gemfile* /tmp/
    ---&gt; Using cache
    ---&gt; fc4c1b01ea8e
  </code></pre>
  <p>Here's what happened when I went into a less used agent's working directory (hasn't been used since Fri Mar 25):</p>
  <pre><code>git log --pretty=format:&quot;%h%x09%ad%x09&quot; -n 1
  28725e2cffa     Fri Mar 25 14:32:03 2022 +0000
  
  git fetch origin master
  git checkout 228c1c2ac24
  Previous HEAD position was 28725e2cffa
  HEAD is now at 228c1c2ac24 
  
  docker build . -f .buildkite/Dockerfile
  Sending build context to Docker daemon  57.18MB
  ...
  Step 18/30 : COPY Gemfile* /tmp/
   ---&gt; 91d2717fa628
  </code></pre>
  <p>I'm using Docker 20.10.7</p>
  <pre><code>Server Version: 20.10.7
   Storage Driver: overlay2
    Backing Filesystem: xfs
    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: d71fcd7d8303cbf684402823e425e9dd2e99285d
   runc version: 84113eef6fc27af1b01b3181f31bbaf708715301
   init version: de40ad0
  </code></pre>
  <p>I don't expect the cache layer ID to have changed and I expected to use <code>fc4c1b01ea8e</code> in my second build as both of these two working directories have the exact same code now.</p>
  <pre><code>docker inspect fc4c1b01ea8e | jq &quot;.[0].ContainerConfig.Cmd&quot;
  [
    &quot;/bin/sh&quot;,
    &quot;-c&quot;,
    &quot;#(nop) COPY multi:70a6a1cd3b4a3148dc355928fd84fdee48b69fc60984dd7769f9365f0c2880b3 in /tmp/ &quot;
  ]
  
  docker inspect 91d2717fa628 | jq &quot;.[0].ContainerConfig.Cmd&quot;
  [
    &quot;/bin/sh&quot;,
    &quot;-c&quot;,
    &quot;#(nop) COPY multi:b4b55469c26126e9daaa9d6b37ab6d3a729686134529b6e6b5302383a013a334 in /tmp/ &quot;
  ]
  </code></pre>
  <p>Even when I pull out the <code>UpperDir</code> from each of these two layers and diff the folders there is no differences highlighted.</p>
  <p>I thought it was <code>mtime</code>, <code>atime</code> or <code>ctime</code> or some other metadata but I'm unable to ascertain if this is correct. I know that <code>touch</code>ing the files does not cause any cache miss.</p>