Azure / azure-cli

Azure Command-Line Interface
MIT License
4.01k stars 2.98k forks source link

dockerignore file with specific name is not loaded #30231

Open oginom opened 1 day ago

oginom commented 1 day ago

Describe the bug

For Docker docker build command, if {Dockerfile path}.dockerignore exists, that file overrides the default .dockerignore. (reference) But for az acr build command, {Dockerfile path}.dockerignore is not used in specific conditions.

The condition is any of these:

example filenames in which dockerignore file is not loaded:

And as the Dockerfile is renamed in the .tar archive, the build process on ACR also doesn't consider the dockerignore file.

Related command

az acr build

Errors

To reproduce the behavior, prepare these files:

$ ls .
Dockerfile              Dockerfile.dockerignore a.txt                   b.txt

$ cat Dockerfile
FROM debian

WORKDIR /app
COPY . /app

CMD ["ls", "."]

$ cat Dockerfile.dockerignore
a.txt

For docker build, a.txt is ignored

Docker version ```sh $ 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 -t test .
$ docker run -it test
Dockerfile  Dockerfile.dockerignore  b.txt

For az acr build, a.txt is not ignored. (The warning doesn't matter for this issue)

$ az acr build --registry {my_registry} --image test:latest .
$ docker pull {my_registry}.azurecr.io/test:latest
$ docker run -it {my_registry}.azurecr.io/test:latest
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
Dockerfile  Dockerfile.dockerignore  a.txt  b.txt  d9105d5fca9e4ff5a7d062fcaf57dc0f_Dockerfile

Issue script & Debug output

The same setup with --debug flag:

$ az acr build --debug --registry {my_registry} --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 Dockerfile.dockerignore to .dockerignore:

$ az acr build --debug --registry {my_registry} --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

Expected behavior: Dockerfile.dockerignore is correctly loaded.

I think these lines should be changed:

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

yonzhan commented 1 day ago

Thank you for opening this issue, we will look into it.

github-actions[bot] commented 1 day ago

Here are some similar issues that might help you. Please check if they can solve your problem.