Skip to content

Consecutive builds confuse build-context #6470

Closed

Description

@kohtala

Contributing guidelines and issue reporting guide

Well-formed report checklist

  • I have found a bug that the documentation does not mention anything about my problem
  • I have found a bug that there are no open or closed issues that are related to my problem
  • I have provided version/information about my environment and done my best to provide a reproducer

Description of bug

Bug description

Building a project with a --build-context given with different directory runs the build with an earlier context instead of the one provided.

Reproduction

#!/bin/sh -x

mkdir a b
echo "hello from a" > a/file
#dd if=/dev/urandom of=a/largefile bs=1M count=1
echo "hello from b" > b/file
dd if=/dev/urandom of=b/largefile bs=1M count=1

cat > Dockerfile <<EOF
FROM alpine

RUN --mount=from=context,target=/context <<NUR
    cat /context/file
    cp /context/file /
NUR
EOF
trap "rm -rf a b Dockerfile" EXIT

ls -laR .

docker buildx build --load --build-context context=a --no-cache -t a .
docker buildx build --load --build-context context=b --no-cache -t b .

docker run --rm a cat /file
docker run --rm b cat /file
Test run output
$ sh -x test.sh
+ mkdir a b
+ echo hello from a
+ echo hello from b
+ dd if=/dev/urandom of=b/largefile bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1,0 MB, 1,0 MiB) copied, 0,00433338 s, 242 MB/s
+ cat
+ trap rm -rf a b Dockerfile EXIT
+ ls -laR .
.:
total 24
drwxrwxr-x  4 kohtala kohtala 4096 tammi  15 18:33 .
drwxrwxr-x 49 kohtala kohtala 4096 tammi  15 17:34 ..
drwxrwxr-x  2 kohtala kohtala 4096 tammi  15 18:33 a
drwxrwxr-x  2 kohtala kohtala 4096 tammi  15 18:33 b
-rw-rw-r--  1 kohtala kohtala  109 tammi  15 18:33 Dockerfile
-rw-rw-r--  1 kohtala kohtala  558 tammi  15 18:33 test.sh

./a:
total 12
drwxrwxr-x 2 kohtala kohtala 4096 tammi  15 18:33 .
drwxrwxr-x 4 kohtala kohtala 4096 tammi  15 18:33 ..
-rw-rw-r-- 1 kohtala kohtala   13 tammi  15 18:33 file

./b:
total 1036
drwxrwxr-x 2 kohtala kohtala    4096 tammi  15 18:33 .
drwxrwxr-x 4 kohtala kohtala    4096 tammi  15 18:33 ..
-rw-rw-r-- 1 kohtala kohtala      13 tammi  15 18:33 file
-rw-rw-r-- 1 kohtala kohtala 1048576 tammi  15 18:33 largefile
+ docker buildx build --load --build-context context=a --no-cache -t a .
#0 building with "buildkit-dev" instance using docker-container driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 146B done
#1 DONE 0.0s

#2 [auth] library/alpine:pull token for registry-1.docker.io
#2 DONE 0.0s

#3 [context context] load .dockerignore
#3 transferring _context: 2B done
#3 DONE 0.0s

#4 [internal] load metadata for docker.io/library/alpine:latest
#4 DONE 0.8s

#5 [internal] load .dockerignore
#5 transferring context: 2B done
#5 DONE 0.0s

#6 [context context] load from client
#6 transferring _context: 44B done
#6 DONE 0.0s

#7 [stage-0 1/2] FROM docker.io/library/alpine:latest@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62
#7 resolve docker.io/library/alpine:latest@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62 0.0s done
#7 CACHED

#8 [stage-0 2/2] RUN --mount=from=context,target=/context <<NUR (cat /context/file...)
#8 0.071 hello from a
#8 DONE 0.1s

#9 exporting to oci image format
#9 exporting layers 0.1s done
#9 exporting manifest sha256:1e12f7bddb93dd3f36ef3cef9adb2a2b0ee5fd62c34e70eb92a475cbaa04d999 0.0s done
#9 exporting config sha256:1f33eed3b2e20b39981f097bb58a4ac389387bb7ed68d3aaa50b1abcea5ac4c4 0.0s done
#9 sending tarball
#9 sending tarball 0.3s done
#9 DONE 0.4s

#10 importing to docker
#10 DONE 0.0s
+ docker buildx build --load --build-context context=b --no-cache -t b .
#0 building with "buildkit-dev" instance using docker-container driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 146B done
#1 DONE 0.0s

#2 [context context] load .dockerignore
#2 transferring _context: 2B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/alpine:latest
#3 DONE 0.2s

#4 [internal] load .dockerignore
#4 transferring context: 2B done
#4 DONE 0.0s

#5 [context context] load from client
#5 transferring _context: 1.05MB 0.0s done
#5 DONE 0.0s

#6 [stage-0 1/2] FROM docker.io/library/alpine:latest@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62
#6 resolve docker.io/library/alpine:latest@sha256:865b95f46d98cf867a156fe4a135ad3fe50d2056aa3f25ed31662dff6da4eb62 0.0s done
#6 CACHED

#7 [stage-0 2/2] RUN --mount=from=context,target=/context <<NUR (cat /context/file...)
#7 0.063 hello from a
#7 DONE 0.1s

#8 exporting to oci image format
#8 exporting layers 0.1s done
#8 exporting manifest sha256:446979d0ea309400583ced6c2e62953795fe4828151dfbf177d9861ffffead3d 0.0s done
#8 exporting config sha256:1ad64adad6e9f3465a9a38932539dd383e96fe14213ae7c8da2d1d2b5ea1af71 0.0s done
#8 sending tarball
#8 sending tarball 0.2s done
#8 DONE 0.4s

#9 importing to docker
#9 DONE 0.0s
+ docker run --rm a cat /file
hello from a
+ docker run --rm b cat /file
hello from a

The correct context seems to be loaded as seen from the size, but the RUN command has wrong context mounted. Adding the largefile in the a directory causes it to mount correct context.

Version information

$ docker buildx inspect
Name:          buildkit-dev
Driver:        docker-container
Last Activity: 2026-01-15 16:25:17 +0000 UTC

Nodes:
Name:                  buildkit-dev0
Endpoint:              unix:///var/run/docker.sock
Driver Options:        image="moby/buildkit:dev"
Status:                running
BuildKit daemon flags: --allow-insecure-entitlement=network.host
BuildKit version:      faed462
Platforms:             linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
Labels:
 org.mobyproject.buildkit.worker.executor:         oci
 org.mobyproject.buildkit.worker.hostname:         9d1e00c96a4a
 org.mobyproject.buildkit.worker.network:          host
 org.mobyproject.buildkit.worker.oci.process-mode: sandbox
 org.mobyproject.buildkit.worker.selinux.enabled:  false
 org.mobyproject.buildkit.worker.snapshotter:      overlayfs
GC Policy rule#0:
 All:            false
 Filters:        type==source.local,type==exec.cachemount,type==source.git.checkout
 Keep Duration:  48h0m0s
 Max Used Space: 488.3MiB
GC Policy rule#1:
 All:            false
 Keep Duration:  1440h0m0s
 Reserved Space: 9.313GiB
 Max Used Space: 93.13GiB
 Min Free Space: 188.1GiB
GC Policy rule#2:
 All:            false
 Reserved Space: 9.313GiB
 Max Used Space: 93.13GiB
 Min Free Space: 188.1GiB
GC Policy rule#3:
 All:            true
 Reserved Space: 9.313GiB
 Max Used Space: 93.13GiB
 Min Free Space: 188.1GiB

Activity

  1. added theissue type on Jan 15, 2026
  2. tonistiigi commented on Jan 16, 2026

    @tonistiigi
    Member

    This is most likely ultimately #2592 (comment) , but it is possible the specific case can be improved as we never need to use the same destination directory if the local basename is different(I guess it currently uses same dest directory because of the same context name).

  3. kohtala commented on Jan 16, 2026

    @kohtala
    ContributorAuthor

    Thank you. I worked around it by building the context name dynamically from the directory name (valid repository name) and passing it as a build-arg.

    #!/bin/sh
    
    mkdir a b
    echo "hello from a" > a/file
    #dd if=/dev/urandom of=a/largefile bs=1M count=1
    echo "hello from b" > b/file
    dd if=/dev/urandom of=b/largefile bs=1M count=1
    
    cat > Dockerfile <<'EOF'
    ARG CONTEXT=scratch
    FROM ${CONTEXT} AS ctx
    
    FROM alpine
    
    RUN --mount=from=ctx,target=/context <<NUR
        cat /context/file
        cp /context/file /
    NUR
    EOF
    trap "rm -rf a b Dockerfile" EXIT
    
    ls -laR .
    
    docker buildx build --load --build-arg CONTEXT=a --build-context a=a --no-cache -t a .
    docker buildx build --load --build-arg CONTEXT=b --build-context b=b --no-cache -t b .
    
    docker run --rm a cat /file
    docker run --rm b cat /file
  4. kohtala commented on Jan 16, 2026

    @kohtala
    ContributorAuthor

    We've had similar issue show up before. It seemed some of the files were from new context and some from the old. If this relates to some attempt to transmit only differences, it could be caused by this same issue. It had only found some of the changes but not all.

  5. modified the milestone: v0.28.0 on Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions