Repository: docker/docker-py

Issue:
Building image from different drive is not working
When invoking building image from drive different then location of Dockerfile, the exception appears

```
# Current directory is  D:\GitHub\sc-docker
base_dir = "C:\Users\user\AppData\Roaming/scbw\docker"
# File C:\Users\user\AppData\Roaming/scbw\docker\game.dockerfile is present
docker_client.images.build(path=base_dir, dockerfile="game.dockerfile", tag=local_image)
```

This code produces 

```
  File "D:\github\sc-docker\scbw\docker_utils.py", line 127, in ensure_local_image
    docker_client.images.build(path=base_dir, dockerfile="game.dockerfile", tag=local_image)
  File "d:\github\sc-docker\env\py3.6\lib\site-packages\docker\models\images.py", line 258, in build
    resp = self.client.api.build(**kwargs)
  File "d:\github\sc-docker\env\py3.6\lib\site-packages\docker\api\build.py", line 154, in build
    print(os.path.relpath(dockerfile, path))
  File "d:\github\sc-docker\env\py3.6\lib\ntpath.py", line 585, in relpath
    path_drive, start_drive))
ValueError: path is on mount 'D:', start on mount 'C:'
```

Environment. 
```
docker==3.2.0
Python 3.6.3
Client:
 Version:       18.01.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    03596f51b1
 Built: Thu Jan 11 22:29:41 2018
 OS/Arch:       windows/amd64
 Experimental:  false
 Orchestrator:  swarm

Server:
 Engine:
  Version:      18.01.0-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   03596f5
  Built:        Wed Jan 10 20:13:12 2018
  OS/Arch:      linux/amd64
  Experimental: false
```

The code is same for current version in the master, so issue should be applicable to current version as well.

Original issue is https://github.com/Games-and-Simulations/sc-docker/issues/59


Discussion:

Resolving patch:
diff --git a/docker/api/build.py b/docker/api/build.py
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -149,15 +149,7 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
                         lambda x: x != '' and x[0] != '#',
                         [l.strip() for l in f.read().splitlines()]
                     ))
-            if dockerfile and os.path.relpath(dockerfile, path).startswith(
-                    '..'):
-                with open(dockerfile, 'r') as df:
-                    dockerfile = (
-                        '.dockerfile.{0:x}'.format(random.getrandbits(160)),
-                        df.read()
-                    )
-            else:
-                dockerfile = (dockerfile, None)
+            dockerfile = process_dockerfile(dockerfile, path)
             context = utils.tar(
                 path, exclude=exclude, dockerfile=dockerfile, gzip=gzip
             )
@@ -312,3 +304,22 @@ def _set_auth_headers(self, headers):
             )
         else:
             log.debug('No auth config found')
+
+
+def process_dockerfile(dockerfile, path):
+    if not dockerfile:
+        return (None, None)
+
+    abs_dockerfile = dockerfile
+    if not os.path.isabs(dockerfile):
+        abs_dockerfile = os.path.join(path, dockerfile)
+
+    if (os.path.splitdrive(path)[0] != os.path.splitdrive(abs_dockerfile)[0] or
+            os.path.relpath(abs_dockerfile, path).startswith('..')):
+        with open(abs_dockerfile, 'r') as df:
+            return (
+                '.dockerfile.{0:x}'.format(random.getrandbits(160)),
+                df.read()
+            )
+    else:
+        return (dockerfile, None)


Test patch:
diff --git a/tests/integration/api_build_test.py b/tests/integration/api_build_test.py
--- a/tests/integration/api_build_test.py
+++ b/tests/integration/api_build_test.py
@@ -440,3 +440,35 @@ def test_build_out_of_context_dockerfile(self):
         lsdata = self.client.logs(ctnr).strip().split(b'\n')
         assert len(lsdata) == 3
         assert sorted([b'.', b'..', b'file.txt']) == sorted(lsdata)
+
+    def test_build_in_context_dockerfile(self):
+        base_dir = tempfile.mkdtemp()
+        self.addCleanup(shutil.rmtree, base_dir)
+        with open(os.path.join(base_dir, 'file.txt'), 'w') as f:
+            f.write('hello world')
+        with open(os.path.join(base_dir, 'custom.dockerfile'), 'w') as df:
+            df.write('\n'.join([
+                'FROM busybox',
+                'COPY . /src',
+                'WORKDIR /src',
+            ]))
+        print(os.path.join(base_dir, 'custom.dockerfile'))
+        img_name = random_name()
+        self.tmp_imgs.append(img_name)
+        stream = self.client.build(
+            path=base_dir, dockerfile='custom.dockerfile', tag=img_name,
+            decode=True
+        )
+        lines = []
+        for chunk in stream:
+            lines.append(chunk)
+        assert 'Successfully tagged' in lines[-1]['stream']
+
+        ctnr = self.client.create_container(img_name, 'ls -a')
+        self.tmp_containers.append(ctnr)
+        self.client.start(ctnr)
+        lsdata = self.client.logs(ctnr).strip().split(b'\n')
+        assert len(lsdata) == 4
+        assert sorted(
+            [b'.', b'..', b'file.txt', b'custom.dockerfile']
+        ) == sorted(lsdata)