Skip to content

Commit 795d003

Browse files
authored
fix(docker): allow secrets in container env (#1346)
1 parent 8c7102b commit 795d003

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

alchemy/src/docker/container.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Context } from "../context.ts";
22
import { Resource } from "../resource.ts";
3+
import { Secret } from "../secret.ts";
34
import {
45
DockerApi,
56
normalizeDuration,
@@ -145,7 +146,7 @@ export interface ContainerProps {
145146
/**
146147
* Environment variables
147148
*/
148-
environment?: Record<string, string>;
149+
environment?: Record<string, string | Secret>;
149150

150151
/**
151152
* Port mappings
@@ -413,7 +414,7 @@ export const Container = Resource(
413414
// Create new container
414415
const containerId = await api.createContainer(imageRef, containerName, {
415416
ports: portMappings,
416-
env: props.environment,
417+
env: normalizeEnvironment(props.environment),
417418
volumes: volumeMappings,
418419
cmd: props.command,
419420
healthcheck: props.healthcheck,
@@ -530,7 +531,12 @@ function shouldReplace(
530531
}
531532

532533
// Environment variables
533-
if (!compareEnv(props.environment, containerInfo.Config.Env)) {
534+
if (
535+
!compareEnv(
536+
normalizeEnvironment(props.environment),
537+
containerInfo.Config.Env,
538+
)
539+
) {
534540
return true;
535541
}
536542

@@ -566,6 +572,21 @@ function shouldReplace(
566572
return false;
567573
}
568574

575+
/**
576+
* Unwrap secrets in given environment variables
577+
* @internal
578+
*/
579+
function normalizeEnvironment(
580+
environment: Record<string, string | Secret> | undefined,
581+
): Record<string, string> {
582+
return Object.fromEntries(
583+
Object.entries(environment ?? {}).map(([key, value]) => [
584+
key,
585+
Secret.unwrap(value),
586+
]),
587+
);
588+
}
589+
569590
/**
570591
* Normalize port mappings to a comparable format
571592
* @internal

0 commit comments

Comments
 (0)