Alexander Mills
Alexander Mills

Reputation: 100210

Specify dockerignore from command line

I have a .dockerignore file, but for one use case, I want to specify the contents of .dockerignore at the command line, something like:

docker build --ignore="node_modules" -t foo . 

is there a way to do this from the command line? I am not seeing this in the docs: https://docs.docker.com/engine/reference/commandline/build/#build-with--

Upvotes: 13

Views: 12407

Answers (1)

VonC
VonC

Reputation: 1327254

2018: No, docker build does not offer an alternative to the .dockerignore file.

That is why I usually keep a symbolic link .dockerignore pointing to the actual .dockerignore_official, except for certain case, where I switch the symlink to .dockerignore_module.

This is a workaround, but that allows me to version the different version of dockerignore I might need, and choose between the two.


Update April 2019: as mentioned by Alba Mendez in the comments, PR 901 should help:

dockerfile: add dockerignore override support

Frontend will first check for <path/to/Dockerfile>.dockerignore and, if it is found, it will be used instead of the root .dockerignore.

See moby/buildkit commit b9db1d2.

It is in Docker v19.03.0 beta1, and Alba has posted an example here:

You need to enable Buildkit mode to use:

$ export DOCKER_BUILDKIT=1

$ echo "FROM ubuntu \n COPY . tmp/" > Dockerfile
$ cp Dockerfile Dockerfile2
$ touch foo bar
$ echo "foo" > .dockerignore
$ echo "bar" > Dockerfile2.dockerignore

$ docker build -t container1 -f Dockerfile .
$ docker build -t container2 -f Dockerfile2 .
$ docker run container1 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
bar

$ docker run container2 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
foo

Update August 2019: this is now in Docker 19.03, with the following comment from Tõnis Tiigi:

  • #12886 (comment) allows setting a dockerignore file per Dockerfile if the repository contains many. (Note that this was the exact description for the initial issue)
  • BuildKit automatically ignores files that are not used, automatically removing the problem where different sets of files needed to ignore each other.
  • The cases where same Dockerfile uses different sets of files for different "modes" of build (eg. dev vs prod) can be achieved with multi-stage builds and defining the mode changes with build arguments.

Note: issue 37129 "add support for multiple (named) build-contexts" reports in May 2023 it is now supported with:

See also "Build docker image using different directory contexts"

Upvotes: 17

Related Questions