Home > Mobile >  Is it possible to use a .dockerignore conditionally?
Is it possible to use a .dockerignore conditionally?

Time:02-19

Is there a way to use a .dockerignore file conditionally?

I have a huge C# codebase, and I obviously have a lot of tests.

But tests contain lots of miscelanious data (~100-150mb for all the tests in the whole codebase).

I would like to build an image only for testing and on an other image, just take what matters (- tests, - other miscleanious stuff)

Or should this be the case where I need to create a build script just for my use case?

CodePudding user response:

Docker ignore files have the same syntax as git ignore files.

This means you can use globs and negations to ignore, but otherwise include different sections on the file tree

You should not be bundling your application test resources into your final application image, however, so sounds like you need a multi-stage docker image or other test and build scripts that contain your conditions

CodePudding user response:

If you enable BuildKit as your builder backend, then you can define a dockerignore specific to a certain Dockerfile:

When using the BuildKit backend, docker build searches for a .dockerignore file relative to the Dockerfile name. For example, running docker build -f myapp.Dockerfile . will first look for an ignore file named myapp.Dockerfile.dockerignore. If such a file is not found, the .dockerignore file is used if present. Using a Dockerfile based .dockerignore is useful if a project contains multiple Dockerfiles that expect to ignore different sets of files.

So by enabling BuildKit (by setting the variable DOCKER_BUILDKIT=1), you can have a testing.Dockerfile with its own testing.Dockerfile.dockerignore.

  • Related