I have done the following
Steps to reproduce
- Create a project directory, for example, a Rust project which generates a large
target directory. - Create a
.dockerignore file in the root of the project with the following content to exclude the build artifacts and IDE folders:
- Have a directory structure similar to this, where
target is large (e.g., >400MB):
$ dua
4.10 KB .dockerignore
4.10 KB .gitignore
4.10 KB Cargo.toml
4.10 KB Dockerfile
...
409.96 MB target
411.48 MB total
- Create a
Dockerfile that uses a COPY . . instruction to copy the project source:
# DockerfileFROM rust:alpine as builder
WORKDIR /opt/src/my-project
# This step is expected to exclude files from .dockerignoreCOPY . .
RUN cargo build --release
...
- Run the build command:
container build --tag my-app --file Dockerfile .
Current behavior
The build process includes files and directories that should be ignored, resulting in an unnecessarily large build context.
In one instance, the build log shows a context size of over 400MB, even though the source code is less than 1MB and the target directory is in .dockerignore:
[internal] load build context 3.8s
=> => transferring context: 405.05MB 3.8s
The problem is more severe with very large ignored directories. For example, with a target directory of ~6.8 GB, the build fails immediately during the context creation phase, before any Dockerfile instructions are executed.
Project structure:
$ dua
...
41.68 MB .git
6.81 GB target
6.85 GB total
Build command and error:
$ container build --tag zexus-axum-lite --file Dockerfile-axum-lite .
[+] Building 7.1s (2/3)
=> [resolver] fetching image...docker.io/library/rust:slim 0.0s
=> [resolver] fetching image...docker.io/library/debian:bookworm-slim 0.0s
Error: failed to create entry
The error failed to create entry suggests that the tool cannot handle the massive build context, a problem that the .dockerignore file is specifically designed to prevent.
Expected behavior
The container build command should respect the .dockerignore file. The target directory and any other specified patterns should be excluded from the build context sent to the builder.
The build context size should be small, reflecting only the necessary source files. For example, after applying a workaround, the context size is correctly reduced:
[internal] load build context 0.2s
=> => transferring context: 94.59kB 0.2s
The build should proceed using only the files not excluded by .dockerignore.
Workaround
The issue can be circumvented by avoiding COPY . . and instead explicitly copying only the required files and directories in the Dockerfile:
-COPY . .+COPY Cargo.lock Cargo.toml /opt/src/race-dns-proxy/+COPY src /opt/src/race-dns-proxy/src
With this change, the build succeeds and the build context is appropriately small. This strongly suggests the issue lies with .dockerignore handling.
Environment
- OS: macOS 15.6
- Xcode: Version 16.4 (16F6)
- Container: container CLI version 0.3.0 (build: release, commit: 3fcf647)
Relevant log output
Code of Conduct
I have done the following
Steps to reproduce
targetdirectory..dockerignorefile in the root of the project with the following content to exclude the build artifacts and IDE folders:targetis large (e.g., >400MB):Dockerfilethat uses aCOPY . .instruction to copy the project source:container build --tag my-app --file Dockerfile .Current behavior
The build process includes files and directories that should be ignored, resulting in an unnecessarily large build context.
In one instance, the build log shows a context size of over 400MB, even though the source code is less than 1MB and the
targetdirectory is in.dockerignore:The problem is more severe with very large ignored directories. For example, with a
targetdirectory of ~6.8 GB, the build fails immediately during the context creation phase, before anyDockerfileinstructions are executed.Project structure:
Build command and error:
The error
failed to create entrysuggests that the tool cannot handle the massive build context, a problem that the.dockerignorefile is specifically designed to prevent.Expected behavior
The
container buildcommand should respect the.dockerignorefile. Thetargetdirectory and any other specified patterns should be excluded from the build context sent to the builder.The build context size should be small, reflecting only the necessary source files. For example, after applying a workaround, the context size is correctly reduced:
The build should proceed using only the files not excluded by
.dockerignore.Workaround
The issue can be circumvented by avoiding
COPY . .and instead explicitly copying only the required files and directories in theDockerfile:With this change, the build succeeds and the build context is appropriately small. This strongly suggests the issue lies with
.dockerignorehandling.Environment
Relevant log output
Code of Conduct