SumNeuron
SumNeuron

Reputation: 5188

docker-compose namespacing for dockerignore

docker-compose version info:

docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j  20 Nov 2018

I have several Dockerfiles and docker-compose files named in the following conventions

Dockerfile.a
Dockerfile.b
Dockerfile.c
docker-compose.x.dev.yml
docker-compose.x.prod.yml
docker-compose.y.dev.yml
docker-compose.y.prod.yml

Where docker-compose.x.prod.yml might look like

version: '3'

services:
  a:
    image: service_a
    container_name: cont_a
    build:
      context: .
      dockerfile: ./Dockerfile.a
  b:
    image: service_b
    container_name: cont_b
    build:
      context: .
      dockerfile: ./Dockerfile.b

and I might make the following call

docker-compose -f docker-compose.x.prod.yml -f docker-compose.x.dev.yml build

How should I name my dockerignore file? Which of any of the following do I need?

Dockerfile.a.dockerignore
docker-compose.x.dockerignore
docker-compose.x.prod.dockerignore
docker-compose.x.dev.dockerignore

Upvotes: 1

Views: 2283

Answers (2)

Chris Johnson
Chris Johnson

Reputation: 21926

You can have multiple dockerignore files, one per dockerfile, if you use BuildKit. This isn't available when not using BuildKit. If you're not using BuildKit, see @DavidMaze's answer.

Builds for the normally-named Dockerfile use the normally-named .dockerignore file, whether you use BuildKit or not. This is the classic default that we're all accustomed to.

For other dockerfile names, the name for the ignore file should be the dockerfile name + a .dockerignore suffix. For example, BuildKit builds for a dockerfile named Dockerfile.test would use an ignore file named Dockerfile.test.dockerignore. And if that specially-named ignore file doesn't exist, the build would fall back and use the usual .dockerignore file.

This features was added in Docker v19.03 - see the release notes. This is a BuildKit-only feature. To use BuildKit, you can either export DOCKER_BUILDKIT=1 in the build environment, or specify it for the duration of individual commands like DOCKER_BUILDKIT=1 docker build ....

Upvotes: 4

David Maze
David Maze

Reputation: 158647

If you are not using the newer BuildKit backend, then the .dockerignore file must be in the context: directory and must be named exactly .dockerignore. Neither docker build nor the docker-compose build: block have the ability to provide an alternate name for it. In your example, both services will always use the same .dockerignore file.

This means that any file either one of the services needs can't be listed in the .dockerignore file. In practice, it probably means you also don't want to blindly COPY . into an image, since that will contain quite a bit of content that an individual image won't need; you will need to be more selective about what you COPY in.

BuildKit has been available in Docker for a while, and is on by default in many installations. If you have at least Docker 19.03, the BuildKit backend does provide a path to do this; see @ChrisJohnson's answer for details.

Upvotes: 5

Related Questions