You can now use multiple .dockerignore files in Docker.

Firstly you need to enable BuildKit mode; this is done either by setting:

export DOCKER_BUILDKIT=1

or by adding this configuration:

{ "features": { "buildkit": true } }

to the Docker daemon configuration in ~/.docker/daemon.json and restarting the daemon. This will be enabled by default in future.

You also need to prefix the name of your .dockerignore file with the Dockerfile name.

So if your Dockerfile is called OneApp.Dockerfile the ignore file needs to be called OneApp.Dockerfile.dockerignore.

All taken from this comment: https://github.com/moby/moby/issues/12886#issuecomment-480575928

Answer from Brian Di Palma on Stack Overflow
Discussions

Support multiple .dockerignore files naming convention for language mode
https://docs.docker.com/build/building/context/#dockerignore states: If you use multiple Dockerfiles, you can use different ignore-files for each Dockerfile. You do so using a special naming conven... More on github.com
🌐 github.com
0
September 6, 2023
Proposal: recursive .dockerignore files
I can't find anything which relates to this, but it would be useful if .dockerignore files could exist in any directory, defining paths relative to themselves (like .gitignore). Example: Working dir . ├── add-dir │ ├── add-me │ ├── .dock... More on github.com
🌐 github.com
18
March 4, 2016
docker - How to specify different .dockerignore files for different builds in the same project? - Stack Overflow
I used to list the tests directory in .dockerignore so that it wouldn't get included in the image, which I used to run a web service. Now I'm trying to use Docker to run my unit tests, and in this... More on stackoverflow.com
🌐 stackoverflow.com
March 1, 2018
Docker ignore file used in sub directories

That depends on what your docker build command looks like. If you perform a docker build -f app/Dockerfile . from the parent directory where the .dockerignore file is then it would include the .dockerignore but if you are in either app or server and run docker build ., it wouldn't be included as .dockerignore isn't in the build context.

More on reddit.com
🌐 r/docker
2
3
July 7, 2017
🌐
Docker Community
forums.docker.com › archive › feature requests
.dockerignore in multi-stage builds - Feature Requests - Docker Community Forums
August 27, 2018 - Hi, my task is to build the Single-Page-Application (Stage 1) and then serve it with NGNIX (Stage 2) FROM node:8.11.4-alpine AS build WORKDIR /usr/local/app COPY . . RUN npm install RUN npm run build FROM nginx:1.15.2-alpine COPY nginx/nginx.conf /etc/nginx/nginx.conf COPY nginx/conf.d/default.conf /etc/nginx/conf.d/ COPY --from=build /usr/local/app/dist/MY-APP /usr/share/nginx/html In Stage 1 I copy all the src files into the build container.
🌐
GitHub
github.com › microsoft › vscode-docker › issues › 4059
Support multiple .dockerignore files naming convention for language mode · Issue #4059 · microsoft/vscode-docker
September 6, 2023 - If you use multiple Dockerfiles, you can use different ignore-files for each Dockerfile. You do so using a special naming convention for the ignore-files. Place your ignore-file in the same directory as the Dockerfile, and prefix the ignore-file ...
Author   thernstig
🌐
GitHub
github.com › moby › moby › issues › 20944
Proposal: recursive .dockerignore files · Issue #20944 · moby/moby
March 4, 2016 - $ docker build -t dockerignore-test . $ docker run dockerignore-test .: Dockerfile add-dir add-me ./add-dir: add-me ignore-me # this file would not be inside the container
Author   c24w
🌐
Medium
medium.com › swlh › handling-multiple-docker-containers-with-different-privacy-settings-e6c4c0c243c3
Handling Multiple Docker Containers With Different Privacy Settings | by Michał Oleszak | The Startup | Medium
September 19, 2020 - Due to privacy reasons, some specific files must not be sent to particular servers. Hence, each container has its own blacklist of files it should not accept inside. This should be handled by the .dockerignore files, except that my .dockerignores got, well, ignored (no pun intended).
🌐
Docker
docker.com › blog › dockerfiles-now-support-multiple-build-contexts
Dockerfiles now Support Multiple Build Contexts | Docker
May 3, 2022 - If your project contains multiple components that need to be built together, it’s sometimes tricky to load them with a single build context where everything needs to be contained in one directory. There’s a variety of issues: every component needs to be accessed by their full path, you can only have one .dockerignore file...
Find elsewhere
Top answer
1 of 2
8

In a simple way no.

The .dockerignore file is used to filter what will be used in the build before even reading the Dockerfile.

The docker daemon does not see your build folder, when the build starts, all the files in the context build folder are compressed (or just packed) and send to the daemon and only then it will read your Dockerfile to build the container with the files it received.

More content about .dockerignore: https://docs.docker.com/engine/reference/builder/#/dockerignore-file

2 of 2
3

In a normal Docker build the .dockerignore file affects the "build context" that is packaged up and sent to the docker server at the beginning of the build. If the "build context" doesn't contain the files then you can't reference them, so this is how the files are excluded. They don't "exist" for the build.

Rocker claims to run differently by not sending a build context to the server. The code looks like each ADD/COPY step is composed into a tar file that ignores the files. Also, the .dockerignore is read once at startup and cached.

As Rocker is not sending the build context before each build, only filtering for each ADD/COPY command, there is hope. But due to the ignore data being read only once at startup you can't do anything funky like copying different .dockerignore files at different stages of the build though.

Use MOUNT

One option is to continue using the .dockerignore as is and use a Rocker MOUNT command to manually copy the ignored directories. Their last example in the mount section demonstrates:

FROM debian:jessie
ADD . /app                       # assets/ in .dockerignore
WORKDIR /app
MOUNT .:/context
RUN cp -r /context/assets /app   # include assets/

Change App Structure

The only other useful option I can think of is to split out your ADD or COPY into multiple commands so that you don't rely on the the .dockerignore to filter files to the other 3 images. This would probably require your assets directory to be stored outside of your application root.

🌐
CodeGenes
codegenes.net › blog › multiple-dockerignore-files-in-same-directory
How to Use Multiple .dockerignore Files in the Same Directory for Separate Dockerfiles — codegenes.net
For example, your frontend `Dockerfile` might need to exclude `node_modules/`, while your backend `Dockerfile` needs to exclude `venv/`—a one-size-fits-all `.dockerignore` won’t work. This blog will guide you through solving this problem by using **multiple `.dockerignore` files** with separate ...
🌐
Codefresh
codefresh.io › home › blog › do not ignore .dockerignore (it’s expensive and potentially dangerous)
Do not ignore .dockerignore (it's expensive and potentially dangerous) | Codefresh
March 13, 2025 - If you copy files into you Docker ... other files not only into the Docker build context, but also into the final Docker image. There are multiple Docker images currently available on DockerHub, that expose application source code, passwords, keys and credentials (for example Twitter Vine). Copying the .git folder in a Docker image by mistake is especially damaging. Tip: Always mention your .git folder in your .dockerignore ...
🌐
GitHub
github.com › docker › compose › issues › 6022
Add support for specifying custom dockerignore · Issue #6022 · docker/compose
June 17, 2018 - Troubles arise when there are several images that use the same context, but require different sets of files. This takes away the simplicity of maintaining a small context and using a straightforward COPY to preserve structure. What would really help is if I could specify something akin to: x-foo: &foo build: context: . dockerfile: ./foo/Dockerfile dockerignore: ./foo/.dockerignore x-bar: &bar build: context: .
Author   langri-sha
🌐
GitHub
github.com › moby › moby › issues › 12886
Add support for specifying .dockerignore file with -i/--ignore · Issue #12886 · moby/moby
April 30, 2015 - As several people have mentioned (@thaJeztah, @duglin) in #9707, it would be great to be able specify the .dockerignore file using -i/--ignore in conjunction with named dockerfiles. It is often difficult to use named dockerfiles because ...
Author   tristanz
🌐
Docker
docs.docker.com › manuals › docker build › build context
Build context | Docker Docs
If this file exists, the files and directories that match patterns in the files are removed from the build context before it's sent to the builder. If you use multiple Dockerfiles, you can use different ignore-files for each Dockerfile.
🌐
Medium
medium.com › @LihauTan › took-me-hours-to-realise-why-docker-build-ignores-my-dockerignore-and-this-is-what-ive-learned-2f87c770ea9c
Took me hours to realise why docker build ignores my .dockerignore, and this is what I’ve learned | by Tan Li Hau | Medium
November 26, 2017 - Docker CLI will only look for .dockerignore file in the root directory of the context, if you have a monorepo of multiple packages, make sure .dockerignore file is on the root directory of your context, it will ignore it if it is somewhere in the subfolder.
🌐
TechRepublic
techrepublic.com › home › what are .dockerignore files, and why you should use them?
What are .dockerignore files, and why you should use them? - TechRepublic
November 7, 2022 - However, each statement inside your Dockerfile would result in building a new intermediate image layer. Because of this, when you make changes to the Dockerfile over and over, this can lead to multiple ...
🌐
CloudBees
cloudbees.com › blog › leveraging-the-dockerignore-file-to-create-smaller-images
Leveraging the dockerignore File to Create Smaller Images
In a "glob" pattern, this matches everything that starts with a .. In a regular expression, this would match every character, essentially matching every file and directory. Since the .dockerignore file uses Unix style glob patterns, we can safely add .* and only dot-files will be excluded.
🌐
Nelson
nelson.cloud › posts › ignore files across all subdirectories in .dockerignore
Ignore Files Across All Subdirectories in .dockerignore | Nelson Figueroa
December 9, 2025 - To ignore a file across all subdirectories, prefix the filename with **. For example, to ignore the file file.txt in all subdirectories, add the following to .dockerignore:
Top answer
1 of 1
10

When you build an image, you send the Docker daemon a build context; in your Compose setup this is the directory named in the build: { context: } setting. The .dockerignore file must be in that exact directory and nowhere else. Its actual effect is to cause files to be excluded from the build context, which can result in a faster build sequence.

The build context's other important effect is that all Dockerfile COPY directives are considered relative to that directory; you cannot COPY from parent or sibling directories. So if files are shared between projects, you must set the context directory to some ancestor directory of all of the files that will be included, and COPY directives will be relative to that directory (even if the Dockerfiles are in per-project directories). See also How to include files outside of Docker's build context?

If your projects are completely separate: maybe there's a front-end and a back-end project, or in your case a producer and a consumer that share a message format but not any actual code. Then in this case:

  • Put a Dockerfile, named exactly Dockerfile, in each project subdirectory
  • Put a .dockerignore file in each project subdirectory (it cannot be in the parent directory)
  • COPY directives are relative to the project subdirectory
    COPY requirements.txt ./
    
  • In the Compose file, you can use the shorthand build: directory syntax, since you have the standard (default) dockerfile: name
    version: '3.8'
    services:
       producer:
         build: ./python_producer
         environment:
           - RABBITMQ_HOST=rabbitmq
       consumer:
         build: ./python_consumer
         environment:
           - RABBITMQ_HOST=rabbitmq
       rabbitmq:
         image: rabbitmq:3
         hostname: rabbitmq # RabbitMQ is very unusual in needing to set this
    

If your projects share code or other files: in your example maybe you define Python data structures for the message format in shared code. This in this case:

  • Put a Dockerfile, named exactly Dockerfile, in each project subdirectory
  • Put a single .dockerignore file in the project root
  • COPY directives are relative to the project root directory
    COPY python_producer/requirements.txt ./
    
  • In the Compose file you need to specify context: . and dockerfile: pointing at a per-component Dockerfile
    version: '3.8'
    services:
       producer:
         build:
           context: .
           dockerfile: python_producer/Dockerfile
         environment:
           - RABBITMQ_HOST=rabbitmq
       consumer:
         build:
           context: .
           dockerfile: python_consumer/Dockerfile
         environment:
           - RABBITMQ_HOST=rabbitmq
       rabbitmq:
         image: rabbitmq:3
         hostname: rabbitmq # RabbitMQ is very unusual in needing to set this