Luminance
Luminance

Reputation: 918

Multiple .dockerignore files in same directory

In the same directory I have two Dockerfiles and I would like to add a separate .dockerignore for each of them.

Currently, I have:

Dockerfile.npm
Dockerfile.nginx

But can I have something like this?:

.dockerignore.npm
.dockerignore.nginx

Upvotes: 23

Views: 17760

Answers (3)

Brian Di Palma
Brian Di Palma

Reputation: 7487

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

Upvotes: 22

Blaise
Blaise

Reputation: 13489

Multiple dockerignore files or specifying which dockerignore file to use is currently not supported by Docker.

See this issue on Github: Add support for specifying .dockerignore file with -i/--ignore

Upvotes: 1

Tarun Lalwani
Tarun Lalwani

Reputation: 146620

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

Upvotes: 16

Related Questions