The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Docker from "@alchemy.run/docker"; |
| 2 | import { DockerEnvironment } from "@alchemy.run/docker/Docker"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import { inMemoryState, State } from "alchemy/State"; |
| 6 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as Redacted from "effect/Redacted"; |
| 11 | import { authenticatedRegistry, scopedBuildx } from "./Runtime.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Docker.providers(), state: inMemoryState() }); |
| 14 | |
| 15 | describe( |
| 16 | "Docker.Image", |
| 17 | { tags: ["provider:docker", "provider:docker:image", "local"], concurrent: false }, |
| 18 | () => { |
| 19 | test.provider("plans an update when the Docker context changes", (stack) => |
| 20 | Effect.gen(function* () { |
| 21 | const fs = yield* FileSystem.FileSystem; |
| 22 | const path = yield* Path.Path; |
| 23 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-context-plan-" }); |
| 24 | yield* fs.writeFileString(path.join(root, "Dockerfile"), "FROM scratch\n"); |
| 25 | |
| 26 | const base = Docker.Image("context-image", { |
| 27 | tag: "latest", |
| 28 | context: "default", |
| 29 | build: { context: root }, |
| 30 | }); |
| 31 | const changed = Docker.Image("context-image", { |
| 32 | tag: "latest", |
| 33 | context: "remote-build", |
| 34 | build: { context: root }, |
| 35 | }); |
| 36 | |
| 37 | yield* stack.deploy(base); |
| 38 | const plan = yield* stack.plan(changed); |
| 39 | expect(plan.resources["context-image"]).toMatchObject({ action: "update" }); |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | test.provider("plans an image whose context is missing from the local docker", (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | const docker = yield* Docker.Docker; |
| 46 | const fs = yield* FileSystem.FileSystem; |
| 47 | const path = yield* Path.Path; |
| 48 | const root = yield* fs.makeTempDirectoryScoped({ |
| 49 | prefix: "alchemy-docker-missing-context-", |
| 50 | }); |
| 51 | yield* fs.writeFileString( |
| 52 | path.join(root, "Dockerfile"), |
| 53 | "FROM scratch\nLABEL alchemy.test=missing-context\n", |
| 54 | ); |
| 55 | |
| 56 | const endpoint = (yield* docker.run([ |
| 57 | "context", |
| 58 | "inspect", |
| 59 | "--format", |
| 60 | "{{.Endpoints.docker.Host}}", |
| 61 | ])).stdout.trim(); |
| 62 | |
| 63 | const program = Effect.gen(function* () { |
| 64 | const context = yield* Docker.Context("machine-local-context", { |
| 65 | docker: `host=${endpoint}`, |
| 66 | }); |
| 67 | const image = yield* Docker.Image("machine-local-image", { |
| 68 | tag: "latest", |
| 69 | context, |
| 70 | build: { context: root }, |
| 71 | }); |
| 72 | return { context, image }; |
| 73 | }); |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | const first = yield* stack.deploy(program); |
| 77 | yield* docker.context.remove(first.context.name, true); |
| 78 | |
| 79 | const plan = yield* stack.plan(program); |
| 80 | expect(plan.resources["machine-local-context"]).toMatchObject({ action: "update" }); |
| 81 | expect(plan.resources["machine-local-image"]).toMatchObject({ action: "update" }); |
| 82 | |
| 83 | const second = yield* stack.deploy(program); |
| 84 | expect(second.image.imageRef).toBe(first.image.imageRef); |
| 85 | const live = yield* docker.context.inspect(first.context.name); |
| 86 | expect(live.Name).toBe(first.context.name); |
| 87 | |
| 88 | yield* stack.destroy(); |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | test.provider("builds a tiny Dockerfile with an auto-generated name", (stack) => |
| 93 | Effect.gen(function* () { |
| 94 | const fs = yield* FileSystem.FileSystem; |
| 95 | const path = yield* Path.Path; |
| 96 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-image-" }); |
| 97 | yield* fs.writeFileString( |
| 98 | path.join(root, "Dockerfile"), |
| 99 | "FROM scratch\nLABEL alchemy.test=true\n", |
| 100 | ); |
| 101 | // No explicit name: the engine auto-generates the physical name. |
| 102 | const image = yield* stack.deploy( |
| 103 | Docker.Image("tiny-image", { tag: "latest", build: { context: root } }), |
| 104 | ); |
| 105 | expect(image.imageRef.endsWith(":latest")).toBe(true); |
| 106 | expect(image.imageId.length).toBeGreaterThan(0); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | test.provider("updates when the build context changes", (stack) => |
| 111 | Effect.gen(function* () { |
| 112 | const fs = yield* FileSystem.FileSystem; |
| 113 | const path = yield* Path.Path; |
| 114 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-canary-" }); |
| 115 | yield* fs.writeFileString(path.join(root, "Dockerfile"), "FROM scratch\n"); |
| 116 | yield* fs.writeFileString( |
| 117 | path.join(root, "Dockerfile"), |
| 118 | "FROM scratch\nLABEL alchemy.test=1\n", |
| 119 | ); |
| 120 | |
| 121 | const makeStack = Docker.Image("tiny-image", { tag: "latest", build: { context: root } }); |
| 122 | |
| 123 | yield* stack.deploy(makeStack); |
| 124 | const plan1 = yield* stack.plan(makeStack); |
| 125 | expect(plan1.resources["tiny-image"]).toMatchObject({ action: "noop" }); |
| 126 | yield* fs.writeFileString( |
| 127 | path.join(root, "Dockerfile"), |
| 128 | "FROM scratch\nLABEL alchemy.test=2\n", |
| 129 | ); |
| 130 | const plan2 = yield* stack.plan(makeStack); |
| 131 | expect(plan2.resources["tiny-image"]).toMatchObject({ action: "update" }); |
| 132 | }), |
| 133 | ); |
| 134 | |
| 135 | test.provider("builds with an explicit repository name and tag", (stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const fs = yield* FileSystem.FileSystem; |
| 138 | const path = yield* Path.Path; |
| 139 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-image-named-" }); |
| 140 | yield* fs.writeFileString( |
| 141 | path.join(root, "Dockerfile"), |
| 142 | "FROM scratch\nLABEL alchemy.test=named\n", |
| 143 | ); |
| 144 | const image = yield* stack.deploy( |
| 145 | Docker.Image("named-image", { |
| 146 | name: "alchemy-test-named", |
| 147 | tag: "v1", |
| 148 | build: { context: root }, |
| 149 | }), |
| 150 | ); |
| 151 | expect(image.name).toBe("alchemy-test-named"); |
| 152 | expect(image.imageRef).toBe("alchemy-test-named:v1"); |
| 153 | expect(image.tag).toBe("v1"); |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | test.provider("rebuilds when the build context changes", (stack) => |
| 158 | Effect.gen(function* () { |
| 159 | const fs = yield* FileSystem.FileSystem; |
| 160 | const path = yield* Path.Path; |
| 161 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-image-rebuild-" }); |
| 162 | const dockerfile = path.join(root, "Dockerfile"); |
| 163 | |
| 164 | yield* fs.writeFileString(dockerfile, "FROM scratch\nLABEL gen=1\n"); |
| 165 | const first = yield* stack.deploy( |
| 166 | Docker.Image("rebuilt-image", { tag: "latest", build: { context: root } }), |
| 167 | ); |
| 168 | |
| 169 | yield* fs.writeFileString(dockerfile, "FROM scratch\nLABEL gen=2\n"); |
| 170 | const second = yield* stack.deploy( |
| 171 | Docker.Image("rebuilt-image", { tag: "latest", build: { context: root } }), |
| 172 | ); |
| 173 | |
| 174 | expect(second.imageRef).toBe(first.imageRef); |
| 175 | }), |
| 176 | ); |
| 177 | |
| 178 | // A build failing inside a `RUN` step must say why, not just its exit |
| 179 | // code: the step's own output has to reach the deploy error. BuildKit |
| 180 | // logs steps to stderr; the legacy builder (no Buildx plugin, or |
| 181 | // `DOCKER_BUILDKIT=0`) logs them to stdout and only the exit reason to |
| 182 | // stderr. |
| 183 | for (const [builder, buildkit] of [ |
| 184 | ["BuildKit", undefined], |
| 185 | ["the legacy builder", "0"], |
| 186 | ] as const) { |
| 187 | test.provider( |
| 188 | `reports the failing RUN step's output when a build fails with ${builder}`, |
| 189 | (stack) => |
| 190 | Effect.gen(function* () { |
| 191 | const fs = yield* FileSystem.FileSystem; |
| 192 | const path = yield* Path.Path; |
| 193 | const root = yield* fs.makeTempDirectoryScoped({ |
| 194 | prefix: "alchemy-docker-image-fail-", |
| 195 | }); |
| 196 | yield* fs.writeFileString( |
| 197 | path.join(root, "Dockerfile"), |
| 198 | // Computed in the step so only the step's output, never the echoed |
| 199 | // command, contains the expected text. |
| 200 | 'FROM alpine:3.19\nRUN echo "npm ERR! missing script: build-$((40 + 2))" && exit 3\n', |
| 201 | ); |
| 202 | yield* Effect.acquireRelease( |
| 203 | Effect.sync(() => { |
| 204 | const previous = process.env.DOCKER_BUILDKIT; |
| 205 | if (buildkit === undefined) delete process.env.DOCKER_BUILDKIT; |
| 206 | else process.env.DOCKER_BUILDKIT = buildkit; |
| 207 | return previous; |
| 208 | }), |
| 209 | (previous) => |
| 210 | Effect.sync(() => { |
| 211 | if (previous === undefined) delete process.env.DOCKER_BUILDKIT; |
| 212 | else process.env.DOCKER_BUILDKIT = previous; |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | const error = yield* stack |
| 217 | .deploy(Docker.Image("failing-image", { tag: "latest", build: { context: root } })) |
| 218 | .pipe(Effect.flip); |
| 219 | |
| 220 | const report = yield* Effect.sync(() => String(error) + JSON.stringify(error)); |
| 221 | expect(report).toContain("npm ERR! missing script: build-42"); |
| 222 | }), |
| 223 | // Mutates `process.env.DOCKER_BUILDKIT`. |
| 224 | { exclusive: true }, |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | test.provider("builds FROM a private base image with the registry credentials", (stack) => |
| 229 | Effect.gen(function* () { |
| 230 | const docker = yield* Docker.Docker; |
| 231 | const fs = yield* FileSystem.FileSystem; |
| 232 | const path = yield* Path.Path; |
| 233 | // Anonymous pulls get 401, and nothing on the host holds a |
| 234 | // `docker login` for this ephemeral port. |
| 235 | const { host, credentials: registry } = yield* authenticatedRegistry(); |
| 236 | const baseRef = `${host}/alchemy-base:v1`; |
| 237 | yield* Effect.addFinalizer(() => |
| 238 | docker.image.remove([baseRef, `${host}/alchemy-app:v1`], true).pipe(Effect.ignore), |
| 239 | ); |
| 240 | |
| 241 | // Publish the private base image, then drop the local copy so the |
| 242 | // build below has to pull it from the authenticated registry. |
| 243 | yield* stack.deploy( |
| 244 | Docker.RemoteImage("private-base", { |
| 245 | name: "busybox", |
| 246 | tag: "latest", |
| 247 | targetName: `${host}/alchemy-base`, |
| 248 | targetTag: "v1", |
| 249 | registry, |
| 250 | }), |
| 251 | ); |
| 252 | yield* docker.image.remove(baseRef, true); |
| 253 | |
| 254 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-private-base-" }); |
| 255 | yield* fs.writeFileString( |
| 256 | path.join(root, "Dockerfile"), |
| 257 | `FROM ${baseRef}\nLABEL alchemy.test=private-base\n`, |
| 258 | ); |
| 259 | |
| 260 | const image = yield* stack.deploy( |
| 261 | Docker.Image("private-base-app", { |
| 262 | name: `${host}/alchemy-app`, |
| 263 | tag: "v1", |
| 264 | registry, |
| 265 | build: { context: root }, |
| 266 | }), |
| 267 | ); |
| 268 | expect(image.imageRef).toBe(`${host}/alchemy-app:v1`); |
| 269 | expect(image.repoDigest).toContain(`${host}/alchemy-app@sha256:`); |
| 270 | }), |
| 271 | ); |
| 272 | |
| 273 | // Publish `busybox` as a private base image, then drop the local copy so |
| 274 | // a build has to pull it back through the registry's auth. |
| 275 | const publishPrivateBase = ( |
| 276 | host: string, |
| 277 | credentials: Docker.RegistryCredentials, |
| 278 | repository: string, |
| 279 | ) => |
| 280 | Effect.gen(function* () { |
| 281 | const docker = yield* Docker.Docker; |
| 282 | const ref = `${host}/${repository}:v1`; |
| 283 | yield* docker.image.pull("busybox:latest"); |
| 284 | yield* docker.image.tag("busybox:latest", ref); |
| 285 | yield* docker.image.push(ref, credentials); |
| 286 | yield* docker.image.remove(ref, true); |
| 287 | yield* Effect.addFinalizer(() => docker.image.remove(ref, true).pipe(Effect.ignore)); |
| 288 | return ref; |
| 289 | }); |
| 290 | |
| 291 | const dockerfileContext = (dockerfile: string) => |
| 292 | Effect.gen(function* () { |
| 293 | const fs = yield* FileSystem.FileSystem; |
| 294 | const path = yield* Path.Path; |
| 295 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-docker-build-auth-" }); |
| 296 | yield* fs.writeFileString(path.join(root, "Dockerfile"), dockerfile); |
| 297 | return root; |
| 298 | }); |
| 299 | |
| 300 | test.provider("authenticates the build but does not push when skipPush is set", (stack) => |
| 301 | Effect.gen(function* () { |
| 302 | const registry = yield* authenticatedRegistry(); |
| 303 | const baseRef = yield* publishPrivateBase(registry.host, registry.credentials, "skip-base"); |
| 304 | const root = yield* dockerfileContext(`FROM ${baseRef}\nLABEL alchemy.test=skip-push\n`); |
| 305 | |
| 306 | yield* stack.deploy( |
| 307 | Docker.Image("skip-push-app", { |
| 308 | name: `${registry.host}/skip-push-app`, |
| 309 | tag: "v1", |
| 310 | registry: registry.credentials, |
| 311 | skipPush: true, |
| 312 | build: { context: root }, |
| 313 | }), |
| 314 | ); |
| 315 | expect(yield* registry.hasManifest("skip-push-app", "v1")).toBe(false); |
| 316 | }), |
| 317 | ); |
| 318 | |
| 319 | test.provider("fails the build with 401 when the registry credentials are wrong", (stack) => |
| 320 | Effect.gen(function* () { |
| 321 | const registry = yield* authenticatedRegistry(); |
| 322 | const baseRef = yield* publishPrivateBase( |
| 323 | registry.host, |
| 324 | registry.credentials, |
| 325 | "wrong-base", |
| 326 | ); |
| 327 | const root = yield* dockerfileContext(`FROM ${baseRef}\n`); |
| 328 | |
| 329 | const error = yield* stack |
| 330 | .deploy( |
| 331 | Docker.Image("wrong-credentials-app", { |
| 332 | name: `${registry.host}/wrong-credentials-app`, |
| 333 | tag: "v1", |
| 334 | registry: { ...registry.credentials, password: Redacted.make("not-the-password") }, |
| 335 | build: { context: root }, |
| 336 | }), |
| 337 | ) |
| 338 | .pipe(Effect.flip); |
| 339 | const report = yield* Effect.sync(() => String(error) + JSON.stringify(error)); |
| 340 | expect(report).toMatch(/401|unauthorized/i); |
| 341 | expect(report).not.toContain("not-the-password"); |
| 342 | expect(yield* registry.hasManifest("wrong-credentials-app", "v1")).toBe(false); |
| 343 | }), |
| 344 | ); |
| 345 | |
| 346 | test.provider("keeps existing DOCKER_AUTH_CONFIG credentials for other registries", (stack) => |
| 347 | Effect.gen(function* () { |
| 348 | // Base A lives behind credentials the user already has in |
| 349 | // DOCKER_AUTH_CONFIG; base B behind the Image's own `registry`. |
| 350 | const a = yield* authenticatedRegistry(); |
| 351 | const b = yield* authenticatedRegistry(); |
| 352 | const baseA = yield* publishPrivateBase(a.host, a.credentials, "base-a"); |
| 353 | const baseB = yield* publishPrivateBase(b.host, b.credentials, "base-b"); |
| 354 | const auth = yield* Effect.sync(() => |
| 355 | Buffer.from("alchemy:alchemy-test-password").toString("base64"), |
| 356 | ); |
| 357 | const root = yield* dockerfileContext( |
| 358 | `FROM ${baseA} AS a\nFROM ${baseB}\nCOPY --from=a /bin/busybox /from-a\n`, |
| 359 | ); |
| 360 | |
| 361 | // As if exported before starting Alchemy. |
| 362 | const image = yield* stack |
| 363 | .deploy( |
| 364 | Docker.Image("two-registries-app", { |
| 365 | name: `${b.host}/two-registries-app`, |
| 366 | tag: "v1", |
| 367 | registry: b.credentials, |
| 368 | build: { context: root }, |
| 369 | }), |
| 370 | ) |
| 371 | .pipe( |
| 372 | Effect.provide( |
| 373 | ConfigProvider.layer( |
| 374 | ConfigProvider.fromUnknown({ |
| 375 | DOCKER_AUTH_CONFIG: JSON.stringify({ auths: { [a.host]: { auth } } }), |
| 376 | }), |
| 377 | ), |
| 378 | ), |
| 379 | ); |
| 380 | expect(image.repoDigest).toContain(`${b.host}/two-registries-app@sha256:`); |
| 381 | }), |
| 382 | ); |
| 383 | |
| 384 | test.provider("merges a DockerEnvironment auth config with the process one", (stack) => |
| 385 | Effect.gen(function* () { |
| 386 | // Base A behind the user's own DOCKER_AUTH_CONFIG, base C behind one a |
| 387 | // provider passes through DockerEnvironment, push to B's `registry`. |
| 388 | const a = yield* authenticatedRegistry(); |
| 389 | const b = yield* authenticatedRegistry(); |
| 390 | const c = yield* authenticatedRegistry(); |
| 391 | const baseA = yield* publishPrivateBase(a.host, a.credentials, "base-a"); |
| 392 | const baseC = yield* publishPrivateBase(c.host, c.credentials, "base-c"); |
| 393 | const auth = yield* Effect.sync(() => |
| 394 | Buffer.from("alchemy:alchemy-test-password").toString("base64"), |
| 395 | ); |
| 396 | const root = yield* dockerfileContext( |
| 397 | `FROM ${baseA} AS a\nFROM ${baseC}\nCOPY --from=a /bin/busybox /from-a\n`, |
| 398 | ); |
| 399 | |
| 400 | const image = yield* stack |
| 401 | .deploy( |
| 402 | Docker.Image("merged-auth-app", { |
| 403 | name: `${b.host}/merged-auth-app`, |
| 404 | tag: "v1", |
| 405 | registry: b.credentials, |
| 406 | build: { context: root }, |
| 407 | }), |
| 408 | ) |
| 409 | .pipe( |
| 410 | Effect.provideService(DockerEnvironment, { |
| 411 | DOCKER_AUTH_CONFIG: JSON.stringify({ auths: { [c.host]: { auth } } }), |
| 412 | }), |
| 413 | Effect.provide( |
| 414 | ConfigProvider.layer( |
| 415 | ConfigProvider.fromUnknown({ |
| 416 | DOCKER_AUTH_CONFIG: JSON.stringify({ auths: { [a.host]: { auth } } }), |
| 417 | }), |
| 418 | ), |
| 419 | ), |
| 420 | ); |
| 421 | expect(image.repoDigest).toContain(`${b.host}/merged-auth-app@sha256:`); |
| 422 | }), |
| 423 | ); |
| 424 | |
| 425 | test.provider("never writes the registry password to state", (stack) => |
| 426 | Effect.gen(function* () { |
| 427 | const registry = yield* authenticatedRegistry(); |
| 428 | const baseRef = yield* publishPrivateBase( |
| 429 | registry.host, |
| 430 | registry.credentials, |
| 431 | "state-base", |
| 432 | ); |
| 433 | const root = yield* dockerfileContext(`FROM ${baseRef}\n`); |
| 434 | yield* stack.deploy( |
| 435 | Docker.Image("state-app", { |
| 436 | name: `${registry.host}/state-app`, |
| 437 | tag: "v1", |
| 438 | registry: registry.credentials, |
| 439 | build: { context: root }, |
| 440 | }), |
| 441 | ); |
| 442 | const state = yield* yield* State; |
| 443 | const fqns = yield* state.list({ stack: stack.name, stage: stack.stage }); |
| 444 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 445 | state.get({ stack: stack.name, stage: stack.stage, fqn }), |
| 446 | ); |
| 447 | const persisted = yield* Effect.sync(() => JSON.stringify(rows)); |
| 448 | expect(persisted).not.toContain("alchemy-test-password"); |
| 449 | }), |
| 450 | ); |
| 451 | |
| 452 | // The legacy builder (no Buildx plugin) also honors DOCKER_AUTH_CONFIG. |
| 453 | // Buildx < 0.26 does not; that limit is documented on `registry`. |
| 454 | for (const [builder, version] of [["without a Buildx plugin", undefined]] as const) { |
| 455 | test.provider( |
| 456 | `builds FROM a private base image ${builder}`, |
| 457 | (stack) => |
| 458 | Effect.gen(function* () { |
| 459 | const registry = yield* authenticatedRegistry(); |
| 460 | const baseRef = yield* publishPrivateBase( |
| 461 | registry.host, |
| 462 | registry.credentials, |
| 463 | `builder-base-${version ?? "legacy"}`, |
| 464 | ); |
| 465 | yield* scopedBuildx(version); |
| 466 | const root = yield* dockerfileContext(`FROM ${baseRef}\n`); |
| 467 | const image = yield* stack.deploy( |
| 468 | Docker.Image(`builder-app-${version ?? "legacy"}`, { |
| 469 | name: `${registry.host}/builder-app`, |
| 470 | tag: version ?? "legacy", |
| 471 | registry: registry.credentials, |
| 472 | build: { context: root }, |
| 473 | }), |
| 474 | ); |
| 475 | expect(image.repoDigest).toContain(`${registry.host}/builder-app@sha256:`); |
| 476 | }), |
| 477 | // Mutates `process.env.DOCKER_CONFIG` / `DOCKER_HOST`. |
| 478 | { exclusive: true, timeout: 180_000 }, |
| 479 | ); |
| 480 | } |
| 481 | }, |
| 482 | ); |