question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
majkrzakcommented, Apr 14, 2020

@danieladams456 I end up with the PR that they don’t want to merge and this simple polyfill that might help you:

import docker.api.build
docker.api.build.process_dockerfile = lambda dockerfile, path: ('Dockerfile', dockerfile)

And then simply:

client.images.build(path='.',dockerfile='FROM ...')
1reaction
majkrzakcommented, Aug 11, 2018

If dockerfile it’s not part of context for some reason, it have to be affected to be dockerized. I know that it can be passed as absolute path, but if dockerfile is ephemeral it requires saving is somewhere is filesystem which is little more complicated. Generally it py lib lacs functionality of cli, and arguments could be simplified a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to copy file to server on build. Access to path is denied
Running SysInternals procexp: devenv.exe sitting on the file. Unloading project didn't work but after closing solution I could delete System.Web.WebPages.
Read more >
build.py - Google Git
raise TypeError("Either path or fileobj needs to be provided.") if gzip and encoding is not None: raise errors.DockerException(. 'Can not use custom ...
Read more >
python - Should I pass in filenames to be opened, or open files?
No, what you're doing still isn't duck typing. A hasattr(fileobj, 'read') test would be duck typing; an isinstance(fileobj, str) test is not.
Read more >
tarfile — Read and write tar archive files ... - Python Docs
A TarFile object can be used as a context manager in a with statement. It will automatically be closed when the block is...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found