Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --parents flag for ADD/COPY instructions #3000

Closed
wants to merge 3 commits into from

Conversation

DYefimov
Copy link
Contributor

@DYefimov DYefimov commented Aug 4, 2022

partially fixes: #2917

.parents_test.sh:

#!/usr/bin/env sh
set -e

TMP_DIR=$(mktemp -dt parents_test.XXXXXXXX)

echo "Created \"$TMP_DIR\""
trap "echo \"Removing \\\"$TMP_DIR\\\"\"; rm -rf \"$TMP_DIR\"" EXIT

mkdir -p $TMP_DIR/src/dir1
mkdir -p $TMP_DIR/src/dir2/subdir2

touch $TMP_DIR/src/dir1/file1-1.py
touch $TMP_DIR/src/dir1/file1-2.go
touch $TMP_DIR/src/dir2/subdir2/file2-1.go
touch $TMP_DIR/src/dir2/subdir2/file2-2.js

cat << DOCKERFILE > $TMP_DIR/dockerfile
# syntax=dyefimov/dockerfile:copy_parents-labs

FROM scratch

COPY ./src/*/*/*.go ./dst1/
COPY --parents ./src/*/*/*.go ./dst2/

CMD ["echo", "hello-world"]
DOCKERFILE

echo "======================================================="
SOCKET_DIR=$(realpath $(dirname $0))/.tmp
mkdir -p $SOCKET_DIR
sudo buildctl \
    --addr=unix://$SOCKET_DIR/buildkitd.socket \
    build \
        --frontend=dockerfile.v0 \
        --local context=$TMP_DIR \
        --local dockerfile=$TMP_DIR \
        --no-cache \
        --output=type=docker,name=test_parents | docker load

echo "======================================================="
echo "source:"
find $TMP_DIR/src -printf '%P\n' | grep -v '^[[:space:]]*$' | sort

echo "======================================================="
docker container rm -f test_parents > /dev/null 2>&1
docker container create --name=test_parents test_parents > /dev/null 2>&1
echo "COPY:"
docker container export test_parents | tar tf - | grep "dst1" | sed 's#^dst1/##' | grep -v '^[[:space:]]*$' | sort
echo "COPY --parents:"
docker container export test_parents | tar tf - | grep "dst2" | sed 's#^dst2/##' | grep -v '^[[:space:]]*$' | sort
docker container rm -f test_parents > /dev/null 2>&1

echo "======================================================="

.parents_test.sh output:

$ ./.parents_test.sh 
Created "/tmp/parents_test.XduwOh8Y"
=======================================================
[+] Building 34.9s (8/8) FINISHED                                                                                      
 => [internal] load build definition from Dockerfile                                                              0.2s
 => => transferring dockerfile: 196B                                                                              0.0s
 => [internal] load .dockerignore                                                                                 0.2s
 => => transferring context: 2B                                                                                   0.0s
 => resolve image config for docker.io/dyefimov/dockerfile:copy_parents-labs                                      3.5s
 => docker-image://docker.io/dyefimov/dockerfile:copy_parents-labs@sha256:d610b759d921bc35515d468718f2609cd6a43  20.0s
 => => resolve docker.io/dyefimov/dockerfile:copy_parents-labs@sha256:d610b759d921bc35515d468718f2609cd6a436327e  0.0s
 => => sha256:9b5f69abec23db87906aa36405c32438ee46fa07539bab74519bbc1df4f7f43b 10.50MB / 10.50MB                 19.3s
 => => extracting sha256:9b5f69abec23db87906aa36405c32438ee46fa07539bab74519bbc1df4f7f43b                         0.6s
 => [internal] load build context                                                                                 0.0s
 => => transferring context: 137B                                                                                 0.0s
 => [1/2] COPY ./src/*/*/*.go ./dst1/                                                                             5.4s
 => [2/2] COPY --parents ./src/*/*/*.go ./dst2/                                                                   4.7s
 => exporting to oci image format                                                                                 0.4s
 => => exporting layers                                                                                           0.3s
 => => exporting manifest sha256:561ee1a533640037d09124564cc0c87a8b83f2afb6c295388396aee90c358b41                 0.0s
 => => exporting config sha256:e8e6fb69968b721a8025df93212bf37429781089a3be88f0f73f275c3023c096                   0.0s
 => => sending tarball                                                                                            0.0s
374816127ac7: Loading layer [==================================================>]     138B/138B
d46573954a71: Loading layer [==================================================>]     186B/186B
The image test_parents:latest already exists, renaming the old one with ID sha256:623c1fa1a66cc452143122dfce26062f698284b8428baf59cf25da8ab6416890 to empty string
Loaded image: test_parents:latest
=======================================================
source:
dir1
dir1/file1-1.py
dir1/file1-2.go
dir2
dir2/subdir2
dir2/subdir2/file2-1.go
dir2/subdir2/file2-2.js
=======================================================
COPY:
file2-1.go
COPY --parents:
src/
src/dir2/
src/dir2/subdir2/
src/dir2/subdir2/file2-1.go
=======================================================
Removing "/tmp/parents_test.XduwOh8Y"

@DYefimov DYefimov closed this Aug 4, 2022
@DYefimov DYefimov deleted the copy_parents branch August 4, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] ADD/COPY scripting improvements
1 participant