Specify dockerignore from command line

2019-04-14 09:44发布

问题:

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

回答1:

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