Skip to content

Commit c3a66cc

Browse files
committed
Merge pull request #928 from docker/926-ignored_dockerfiles
Never exclude Dockerfile from the build context.
2 parents f368f07 + 90760cf commit c3a66cc

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

docker/utils/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,13 @@ def exclude_paths(root, patterns, dockerfile=None):
128128
paths = get_paths(root, exclude_patterns, include_patterns,
129129
has_exceptions=len(exceptions) > 0)
130130

131-
return set(paths)
131+
return set(paths).union(
132+
# If the Dockerfile is in a subdirectory that is excluded, get_paths
133+
# will not descend into it and the file will be skipped. This ensures
134+
# it doesn't happen.
135+
set([dockerfile])
136+
if os.path.exists(os.path.join(root, dockerfile)) else set()
137+
)
132138

133139

134140
def should_include(path, exclude_patterns, include_patterns):

tests/unit/utils_test.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ class ExcludePathsTest(base.BaseTestCase):
736736
'foo/b.py',
737737
'foo/bar/a.py',
738738
'bar/a.py',
739+
'foo/Dockerfile3',
739740
]
740741

741742
all_paths = set(dirs + files)
@@ -775,6 +776,14 @@ def test_exclude_custom_dockerfile(self):
775776
assert self.exclude(['*'], dockerfile='Dockerfile.alt') == \
776777
set(['Dockerfile.alt', '.dockerignore'])
777778

779+
assert self.exclude(['*'], dockerfile='foo/Dockerfile3') == \
780+
set(['foo/Dockerfile3', '.dockerignore'])
781+
782+
def test_exclude_dockerfile_child(self):
783+
includes = self.exclude(['foo/'], dockerfile='foo/Dockerfile3')
784+
assert 'foo/Dockerfile3' in includes
785+
assert 'foo/a.py' not in includes
786+
778787
def test_single_filename(self):
779788
assert self.exclude(['a.py']) == self.all_paths - set(['a.py'])
780789

@@ -825,28 +834,31 @@ def test_wildcard_subdir_wildcard_filename(self):
825834
def test_directory(self):
826835
assert self.exclude(['foo']) == self.all_paths - set([
827836
'foo', 'foo/a.py', 'foo/b.py',
828-
'foo/bar', 'foo/bar/a.py',
837+
'foo/bar', 'foo/bar/a.py', 'foo/Dockerfile3'
829838
])
830839

831840
def test_directory_with_trailing_slash(self):
832841
assert self.exclude(['foo']) == self.all_paths - set([
833842
'foo', 'foo/a.py', 'foo/b.py',
834-
'foo/bar', 'foo/bar/a.py',
843+
'foo/bar', 'foo/bar/a.py', 'foo/Dockerfile3'
835844
])
836845

837846
def test_directory_with_single_exception(self):
838847
assert self.exclude(['foo', '!foo/bar/a.py']) == self.all_paths - set([
839-
'foo/a.py', 'foo/b.py', 'foo', 'foo/bar'
848+
'foo/a.py', 'foo/b.py', 'foo', 'foo/bar',
849+
'foo/Dockerfile3'
840850
])
841851

842852
def test_directory_with_subdir_exception(self):
843853
assert self.exclude(['foo', '!foo/bar']) == self.all_paths - set([
844-
'foo/a.py', 'foo/b.py', 'foo'
854+
'foo/a.py', 'foo/b.py', 'foo',
855+
'foo/Dockerfile3'
845856
])
846857

847858
def test_directory_with_wildcard_exception(self):
848859
assert self.exclude(['foo', '!foo/*.py']) == self.all_paths - set([
849-
'foo/bar', 'foo/bar/a.py', 'foo'
860+
'foo/bar', 'foo/bar/a.py', 'foo',
861+
'foo/Dockerfile3'
850862
])
851863

852864
def test_subdirectory(self):

0 commit comments

Comments
 (0)