Additional contexts should not send unused files
Issue
Why additional context (ctx2 example below) transfers unused files to the daemon even when they are not used? Note, the main context skips unused files.
I would expect the same behavior for skipping unused files when using multiple contexts
Steps to reproduce:
$ mkdir tmp ctx2 && cd ctx2
$ touch ctx2_used
$ head -c 3MB /dev/urandom > ctx2_unused
$ cd ../tmp
$ touch used
$ head -c 2MB /dev/urandom > unused
$ echo -e "#syntax=docker/dockerfile:1.4 \n FROM ubuntu\n COPY used tmp/ \n COPY --from=ctx2 ctx2_used tmp/" > Dockerfile
$ docker buildx build -t two-contexts --build-context=ctx2=../ctx2 .
see ctx2 transferring 3.00MB in image. My actual use case, in terms of size, is way bigger than this.

Environment:
docker -v
Docker version 20.10.17, build 100c701
docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 2
Server Version: 20.10.17
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: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
Default Runtime: runc
Init Binary: docker-init
containerd version: 0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
runc version: v1.1.3-0-g6724737
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18.0-372.13.1.el8_6.x86_64
Operating System: Rocky Linux 8.6 (Green Obsidian)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.644GiB
Name: dd-rocky8
ID: 7WUG:QLRD:XFM4:H2AM:U42E:AF3C:PXS5:T6ZW:CMH2:AWPQ:DVXI:EOUG
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
Hi team! Could you provide some directions on how/where to implement this? I expect this to be somewhat trivial as the main context already exhibits the wanted behavior. Are there any traps lurking? Thanks!
I think this is already fixed by https://github.com/moby/buildkit/pull/4161 - cc @jsternberg @tonistiigi.
Looks like the issue number in the description for that PR isn't right either, should it have been for this?
That issue number in the PR makes no sense. It looks like I just made a mistake and was trying to reference the one here: https://github.com/docker/buildx/issues/1765.
I think this is the same issue so we can close this.
Fixed by https://github.com/moby/buildkit/pull/4161.