.dockerignore not work

$ tree -a
├── a.txt
├── b.txt
├── compose.yml
├── c.txt
├── Dockerfile
└── .dockerignore



$ cat compose.yml 
services:
    test:
        build: .
        volumes:
            - type: bind
              source: .
              target: /data



$ cat Dockerfile 
FROM bash


$ cat .dockerignore 
a.txt
b.txt
c.txt




$ docker compose run --rm test ls -1 /data 
Dockerfile
a.txt
b.txt
c.txt
compose.yml



$ docker compose run --rm test ls -1 /data | grep txt && echo FILES NOT IGNORED
a.txt
b.txt
c.txt
FILES NOT IGNORED

FILES NOT IGNORED
Why?

The .dockerignore file tells Docker which files and directories to exclude from the build context when running docker build. For example when using

FROM bash
COPY . /data

the files from .dockerignore are not included in the image.

But you are using a bind mount with volumes, which mounts the files into the container.

1 Like