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 OverflowYou 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
I don't think you can specify a different ignore file while doing a build. But since you are creating a separate file, you can write a shell script
build_nginx.sh
#!/bin/bash
ln -fs .dockerignore.nginx .dockerignore
docker build -f Dockerfile.nginx -t nginxbuild .
build_npm.sh
#!/bin/bash
ln -fs .dockerignore.npm .dockerignore
docker build -f Dockerfile.npm -t npmbuild .
If you need to use it with docker-compose then you need to separate folders for ngixn and npm and then can have their individual .dockerignore file. In your docker-compose.yml file you need specify the name of the directory as the context
Support multiple .dockerignore files naming convention for language mode
Proposal: recursive .dockerignore files
docker - How to specify different .dockerignore files for different builds in the same project? - Stack Overflow
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.
Docker 19.03 shipped a solution for this.
The Docker client tries to load
<dockerfile-name>.dockerignorefirst and then falls back to.dockerignoreif it can't be found. Sodocker build -f Dockerfile.foo .first tries to loadDockerfile.foo.dockerignore.
Setting the DOCKER_BUILDKIT=1 environment variable is currently required to use this feature. This flag can be used with docker compose since 1.25.0-rc3 by also specifying COMPOSE_DOCKER_CLI_BUILD=1.
See also comment0, comment1, comment2
from Mugen comment, please note
the custom dockerignore should be in the same directory as the Dockerfile and not in root context directory like the original .dockerignore
i.e. when calling
export DOCKER_BUILDKIT=1
docker build -f /path/to/custom.Dockerfile
your .dockerignore file should be at
/path/to/custom.Dockerfile.dockerignore
At the moment, there is no way to do this. There is a lengthy discussion about adding an --ignore flag to Docker to provide the ignore file to use - please see here.
The options you have at the moment are mostly ugly:
- Split your project into subdirectories that each have their own
Dockerfileand.dockerignore, which might not work in your case. - Create a script that copies the relevant files into a temporary directory and run the Docker build there.
Hey,
I have a structure like so.
- Docker.compose.yml
- .dockerignore
- app
- Dockerfile
- server
- Dockerfile
- configDoes my dockerignore file get used in the app and server folders when their respective dockerfiles are run?
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
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.