docker / docker/docker-py

Can not pass `fileobj` together with `path` context to `build`

Open
#2,105 12 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

It seems to be impossible to pass ephemeral Dockerfile to build command together with build context. Here is command line use case:

docker build -t . -f-<<EOF
Lorem ipsum docker file
EOF

According to the documentation https://docker-py.readthedocs.io/en/stable/images.htmlit should be possible to pass Dockerfile as file object, but in current implementation path param with context is ignored.

Here is path that fix part of this issue (it don't work when path.startswith(('http://', 'https://','git://', 'github.com/', 'git@')))

diff --git a/docker/api/build.py b/docker/api/build.py
index 0486dce..711def2 100644
--- a/docker/api/build.py
+++ b/docker/api/build.py
@@ -133,7 +133,7 @@ class BuildApiMixin(object):
            if not fileobj:
                raise TypeError("You must specify fileobj with custom_context")
            context = fileobj
-        elif fileobj is not None:
+        elif path is None and fileobj is not None:
            context = utils.mkbuildcontext(fileobj)
        elif path.startswith(('http://', 'https://',
                            'git://', 'github.com/', 'git@')):
@@ -149,7 +149,7 @@ class BuildApiMixin(object):
                        lambda x: x != '' and x[0] != '#',
                        [l.strip() for l in f.read().splitlines()]
                    ))
-            dockerfile = process_dockerfile(dockerfile, path)
+            dockerfile = process_dockerfile(dockerfile, path, fileobj)
            context = utils.tar(
                path, exclude=exclude, dockerfile=dockerfile, gzip=gzip
            )
@@ -332,7 +332,13 @@ class BuildApiMixin(object):
            log.debug('No auth config found')


-def process_dockerfile(dockerfile, path):
+def process_dockerfile(dockerfile, path, fileobj):
+    if fileobj is not None:
+        return (
+            '.dockerfile.{0:x}'.format(random.getrandbits(160)),
+            fileobj.read()
+        )
+
    if not dockerfile:
        return (None, None)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in docker/api/build.py at BuildApiMixin.build and process_dockerfile, then compare their behavior with the linked build documentation. Verify that a fileobj Dockerfile can be used alongside a path context, including the remote path forms mentioned in the issue; done means the build command accepts both inputs without ignoring either.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
build-system, devops
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.