Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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--

like image 484
Alexander Mills Avatar asked May 26 '18 22:05

Alexander Mills


People also ask

Where do I put my Dockerignore file?

The . dockerignore file is a special file that can be placed within the build context directory. The build context directory is the directory that we specify at the end of a docker build command.

How do I specify a Dockerfile volume?

In Dockerfile you can specify only the destination of a volume inside a container. e.g. /usr/src/app . When you run a container, e.g. docker run --volume=/opt:/usr/src/app my_image , you may but do not have to specify its mounting point ( /opt ) on the host machine.


1 Answers

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 i

$ 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.
like image 70
VonC Avatar answered Sep 18 '22 03:09

VonC