-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
Describe the bug
According to Docker's official documentation, if a file named {Dockerfile-path}.dockerignore exists, it should be used as the .dockerignore file, overriding the default .dockerignore in the root of the context.
However, the az acr build command fails to load this Dockerfile-specific ignore file under the following conditions:
- The Dockerfile is named
Dockerfile(e.g.,DockerfileandDockerfile.dockerignore). - The Dockerfile is located in a subdirectory (e.g.,
/dir/dev.Dockerfileand/dir/dev.Dockerfile.dockerignore).
Consequently, files that should be excluded are incorrectly included in the build context uploaded to Azure Container Registry.
Related command
az acr build
Errors
1. Create the following files:
$ ls .
Dockerfile Dockerfile.dockerignore a.txt b.txt
$ cat Dockerfile
FROM debian
WORKDIR /app
COPY . /app
CMD ["ls", "."]
$ cat Dockerfile.dockerignore
a.txt2. Run a standard docker build (Correct Behavior)
The docker build command correctly uses Dockerfile.dockerignore and excludes a.txt from the context.
Docker version
$ docker version
Client:
Version: 27.1.1-rd
API version: 1.45 (downgraded from 1.46)
Go version: go1.21.12
Git commit: cdc3063
Built: Wed Jul 24 17:06:24 2024
OS/Arch: darwin/arm64
Context: rancher-desktop
Server:
Engine:
Version: 26.1.3
API version: 1.45 (minimum version 1.24)
Go version: go1.22.5
Git commit: 8e96db1c328d0467b015768e42a62c0f834970bb
Built: Sun Jul 7 17:34:20 2024
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: v1.7.17
GitCommit: 3a4de459a68952ffb703bbe7f2290861a75b6b67
runc:
Version: 1.1.12
GitCommit: 51d5e94601ceffbbd85688df1c928ecccbfa4685
docker-init:
Version: 0.19.0
GitCommit:$ docker build --platform linux/arm64 -t test .
$ docker run -it test
Dockerfile Dockerfile.dockerignore b.txt3. Run az acr build (Incorrect Behavior)
The az acr build command ignores Dockerfile.dockerignore and includes a.txt in the context.
$ az acr build --registry {my_registry} --platform linux/arm64 --image test:latest .
$ docker pull {my_registry}.azurecr.io/test:latest
$ docker run -it {my_registry}.azurecr.io/test:latest
Dockerfile Dockerfile.dockerignore a.txt b.txt d9105d5fca9e4ff5a7d062fcaf57dc0f_DockerfileIssue script & Debug output
The debug logs confirm this behavior.
- With
Dockerfile.dockerignore, the debug log shows no evidence of parsing any ignore file before archiving:
$ az acr build --debug --registry {my_registry} --platform linux/arm64 --image test:latest .
...
cli.azure.cli.command_modules.acr.build: '--file or -f' is not provided. './Dockerfile' is used.
cli.azure.cli.command_modules.acr._archive_utils: Packing source code into tar to upload...
cli.azure.cli.command_modules.acr._archive_utils: Uploading archived source code from '/var/folders/xn/d4tvgf8d0r35m512_zvlb4dr0000gn/T/build_archive_176fe68807b5421e88f645afd32712c4.tar.gz'...
...- After renaming to
.dockerignore, the debug log correctly shows the ignore rules being processed:
$ az acr build --debug --registry {my_registry} --platform linux/arm64 --image test:latest .
...
cli.azure.cli.command_modules.acr.build: '--file or -f' is not provided. './Dockerfile' is used.
cli.azure.cli.command_modules.acr._archive_utils: Packing source code into tar to upload...
cli.azure.cli.command_modules.acr._archive_utils: .dockerignore: no rule for ''. parent ignore 'False'
cli.azure.cli.command_modules.acr._archive_utils: .dockerignore: no rule for 'Dockerfile'. parent ignore 'False'
cli.azure.cli.command_modules.acr._archive_utils: .dockerignore: no rule for 'b.txt'. parent ignore 'False'
cli.azure.cli.command_modules.acr._archive_utils: .dockerignore: no rule for '.dockerignore'. parent ignore 'False'
cli.azure.cli.command_modules.acr._archive_utils: .dockerignore: rule 'a.txt' matches 'a.txt'.
cli.azure.cli.command_modules.acr._archive_utils: Uploading archived source code from '/var/folders/xn/d4tvgf8d0r35m512_zvlb4dr0000gn/T/build_archive_83287bf1feb64a2c9f5608a41c9bf7d4.tar.gz'...
...Expected behavior
The az acr build command should correctly identify and use the {Dockerfile-path}.dockerignore file, ensuring its behavior is consistent with the standard docker build command.
Cause and Suggested Fix
The issue appears to stem from incorrect path resolution logic in _archive_utils.py. The following changes should resolve the bug:
- To support Dockerfiles named
Dockerfile:- The
ifcondition should be removed from this line: if original_docker_file_name != "Dockerfile":
- The
- To support Dockerfiles in subdirectories:
- The path construction should be simplified to pass the full
docker_file_pathand append.dockerignore. azure-cli/src/azure-cli/azure/cli/command_modules/acr/_archive_utils.py
Lines 155 to 156 in 0122c11
docker_ignore_file_override = os.path.join( source_location, "{}.dockerignore".format(original_docker_file_name))
- The path construction should be simplified to pass the full
Environment Summary
azure-cli 2.65.0
core 2.65.0
telemetry 1.1.0
Extensions:
bastion 0.2.5
storage-preview 1.0.0b2
Dependencies:
msal 1.31.0
azure-mgmt-resource 23.1.1
Python location '/usr/local/Cellar/azure-cli/2.65.0_2/libexec/bin/python'
Extensions directory '/Users/user/.azure/cliextensions'
Python (Darwin) 3.11.10 (main, Sep 7 2024, 01:03:31) [Clang 15.0.0 (clang-1500.3.9.4)]
Additional context
No response