Dockerfile path and relative files during container build

When referring to a Dockerfile in another folder, any instructions such as COPY from the Dockerfile will use the current directory instead of the directory of Dockerfile when running, ultimately failing:

 $ rg -N -A 1 build toml/vector.toml
[build]
dockerfile = "../docker/vector/Dockerfile"

$ cat docker/vector/Dockerfile
FROM timberio/vector:0.21.X-alpine

COPY vector.toml /etc/vector/

$ flyctl deploy -c toml/vector.toml
==> Verifying app config
--> Verified app config
==> Building image
==> Creating build context
--> Creating build context done
==> Building image with Docker
--> docker host: 20.10.13 linux x86_64
[+] Building 0.0s (0/1)
[+] Building 0.2s (5/5) FINISHED
 => [internal] load remote build context                                                                                                                                                                  0.0s
 => copy /context /                                                                                                                                                                                       0.1s
 => [internal] load metadata for docker.io/timberio/vector:0.21.X-alpine                                                                                                                                  0.0s
 => [1/2] FROM docker.io/timberio/vector:0.21.X-alpine                                                                                                                                                    0.0s
 => ERROR [2/2] COPY ./vector.toml /etc/vector/                                                                                                                                                           0.0s
------
 > [2/2] COPY ./vector.toml /etc/vector/:
------
Error failed to fetch an image or build from source: error building: failed to compute cache key: "/vector.toml" not found: not found

Here’s my current file structure:

$ ls --tree
.
├── deploy
│   └── dashboard
│       ├── README.md
│       └── run.sh
├── docker
│   └── vector
│       ├── Dockerfile
│       └── vector.toml
├── docs
│   ├── grafana.md
│   ├── loki.md
│   └── vector.toml
├── toml
│   ├── grafana.toml
│   ├── loki.toml
│   └── vector.toml

It would be great if while building, the CWD changes to the path of the Dockerfile.

Can you try fly deploy -c vector.toml toml? That will set the working dir to toml.

So, based on above file structure, the command would then be:

$ flyctl deploy -c toml/vector.toml docker/vector
Error we couldn't find a fly.toml nor an app specified by the -a flag. If you want to launch a new app, use 'flyctl launch'
$ ls -l toml/vector.toml
.rw-r--r-- jbergstroem 590 B 10 hours ago toml/vector.toml
$ ls -l docker/vector/Dockerfile
.rw-r--r-- jbergstroem 77 B 5 hours ago docker/vector/Dockerfile
$ flyctl version
flyctl v0.0.320 darwin/amd64 Commit: 58aae1e BuildDate: 2022-04-13T15:58:08Z

Edit: ah, I get it now; the proper command would be:

flyctl deploy -c ../../toml/vector.toml docker/vector
<snip>

I guess that works! That said, is there a downside to change CWD to the path of Dockerfile while building the container?

Glad you figured it out! The downside to changing the working directory is that some people rely on the working directory staying at the root. For example, if you have docker/Dockerfile.base. This is a common pattern using docker build.

That said adding a flag to use the Dockerfile root might be easier than having to know about this trick.