Dockerignore: allow to add only specific extension

2019-03-01 14:19发布

I have a .dockerignore file and I'm trying to allow Docker to upload only *.json files but from any of subfolders.

For example, for the next files structure:

public/readme.md
public/subfolder/a.json
public/subfolder/b.json
public/other/c.json
public/other/file.txt

I'm expecting to see only json files in the image:

public/subfolder/a.json
public/subfolder/b.json
public/other/c.json

Of course they must be located in the same directories as in original source.

I tried several ways but didn't succeed.

UP: I don't know how many subfolders will be created in the public/ directory and how deep will be the directories structure.

1条回答
劳资没心,怎么记你
2楼-- · 2019-03-01 14:43

I think you can achieve what you want by relying on one such .dockerignore:

public/*
!public/subfolder
public/subfolder/*
!public/other
public/other/*
!**/*.json

The tricky thing is that the first line of this file is public/* but not public nor * (otherwise the !... subsequent lines won't work).

Note also that you may want to automate the generation of one such .dockerignore, to cope with possible tree structure changes.

For example:

gen-dockerignore.sh

#!/usr/bin/env bash

{ echo '*' ;  # header of the .dockerignore - to be changed if need be
  find public -type d -exec echo -en "!{}\n{}/*\n" \; ;
  echo '!**/*.json' ; } > .dockerignore

$ ./gen-dockerignore.sh would output the following file:

.dockerignore

*
!public
public/*
!public/other
public/other/*
!public/subfolder
public/subfolder/*
!**/*.json
查看更多
登录 后发表回答