I have the following project structure:
project
├── .docker
│ ├── service1
│ │ └── Dockerfile
│ └── service2
│ └── Dockerfile
├── src
│ └── myfile.txt
└── compose.yaml
compose.yaml:
services:
service1:
build:
context: .
dockerfile: ./.docker/service1/Dockerfile
...
I’m encountering an issue with Docker’s COPY
command when using globs on file names in subdirectories.
In recent versions of Docker, it’s my understanding that COPY supports simple globbing (e.g., using [ ]
patterns). For example, this command should work even if myfile.txt
does not exist:
COPY myfile.tx[t] /
Docker doesn’t complain if the file doesn’t exist, which seems to be the expected behavior with glob patterns. However, when I try to use globbing within a subdirectory, it consistently fails. Here’s what I’m running:
COPY src/myfile.tx[t] /
This results in an error, even though the src
directory does exist within my project:
failed to solve: lstat /var/lib/docker/tmp/buildkit-mount3262405955/src: no such file or directory
Note that when the file actually exists, it all works perfectly fine. And what’s odd is that if I apply the glob to the folder name instead of the file name, it works:
COPY sr[c]/myfile.txt /
This approach works, but it feels like a workaround rather than intended behavior.
Additional context
In the early days of Docker, the COPY
command required at least one valid file, and the typical workaround was to include a known existing file along with the globbed one:
COPY file-that-exists.txt myfile.tx[t] /
I considered adding a dummy file to ensure the command doesn’t fail, but I feel like there should be a more robust solution.
My questions are:
- Am I missing something about how globbing works with Docker
COPY
commands in subdirectories? - Is this behavior a bug, or is it expected that globs only work at the directory level and not for file names in subdirectories?
- Is there a better solution than using a dummy file or applying the glob to the directory name?
Am I missing something about how globbing works with Docker COPY commands in
subdirectories?
I think globbing works exactly like you expect. If your Dockerfile contains:
COPY src/myfile.[abc] ./
And your local src/
directory contains one or more of myfile.a
, myfile.b
, myfile.c
, those files will be copied as expected.
Is this behavior a bug, or is it expected that globs only work at the directory level and not for file names in subdirectories?
I think this is a bug. While this fails when the local src
directory is empty:
COPY src/myfile.[abc] ./
This works:
COPY . ./
COPY src/myfile.[abc] ./
An earlier COPY
command should not affect the success or failure of a subsequent COPY
command.
Is there a better solution than using a dummy file or applying the glob to the directory name?
I’m not sure that either of these solutions will work. Using a dummy file fails:
$ touch src/dummyfile
$ docker build .
[...]
ERROR: failed to solve: lstat /var/lib/docker/tmp/buildkit-mount782894620/src: no such file or directory
You can apply a glob to the directory, I guess; the following works:
COPY sr[c] ./
But that gets you different behavior (if the directory exists, then all files are copied).
1