Dockerfile COPY with globbing fails in subdirectory when file doesn’t exist

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:

  1. Am I missing something about how globbing works with Docker COPY commands in subdirectories?
  2. Is this behavior a bug, or is it expected that globs only work at the directory level and not for file names in subdirectories?
  3. 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật