MCPcopy Create free account
hub / github.com/conda/constructor / build_image

Function build_image

constructor/docker_build.py:88–146  ·  view source on GitHub ↗

Optionally build the docker image from the generated Dockerfile. Target platform must be linux. Parameters ---------- info: dict Constructor installer info dict. docker_dir: Path Path to the Docker directory containing the Docker artifacts. Returns -----

(info: dict, docker_dir: Path)

Source from the content-addressed store, hash-verified

86
87
88def build_image(info: dict, docker_dir: Path) -> Path:
89 """Optionally build the docker image from the generated Dockerfile.
90 Target platform must be linux.
91
92 Parameters
93 ----------
94 info: dict
95 Constructor installer info dict.
96 docker_dir: Path
97 Path to the Docker directory containing the Docker artifacts.
98
99 Returns
100 -------
101 Path
102 Path to the saved Docker image tarball.
103
104 """
105 if not (docker_platform := DOCKER_PLATFORM_MAP.get(info["_platform"])):
106 raise RuntimeError(
107 f"Unsupported platform for Docker build: {info['_platform']}. "
108 f"Supported platforms are: {', '.join(DOCKER_PLATFORM_MAP.keys())}."
109 )
110
111 tag = info.get("docker_tag", f"{info['name'].lower()}:{info['version']}")
112 tarball_dest = docker_dir / f"{Path(info['_outpath']).stem}-docker.tar"
113
114 cmd = [
115 "docker",
116 "buildx",
117 "build",
118 str(docker_dir),
119 "--platform",
120 docker_platform,
121 "-t",
122 tag,
123 "--load",
124 ]
125
126 logger.info("Building Docker image: '%s'", tag)
127 try:
128 subprocess.run(cmd, check=True)
129 except subprocess.CalledProcessError as e:
130 # Gather diagnostics on failure
131 docker_version = subprocess.run(["docker", "--version"], capture_output=True, text=True)
132 buildx_version = subprocess.run(
133 ["docker", "buildx", "version"], capture_output=True, text=True
134 )
135 buildx_ls = subprocess.run(["docker", "buildx", "ls"], capture_output=True, text=True)
136 raise RuntimeError(
137 f"Docker build failed.\n"
138 f"Docker version: {docker_version.stdout.strip()}\n"
139 f"Buildx version: {buildx_version.stdout.strip() or buildx_version.stderr.strip()}\n"
140 f"Buildx builders: {buildx_ls.stdout.strip()}"
141 ) from e
142
143 logger.info("Saving Docker image to tarball: '%s'", tarball_dest)
144 subprocess.run(["docker", "save", tag, "-o", str(tarball_dest)], check=True)
145 subprocess.run(["docker", "rmi", tag], check=False)

Callers 1

createFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected