How to specify different .dockerignore files for different builds in the same project?

Docker

Docker Problem Overview


I used to list the tests directory in .dockerignore so that it wouldn't get included in the image, which I used to run a web service.

Now I'm trying to use Docker to run my unit tests, and in this case I want the tests directory included.

I've checked docker build -h and found no option related.

How can I do this?

Docker Solutions


Solution 1 - Docker

Docker 19.03 shipped a solution for this.

> The Docker client tries to load <dockerfile-name>.dockerignore first and then falls back to .dockerignore if it can't be found. So docker build -f Dockerfile.foo . first tries to load Dockerfile.foo.dockerignore.

Setting the DOCKER_BUILDKIT=1 environment variable is currently required to use this feature. This flag can be used with docker compose since 1.25.0-rc3 by also specifying COMPOSE_DOCKER_CLI_BUILD=1.

See also:

Solution 2 - Docker

At the moment, there is no way to do this. There is a lengthy discussion about adding an --ignore flag to Docker to provide the ignore file to use - please see here.

The options you have at the moment are mostly ugly:

  • Split your project into subdirectories that each have their own Dockerfile and .dockerignore, which might not work in your case.
  • Create a script that copies the relevant files into a temporary directory and run the Docker build there.

Solution 3 - Docker

Adding the cleaned tests as a volume mount to the container could be an option here. After you build the image, if running it for testing, mount the source code containing the tests on top of the cleaned up code.

services:
   tests:
      image: my-clean-image
      volumes:
         - '../app:/opt/app' # Add removed tests

Solution 4 - Docker

I've tried activating the DOCKER_BUILDKIT as suggested by @thisismydesign, but I ran into other problems (outside the scope of this question).

As an alternative, I'm creating an intermediary tar by using the -T flag which takes a txt file containing the files to be included in my tar, so it's not so different than a whitelist .dockerignore.

I export this tar and pipe it to the docker build command, and specify my docker file, which can live anywhere in my file hierarchy. In the end it looks like this:

tar -czh -T files-to-include.txt | docker build -f path/to/Dockerfile -

Solution 5 - Docker

Another option is to have a further build process that includes the tests. The way I do it is this:

If the tests are unit tests then I create a new Docker image that is derived from the main project image; I just stick a FROM at the top, and then ADD the tests, plus any required tools (in my case, mocha, chai and so on). This new 'testing' image now contains both the tests and the original source to be tested. It can then simply be run as is or it can be run in 'watch mode' with volumes mapped to your source and test directories on the host.

If the tests are integration tests--for example the primary image might be a GraphQL server--then the image I create is self-contained, i.e., is not derived from the primary image (it still contains the tests and tools, of course). My tests use environment variables to tell them where to find the endpoint that needs testing, and it's easy enough to get Docker Compose to bring up both a container using the primary image, and another container using the integration testing image, and set the environment variables so that the test suite knows what to test.

Solution 6 - Docker

Sadly it isn't currently possible to point to a specific file to use for .dockerignore, so we generate it in our build script based on the target/platform/image. As a docker enthusiast it's a sad and embarrassing workaround.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsatoruView Question on Stackoverflow
Solution 1 - DockerthisismydesignView Answer on Stackoverflow
Solution 2 - DockernwinklerView Answer on Stackoverflow
Solution 3 - DockerKazlauskisView Answer on Stackoverflow
Solution 4 - DockerHHKView Answer on Stackoverflow
Solution 5 - DockerMark BirbeckView Answer on Stackoverflow
Solution 6 - DockerPeter Kionga-KamauView Answer on Stackoverflow