0

I have multiple Dockerfiles in my project. One Dockerfiles builds the publishable image. The other builds an image based on that artifact with extra data for regression testing. The extra data is large, and slows down sending the context to the docker daemon for building the publishable image.

I am aware of .dockerfile, but I only want to ignore the large directory with one Dockerfile. Is there a way to do something like

# Build publishable image
docker build --ignore-file .dockerignore-publish docker/Dockerfile-publish -t publish .
# Build regression image
docker build --ignore-file .dockerignore-regression docker/Dockerfile-regression -t regression .

where .dockerignore-regression does not ignore the regression data directory, but .dockerignore-publish does?

I suspect the standard answer is to only have one Dockerfile per project, or to change the root directory so only the regression build finds the regression data. Those could work, but are awkward in this case. A per-dockerfile solution would be the simplest option.

Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
  • 1
    Does this answer your question? [How to specify different .dockerignore files for different builds in the same project?](https://stackoverflow.com/questions/40904409/how-to-specify-different-dockerignore-files-for-different-builds-in-the-same-pr) – Forrest Mar 08 '21 at 20:20

2 Answers2

0

To do exactly what you need, you could tweak the .dockerignore between builds:

.dockerignore:

#path/to/test/files

Then:

# Ensure it's excluded
sed --in-place 's|^path/to/test/files|#path/to/test/files|g' .dockerignore

docker build --tag=publish ...

# Ensure it's included
sed --in-place 's|^#path/to/test/files|path/to/test/files|g' .dockerignore

docker build --tag=test ...
DazWilkin
  • 32,823
  • 5
  • 47
  • 88
0

Are you willing to use Docker BuildKit and running Docker 19.03 or higher? If so this is supported.

Enable Docker BuildKit via export DOCKER_BUILDKIT=1

Create an ignore file with the same name as the container, so Dockerfile-publish.dockerignore and populate it with the content you want to ignore for that image.

If you're not able to use BuildKit move the publish dockerfile in to different directories instead of a single docker directory and create a .dockerignore within the publish directory.

There are more details on the GitHub issue for this feature: https://github.com/moby/moby/issues/12886

Forrest
  • 1,370
  • 9
  • 20