devthefuture-org / dockerfile-x

Dockerfile factorization superset
https://codeberg.org/devthefuture/dockerfile-x/
MIT License
35 stars 4 forks source link

Relative paths broken in nested Dockerfiles #7

Closed cheruvian closed 11 months ago

cheruvian commented 11 months ago

Repro File Structure

|- Dockerfile
|- nested
|| - Dockerfile.dockerfile
|| - test.txt

Dockerfile

# syntax = devthefuture/dockerfile-x
FROM ./nested/Dockerfile

nested/Dockerfile.dockerfile

FROM alpine:latest
COPY test.txt .

Logs:

# % docker build docker-test
[+] Building 0.9s (7/7) FINISHED                                                                                                                           docker:desktop-linux
 => [internal] load .dockerignore                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                            0.0s
 => [internal] load build definition from Dockerfile                                                                                                                       0.0s
 => => transferring dockerfile: 89B                                                                                                                                        0.0s
 => resolve image config for docker.io/devthefuture/dockerfile-x:latest                                                                                                    0.5s
 => CACHED docker-image://docker.io/devthefuture/dockerfile-x@sha256:e4916e77d6f4b37b1daf049fa3e7162294f8f3a28497178112cf828fc8f3ee5e                                      0.0s
 => local://dockerfile (Dockerfile)                                                                                                                                        0.0s
 => => transferring dockerfile: 89B                                                                                                                                        0.0s
 => local://context (nested)                                                                                                                                               0.0s
 => => transferring context: 145B                                                                                                                                          0.0s
 => local://context (nested.dockerfile)                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                            0.0s
Dockerfile:1
--------------------
   1 | >>> # syntax = devthefuture/dockerfile-x
   2 |     
   3 |     FROM ./nested
--------------------
ERROR: failed to solve: failed to execute dockerfile-x: failed to read dockerfile 'nested.dockerfile': open /var/lib/docker/tmp/buildkit-mount1720476522/nested.dockerfile: no such file or directory
, Output: 

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/vs8bozd19cucfra0sytv9qpew
# % docker build docker-test
[+] Building 1.3s (10/10) FINISHED                                                                                                                         docker:desktop-linux
 => [internal] load .dockerignore                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                            0.0s
 => [internal] load build definition from Dockerfile                                                                                                                       0.0s
 => => transferring dockerfile: 100B                                                                                                                                       0.0s
 => resolve image config for docker.io/devthefuture/dockerfile-x:latest                                                                                                    0.2s
 => CACHED docker-image://docker.io/devthefuture/dockerfile-x@sha256:e4916e77d6f4b37b1daf049fa3e7162294f8f3a28497178112cf828fc8f3ee5e                                      0.0s
 => local://dockerfile (Dockerfile)                                                                                                                                        0.0s
 => => transferring dockerfile: 100B                                                                                                                                       0.0s
 => local://context (nested/Dockerfile)                                                                                                                                    0.0s
 => => transferring context: 64B                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                           0.7s
 => [internal] load build context                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                            0.0s
 => [docker8bdcae--final-stage 1/1] FROM docker.io/library/alpine:latest@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48                           0.0s
 => => resolve docker.io/library/alpine:latest@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48                                                     0.0s
 => ERROR [docker8bdcae 1/1] COPY test.txt .                                                                                                                               0.0s
------
 > [docker8bdcae 1/1] COPY test.txt .:
------
Dockerfile:4
--------------------
   2 |     FROM alpine:latest AS docker8bdcae--final-stage
   3 |     FROM docker8bdcae--final-stage AS docker8bdcae
   4 | >>> COPY test.txt .
   5 |     # DOCKERFILE-X:END file="./nested/Dockerfile" includedBy="Dockerfile_1703175677"
   6 |     FROM docker8bdcae AS final-stage
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 7cdee3a5-a06c-45c9-8a6b-d7c9a2df7489::qgecqoycd4ade976g2gz2szcf: "/test.txt": not found

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/723jlqr4eltahj8b6ucm6jfdq

Expected behavior: test.txt is copied into the image based on the relative path.

devthejo commented 11 months ago

It's expected, dockerfile-x doesn't rewrite COPY instructions, as they are relative to the context of the build, not the dockerfile. It's consistent with the dockerfile behavior that decouple context and dockerfile.