Recursive exceptions to exclusions in .dockerignore not working as expected

324 views Asked by At

I'm dockerizing a PHP-based application. I've got an nginx container containing .js, .html and .css files along with all assets. For the PHP code, I'm using a dedicated PHP Image which is proxied by the nginx server. I would like to only have .php files in that PHP image, without any of the assets or public files.

In order to keep the resulting image as small as possible, but still easily maintain the files copied in my image, I want to make use of the .dockerignore whitelist pattern. Somehow, I can't ignore all files and make exceptions for files in any directory for a specific file type.

In my special case, I want to ignore any file by default. Then, I would like to 'allow' php files. Those are nested in sub directories. It looks like that the .dockerigone doesn't catch these files in ignored directories if they aren't found somehow else.

My .dockerignore is located in app/.

Case 1

Consider the following directory structure:

app
├── index.php
├── controllers
│   ├── controller-1.php
│   ├── controller-2.php
│   └── controller-3.php
└── models
    ├── index.php
    ├── checkout
    │   ├── model-1.php
    │   ├── model-2.php
    │   └── model-3.php
    ├── profile
    │   ├── model-1.php
    │   ├── model-2.php
    │   └── model-3.php
    └── registration
        ├── model-1.php
        ├── model-2.php
        └── model-3.php

And the following .dockerignore file:

# Ignore all files by default.
**

!**/*.php

The container will contain the following files:

app
└── index.php

Case 2

Using a more descriptive .dockerignore will show subdirectories:

# Ignore all files by default.
**

!controllers/*.php
!models/*.php

But still don't lead to the desired result:

app
├── index.php
├── controllers
│   ├── controller-1.php
│   ├── controller-2.php
│   └── controller-3.php
└── models
    └── index.php

Case 3

But if directories are known to the Docker daemon, it works in these but only these directories:

# Ignore all files by default.
**

!**/*.php
!models/checkout/model-1.php

will result in:

app
├── index.php
└── models
    ├── index.php
    └── checkout
        ├── model-1.php
        ├── model-2.php
        └── model-3.php

Question

Does anyone has an idea how to resolve the issue? I know what I'm doing might be a bit more special, but somehow I expect to work like that. The workaround to specifically list all directories I would like to have included. Still, the reverse way works well (excluding a specific file type in any directory like **/.DS_STORE). If there isn't a logic error and there is no clear solution, I would file in a bug on GitHub. Thanks in advance. Otherwise, I would like to extend the docs of Docker.

0

There are 0 answers