compose icon indicating copy to clipboard operation
compose copied to clipboard

Inconsistent ".dockerignore" behavior between "docker build" and "docker compose build"

Open paslandau opened this issue 4 years ago • 5 comments

The behavior between docker build and docker compose build is incosistent when applying the rules of a .dockerignore file. In my specific case docker compose build includes files that should be ignored whereas docker build handles the files correctly.

Consider the following structure

./
└── context/
     ├── .dockerignore
     └── ignore_me/
        └── test
  • context/ is used as build context
  • .dockerignore contains the following rules
    ignore_me/
    
    !**test
    

The expected behavior would be, that the file context/ignore_me/test does not show up in the build context, because the full ignore_me/ directory is ignored (please note, that I'm not using ignore_me/*!)

docker build handles the situation as expected whereas docker compose build doesn't.

Steps to reproduce the issue

Create the following file structure

./
└── context/
     ├── .dockerignore
     └── ignore_me/
        ├── ignored
        └── test
├── Dockerfile
└── docker-compose.yml

context/ignore_me/ignored and context/ignore_me/test are simply empty files.

context/.dockerignore

ignore_me/

!**test

Dockerfile

FROM busybox

RUN rm -rf /tmp

COPY . /tmp

RUN find /tmp -print

docker-compose.yml

services:
  test:
    build:
      context: ./context
      dockerfile: ../Dockerfile

Build via docker build

$ docker build ./context -f Dockerfile 

# ...

#7 [3/4] COPY . /tmp
#7 sha256:87797b234423e97e90fd16329e25fb740e2a007ef7cd64854cfefc7e748228fa
#7 DONE 0.0s

#8 [4/4] RUN find /tmp -print
#8 sha256:9f205b8c8801cbde9ddcf36324c9872720e09d37bdec0912d85c6d1f0dfaca20
#8 0.206 /tmp
#8 0.206 /tmp/.dockerignore
#8 DONE 0.2s

# ...

==> only the .dockerignore file is transferred in the build context

Build via docker compose build

$ docker compose build

#7 [3/4] COPY . /tmp
#7 DONE 0.0s

#8 [4/4] RUN find /tmp -print
#8 0.182 /tmp
#8 0.182 /tmp/.dockerignore
#8 0.182 /tmp/ignore_me
#8 0.182 /tmp/ignore_me/test
#8 DONE 0.2s

# ...

==> the .dockerignore file AND the ignore_me/test file are transferred in the build context

Additional information

I believe docker compose takes the order of rules in the .dockerignore file somehow into account. E.g. when I change the .dockerignore file to

!**test

ignore_me/

docker compose build behaves as expected.

System Info

Output of docker compose version:

$ docker compose version
Docker Compose version v2.5.1

Output of docker info:

$ docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.8.2)
  compose: Docker Compose (Docker Inc., v2.5.1)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 8
  Running: 6
  Paused: 0
  Stopped: 2
 Images: 120
 Server Version: 20.10.14
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc version: v1.0.3-0-gf46b6ba
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.10.104-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 4.82GiB
 Name: docker-desktop
 ID: QA55:IJ4H:52UP:MOOW:SLVE:45MG:L2JM:2BEE:YPFH:KGBD:VOH2:R2JP
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

Additional environment details:

  • Windows 10 using Docker Desktop (non-WSL)
  • Docker version:
$ docker version
Client:
 Cloud integration: v1.0.24
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:53:11 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.8.2 (79419)
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       87a90dc
  Built:            Thu Mar 24 01:46:14 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.11
  GitCommit:        3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

paslandau avatar May 28 '22 11:05 paslandau

@paslandau

I would assume the inconsistency is due to buildx being enable by default in compose.

After looking at the documentation, I would argue that buildx, and so compose, are compliant with the documentation.

the last line of the .dockerignore that matches a particular file determines whether it is included or excluded

Which explains why /ignore_me/test is included in the context.

CLI should be updated to match that specification as well in our next release.

Note: you can achieve the same result without compose by using docker buildx build instead

https://github.com/moby/moby/pull/43037 https://github.com/docker/buildx/issues/850

maxcleme avatar Jun 02 '22 09:06 maxcleme

@maxcleme thx for the reply.

After looking at the documentation, I would argue that buildx, and so compose, are compliant with the documentation.

Ah good catch - I missed that part. I'm actually fine with both as long as it's consistent :)


Sidenote: I believe it is quite common to keep .dockerignore and .gitignore at least "somewhat" in sync. Unfortunately, the behavior of git seems to be quite different, as especially this part of the docu shows:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

Mentioning this here because it was my initial trigger for this issue: Trying to find rules that cause the same behavior on git and docker build so that I only need to maintain one file and can simply "copy" the rules from one file to the other. I was hoping that the current behavior docker build would be the same as git, but I now realize that it was likely just a coincidence in this case.

paslandau avatar Jun 02 '22 11:06 paslandau

@paslandau

Interesting answer! However I have to admit that compose is only a client for buildx, so we can't really make any changes here.

However, you may want to raise an issue on docker/buildx for starting discussion about git/docker consistency regarding .dockerignore & .gitignore processing.

maxcleme avatar Jun 02 '22 12:06 maxcleme

I was hoping that the current behavior docker build would be the same as git, but I now realize that it was likely just a coincidence in this case.

Yes, there's (unfortunately) various issues with .dockerignore (see https://github.com/moby/moby/issues/40319, where I collected some). Some of which may be "fixable", others would potentially be breaking changes, so it's a very thin line to walk when fixing things.

I should also note that docker 20.10 uses an older version of BuildKit (to be updated in the upcoming 22.06 release), so there may be some discrepancies between them (the 20.10 version missing some fixes).

In general, the switch to using BuildKit / buildx as a default builder helps somewhat here; unlike the "classic" builder, BuildKit uses an interactive session to send the build-context to the daemon. Where the classic builder would default to "send everything, except for .dockerignore'd files" (even if the Dockerfile only needed a single file), BuildKit requests only the files that the Dockerfile asks for

For example, when using the classic builder, the following Dockerfile would benefit from using a .dockerignore that excludes everything except for one-file.txt;

# syntax=docker/dockerfile:1

FROM alpine
COPY one-file.txt one-file.txt

But when using BuildKit / buildx for building, no .dockerignore is needed, as it only invalidates the cache if one-file.txt changes, and only sends one-file.txt to the daemon (nothing else).

thaJeztah avatar Jun 02 '22 12:06 thaJeztah

Let me tell you a funny fact.

i was searching about 4 hours and i found out my dockerignore file was getting ignored by docker cause of a miss spell and writing capital I.

dockerignore VS dockerIgnore!!!

god i'm going to cry 😭😭😭😭😭

ARiyou2000 avatar Jan 13 '24 12:01 ARiyou2000