|
13 | 13 | from pathlib import Path |
14 | 14 |
|
15 | 15 | from iso8601 import parse_date |
16 | | -from traitlets import Dict, List, Unicode |
| 16 | +from traitlets import Dict, List, Unicode, default |
17 | 17 |
|
18 | 18 | import docker |
19 | 19 |
|
@@ -66,6 +66,35 @@ class DockerEngine(ContainerEngine): |
66 | 66 |
|
67 | 67 | string_output = True |
68 | 68 |
|
| 69 | + cli = Unicode( |
| 70 | + "", |
| 71 | + help=""" |
| 72 | + The commandline for Docker. |
| 73 | + """, |
| 74 | + config=True, |
| 75 | + ) |
| 76 | + |
| 77 | + @default("cli") |
| 78 | + def _default_cli(self): |
| 79 | + for cli in ["docker", "podman"]: |
| 80 | + docker_version = subprocess.run([cli, "version"]) |
| 81 | + if docker_version.returncode == 0: |
| 82 | + docker_cli = cli |
| 83 | + break |
| 84 | + else: |
| 85 | + raise RuntimeError("The docker or podman commandline client must be installed") |
| 86 | + |
| 87 | + # docker buildx is based in a plugin that might not be installed |
| 88 | + # https://github.com/docker/buildx |
| 89 | + # |
| 90 | + # podman buildx command is an alias of podman build. |
| 91 | + # Not all buildx build features are available in Podman. |
| 92 | + docker_buildx_version = subprocess.run([docker_cli, "buildx", "version"]) |
| 93 | + if docker_buildx_version.returncode: |
| 94 | + raise RuntimeError("The docker buildx plugin must be installed") |
| 95 | + |
| 96 | + return docker_cli |
| 97 | + |
69 | 98 | extra_init_args = Dict( |
70 | 99 | {}, |
71 | 100 | help=""" |
@@ -105,16 +134,7 @@ def build( |
105 | 134 | platform=None, |
106 | 135 | **kwargs, |
107 | 136 | ): |
108 | | - if not shutil.which("docker"): |
109 | | - raise RuntimeError("The docker commandline client must be installed") |
110 | | - |
111 | | - # docker buildx is based in a plugin that might not be installed |
112 | | - # https://github.com/docker/buildx |
113 | | - docker_buildx_version = subprocess.run(["docker", "buildx", "version"]) |
114 | | - if docker_buildx_version.returncode: |
115 | | - raise RuntimeError("The docker buildx plugin must be installed") |
116 | | - |
117 | | - args = ["docker", "buildx", "build", "--progress", "plain"] |
| 137 | + args = [self.cli, "buildx", "build", "--progress", "plain"] |
118 | 138 | if load: |
119 | 139 | if push: |
120 | 140 | raise ValueError( |
@@ -171,7 +191,7 @@ def inspect_image(self, image): |
171 | 191 | Return image configuration if it exists, otherwise None |
172 | 192 | """ |
173 | 193 | proc = subprocess.run( |
174 | | - ["docker", "image", "inspect", image], capture_output=True |
| 194 | + [self.cli, "image", "inspect", image], capture_output=True |
175 | 195 | ) |
176 | 196 |
|
177 | 197 | if proc.returncode != 0: |
@@ -200,7 +220,7 @@ def docker_login(self, username, password, registry): |
200 | 220 | try: |
201 | 221 | subprocess.run( |
202 | 222 | [ |
203 | | - "docker", |
| 223 | + self.cli, |
204 | 224 | "login", |
205 | 225 | "--username", |
206 | 226 | username, |
|
0 commit comments