Skip to content

Commit d0d87c8

Browse files
committed
Shell out to docker buildx build to build images
pydocker uses the HTTP API (equivalent to `docker build`) which has been dead for ages. `docker buildx` (the interface to BuildKit) is what we *should* be using, but alas pydocker does not currently support it and I suspect it never will (docker/docker-py#2230). This PR simply shells out, as I think that's the way. Fixes #875
1 parent dd097a2 commit d0d87c8

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

repo2docker/docker.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
Docker container engine for repo2docker
33
"""
44

5+
import subprocess
6+
import tarfile
7+
import tempfile
8+
from queue import Empty, Queue
9+
from threading import Thread
10+
511
from iso8601 import parse_date
612
from traitlets import Dict
713

814
import docker
915

1016
from .engine import Container, ContainerEngine, ContainerEngineException, Image
17+
from .utils import execute_cmd
1118

1219

1320
class DockerContainer(Container):
@@ -53,7 +60,7 @@ class DockerEngine(ContainerEngine):
5360
https://docker-py.readthedocs.io/en/4.2.0/api.html#module-docker.api.build
5461
"""
5562

56-
string_output = False
63+
string_output = True
5764

5865
extra_init_args = Dict(
5966
{},
@@ -82,8 +89,8 @@ def __init__(self, *, parent):
8289
def build(
8390
self,
8491
*,
85-
buildargs=None,
86-
cache_from=None,
92+
buildargs: dict | None = None,
93+
cache_from: list[str] | None = None,
8794
container_limits=None,
8895
tag="",
8996
custom_context=False,
@@ -94,22 +101,29 @@ def build(
94101
platform=None,
95102
**kwargs,
96103
):
97-
return self._apiclient.build(
98-
buildargs=buildargs,
99-
cache_from=cache_from,
100-
container_limits=container_limits,
101-
forcerm=True,
102-
rm=True,
103-
tag=tag,
104-
custom_context=custom_context,
105-
decode=True,
106-
dockerfile=dockerfile,
107-
fileobj=fileobj,
108-
path=path,
109-
labels=labels,
110-
platform=platform,
111-
**kwargs,
112-
)
104+
args = ["docker", "buildx", "build", "--progress", "plain"]
105+
if buildargs:
106+
for k, v in buildargs.items():
107+
args += ["--build-arg", f"{k}={v}"]
108+
109+
if cache_from:
110+
for cf in cache_from:
111+
args += ["--cache-from", cf]
112+
113+
if dockerfile:
114+
args += ["--file", dockerfile]
115+
116+
if tag:
117+
args += ["--tag", tag]
118+
119+
if fileobj:
120+
with tempfile.TemporaryDirectory() as d:
121+
tarf = tarfile.open(fileobj=fileobj)
122+
tarf.extractall(d)
123+
124+
args += [d]
125+
126+
yield from execute_cmd(args, True)
113127

114128
def images(self):
115129
images = self._apiclient.images()

0 commit comments

Comments
 (0)