.dockerignore file with space-only lines breaks build on Windows
Originally reported in https://github.com/docker/compose/issues/5526
diff --git a/docker/api/build.py b/docker/api/build.py
index 9ff2dfb3..34456ab3 100644
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -145,7 +145,9 @@ class BuildApiMixin(object):
             exclude = None
             if os.path.exists(dockerignore):
                 with open(dockerignore, 'r') as f:
-                    exclude = list(filter(bool, f.read().splitlines()))
+                    exclude = list(filter(
+                        bool, [l.strip() for l in f.read().splitlines()]
+                    ))
             context = utils.tar(
                 path, exclude=exclude, dockerfile=dockerfile, gzip=gzip
             )
diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py
index 8e98cc9f..7cc32346 100644
--- a/tests/integration/api_build_test.py
+++ b/tests/integration/api_build_test.py
@@ -352,6 +352,28 @@ class BuildTest(BaseAPIIntegrationTest):
 
         assert 'Successfully built' in lines[-1]['stream']
 
+    def test_build_with_dockerfile_empty_lines(self):
+        base_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, base_dir)
+        with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
+            f.write('FROM busybox\\n')
+        with open(os.path.join(base_dir, '.dockerignore'), 'w') as f:
+            f.write('\\n'.join([
+                '   ',
+                '',
+                '\\t\\t',
+                '\\t     ',
+            ]))
+
+        stream = self.client.build(
+            path=base_dir, stream=True, decode=True, nocache=True
+        )
+
+        lines = []
+        for chunk in stream:
+            lines.append(chunk)
+        assert 'Successfully built' in lines[-1]['stream']
+
     def test_build_gzip_custom_encoding(self):
         with self.assertRaises(errors.DockerException):
             self.client.build(path='.', gzip=True, encoding='text/html')
['tests/integration/api_build_test.py', 'docker/api/build.py']