How to test dockerignore file?

2019-02-05 18:45发布

问题:

After reading the .dockerignore documentation, I'm wondering if there is a way to test it?

Examples

**/node_modules/

How do I check my dockerfile ignore the correct files and directories?

回答1:

To expand on VonC's suggestion, here's a sample Dockerfile and set of build/run commands you can use to create a container with the current folder's build context (named Dockerfile.build-context to avoid conflicting with your project's Dockerfile):

FROM busybox
COPY . /build-context
WORKDIR /build-context
CMD find .

Once created, execute these commands in the command line:

docker build -f Dockerfile.build-context -t build-context .
docker run --rm -it build-context

When you copy/paste the above to a prompt, the run command will output the files that were copied to the Docker host that are not ignored by your .dockerignore file.



回答2:

To get a detailed analysis of the build context you could use pwaller/docker-show-context.

$ go get -v -u github.com/pwaller/docker-show-context
$ cd ~/path/to/project/using/docker
$ docker-show-context

It outputs statistics about the build such as file sizes and upload times.



回答3:

One way is to make a small Dockerfile with an ADD or COPY directive in it.

Try to add or copy a file in a node_modules folder: it is does not succeed, that would be because of the .dockerignore.