Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e33be9f

Browse files
vicbemily-shen
andcommitted
sync local containers with latest workerd
Co-authored-by: emily-shen <[email protected]>
1 parent 70ba9fb commit e33be9f

File tree

20 files changed

+247
-63
lines changed

20 files changed

+247
-63
lines changed

.changeset/weak-months-speak.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": patch
3+
"wrangler": patch
4+
---
5+
6+
Add core local dev functionality for containers.
7+
Adds a new WRANGLER_DOCKER_HOST env var to customise what socket to connect to.

packages/miniflare/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ function getDurableObjectClassNames(
344344
enableSql,
345345
unsafeUniqueKey,
346346
unsafePreventEviction,
347+
container,
347348
} = normaliseDurableObject(designator);
348349
// Get or create `Map` mapping class name to optional unsafe unique key
349350
let classNames = serviceClassNames.get(serviceName);
@@ -385,6 +386,7 @@ function getDurableObjectClassNames(
385386
enableSql,
386387
unsafeUniqueKey,
387388
unsafePreventEviction,
389+
container,
388390
});
389391
}
390392
}

packages/miniflare/src/plugins/containers/service/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export class ContainerController {
2626
this.#containerOptions = containerOptions;
2727
this.#sharedOptions = sharedOptions;
2828
this.#logger = logger;
29+
if (process.platform === "win32") {
30+
throw new Error(
31+
"Local development with containers is currently not supported on Windows. You should use WSL instead. You can also set `enable_containers` to false if you do not need to develop the container part of your application."
32+
);
33+
}
2934
}
3035

3136
updateConfig(

packages/miniflare/src/plugins/core/index.ts

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "assert";
22
import { readFileSync } from "fs";
33
import fs from "fs/promises";
4+
import { platform } from "node:os";
45
import path from "path";
56
import { Readable } from "stream";
67
import tls from "tls";
@@ -18,6 +19,7 @@ import {
1819
ServiceDesignator,
1920
supportedCompatibilityDate,
2021
Worker_Binding,
22+
Worker_ContainerEngine,
2123
Worker_DurableObjectNamespace,
2224
Worker_Module,
2325
} from "../../runtime";
@@ -168,6 +170,16 @@ const CoreOptionsSchemaInput = z.intersection(
168170
// There is an issue with the connect() API and the globalOutbound workerd setting that impacts TCP ingress
169171
// We should default it to true once https://github.com/cloudflare/workerd/pull/4145 is resolved
170172
stripCfConnectingIp: z.boolean().default(false),
173+
174+
/** Configuration used to connect to the container engine */
175+
containerEngine: z
176+
.union([
177+
z.object({
178+
localDocker: z.object({ socketPath: z.string() }),
179+
}),
180+
z.string(),
181+
])
182+
.optional(),
171183
})
172184
);
173185
export const CoreOptionsSchema = CoreOptionsSchemaInput.transform((value) => {
@@ -738,28 +750,32 @@ export const CORE_PLUGIN: Plugin<
738750
classNamesEntries.map<Worker_DurableObjectNamespace>(
739751
([
740752
className,
741-
{ enableSql, unsafeUniqueKey, unsafePreventEviction },
742-
]) => {
743-
if (unsafeUniqueKey === kUnsafeEphemeralUniqueKey) {
744-
return {
745-
className,
746-
enableSql,
747-
ephemeralLocal: kVoid,
748-
preventEviction: unsafePreventEviction,
749-
};
750-
} else {
751-
return {
752-
className,
753-
enableSql,
754-
// This `uniqueKey` will (among other things) be used as part of the
755-
// path when persisting to the file-system. `-` is invalid in
756-
// JavaScript class names, but safe on filesystems (incl. Windows).
757-
uniqueKey:
758-
unsafeUniqueKey ?? `${options.name ?? ""}-${className}`,
759-
preventEviction: unsafePreventEviction,
760-
};
761-
}
762-
}
753+
{
754+
enableSql,
755+
unsafeUniqueKey,
756+
unsafePreventEviction: preventEviction,
757+
container,
758+
},
759+
]) =>
760+
unsafeUniqueKey === kUnsafeEphemeralUniqueKey
761+
? {
762+
className,
763+
enableSql,
764+
ephemeralLocal: kVoid,
765+
preventEviction,
766+
container,
767+
}
768+
: {
769+
className,
770+
enableSql,
771+
// This `uniqueKey` will (among other things) be used as part of the
772+
// path when persisting to the file-system. `-` is invalid in
773+
// JavaScript class names, but safe on filesystems (incl. Windows).
774+
uniqueKey:
775+
unsafeUniqueKey ?? `${options.name ?? ""}-${className}`,
776+
preventEviction,
777+
container,
778+
}
763779
),
764780
durableObjectStorage:
765781
classNamesEntries.length === 0
@@ -789,6 +805,7 @@ export const CORE_PLUGIN: Plugin<
789805
options.hasAssetsAndIsVitest
790806
);
791807
}),
808+
containerEngine: getContainerEngine(options.containerEngine),
792809
},
793810
});
794811
}
@@ -1023,6 +1040,29 @@ function getWorkerScript(
10231040
}
10241041
}
10251042

1043+
/**
1044+
* Returns the Container engine configuration
1045+
* @param engineOrSocketPath Either a full engine config or a unix socket
1046+
* @returns The container engine, default to local Docker at `unix:/var/run/docker.sock`
1047+
*/
1048+
function getContainerEngine(
1049+
engineOrSocketPath: Worker_ContainerEngine | string | undefined
1050+
): Worker_ContainerEngine {
1051+
if (!engineOrSocketPath) {
1052+
// TODO: workerd does not support win named pipes
1053+
engineOrSocketPath =
1054+
platform() === "win32"
1055+
? "//./pipe/docker_engine"
1056+
: "unix:/var/run/docker.sock";
1057+
}
1058+
1059+
if (typeof engineOrSocketPath === "string") {
1060+
return { localDocker: { socketPath: engineOrSocketPath } };
1061+
}
1062+
1063+
return engineOrSocketPath;
1064+
}
1065+
10261066
export * from "./errors";
10271067
export * from "./proxy";
10281068
export * from "./constants";

packages/miniflare/src/plugins/do/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import {
1212
UnsafeUniqueKey,
1313
} from "../shared";
1414

15+
// Options for a container attached to the DO
16+
export const DOContainerOptionsSchema = z.object({
17+
imageName: z.string(),
18+
});
19+
export type DOContainerOptions = z.infer<typeof DOContainerOptionsSchema>;
20+
1521
export const DurableObjectsOptionsSchema = z.object({
1622
durableObjects: z
1723
.record(
@@ -33,6 +39,7 @@ export const DurableObjectsOptionsSchema = z.object({
3339
mixedModeConnectionString: z
3440
.custom<MixedModeConnectionString>()
3541
.optional(),
42+
container: z.custom<DOContainerOptions>().optional(),
3643
}),
3744
])
3845
)
@@ -54,6 +61,7 @@ export function normaliseDurableObject(
5461
unsafeUniqueKey: UnsafeUniqueKey | undefined;
5562
unsafePreventEviction: boolean | undefined;
5663
mixedModeConnectionString: MixedModeConnectionString | undefined;
64+
container: DOContainerOptions | undefined;
5765
} {
5866
const isObject = typeof designator === "object";
5967
const className = isObject ? designator.className : designator;
@@ -70,6 +78,7 @@ export function normaliseDurableObject(
7078
const mixedModeConnectionString = isObject
7179
? designator.mixedModeConnectionString
7280
: undefined;
81+
const container = isObject ? designator.container : undefined;
7382
return {
7483
className,
7584
scriptName,
@@ -78,6 +87,7 @@ export function normaliseDurableObject(
7887
unsafeUniqueKey,
7988
unsafePreventEviction,
8089
mixedModeConnectionString,
90+
container,
8191
};
8292
}
8393

packages/miniflare/src/plugins/shared/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
sanitisePath,
2424
} from "../../workers";
2525
import { UnsafeUniqueKey } from "./constants";
26+
import type { DOContainerOptions } from "../do";
2627

2728
export const DEFAULT_PERSIST_ROOT = ".mf";
2829

@@ -47,6 +48,7 @@ export type DurableObjectClassNames = Map<
4748
enableSql?: boolean;
4849
unsafeUniqueKey?: UnsafeUniqueKey;
4950
unsafePreventEviction?: boolean;
51+
container?: DOContainerOptions;
5052
}
5153
>
5254
>;

packages/miniflare/src/runtime/config/workerd.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export interface ServiceDesignator {
4949
props?: { json: string };
5050
}
5151

52+
export type Worker_DockerConfiguration = {
53+
socketPath: string;
54+
};
55+
56+
export type Worker_ContainerEngine = {
57+
localDocker: Worker_DockerConfiguration;
58+
};
59+
5260
export type Worker = (
5361
| { modules?: Worker_Module[] }
5462
| { serviceWorkerScript?: string }
@@ -64,6 +72,7 @@ export type Worker = (
6472
durableObjectStorage?: Worker_DurableObjectStorage;
6573
moduleFallback?: string;
6674
tails?: ServiceDesignator[];
75+
containerEngine?: Worker_ContainerEngine;
6776
};
6877

6978
export type Worker_DurableObjectStorage =

packages/wrangler/src/__tests__/config-validation-pages.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ describe("validatePagesConfig()", () => {
181181
upstream_protocol: "https",
182182
host: "test-host",
183183
enable_containers: false,
184+
container_engine: undefined,
184185
},
185186
},
186187
};

packages/wrangler/src/api/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import events from "node:events";
22
import { fetch, Request } from "undici";
33
import { startDev } from "../dev";
4-
import { getDockerPath } from "../environment-variables/misc-variables";
4+
import {
5+
getDockerHost,
6+
getDockerPath,
7+
} from "../environment-variables/misc-variables";
58
import { run } from "../experimental-flags";
69
import { logger } from "../logger";
710
import type { Environment } from "../config";
@@ -88,6 +91,7 @@ export interface Unstable_DevOptions {
8891
enableIpc?: boolean;
8992
enableContainers?: boolean; // Whether to build and connect to containers in dev mode. Defaults to true.
9093
dockerPath?: string; // Path to the docker binary, if not on $PATH
94+
containerEngine?: string; // Docker socket
9195
};
9296
}
9397

@@ -224,6 +228,7 @@ export async function unstable_dev(
224228
nodeCompat: undefined,
225229
enableContainers: options?.experimental?.enableContainers ?? false,
226230
dockerPath: options?.experimental?.dockerPath ?? getDockerPath(),
231+
containerEngine: options?.experimental?.containerEngine ?? getDockerHost(),
227232
};
228233

229234
//outside of test mode, rebuilds work fine, but only one instance of wrangler will work at a time

packages/wrangler/src/api/startDevWorker/ConfigController.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
} from "../../dev";
1313
import { getClassNamesWhichUseSQLite } from "../../dev/class-names-sqlite";
1414
import { getLocalPersistencePath } from "../../dev/get-local-persistence-path";
15-
import { getDockerPath } from "../../environment-variables/misc-variables";
15+
import {
16+
getDockerHost,
17+
getDockerPath,
18+
} from "../../environment-variables/misc-variables";
1619
import { UserError } from "../../errors";
1720
import { getFlag } from "../../experimental-flags";
1821
import { logger, runWithLogLevel } from "../../logger";
@@ -155,6 +158,7 @@ async function resolveDevConfig(
155158
enableContainers:
156159
input.dev?.enableContainers ?? config.dev.enable_containers,
157160
dockerPath: input.dev?.dockerPath ?? getDockerPath(),
161+
containerEngine: input.dev?.containerEngine ?? getDockerHost(),
158162
} satisfies StartDevWorkerOptions["dev"];
159163
}
160164

@@ -433,6 +437,9 @@ function resolveContainerConfig(
433437
config: Config
434438
): StartDevWorkerOptions["containers"] {
435439
const containers: WorkerOptions["containers"] = {};
440+
if (!config.dev.enable_containers) {
441+
return containers;
442+
}
436443
for (const container of config.containers ?? []) {
437444
containers[container.class_name] = {
438445
image: container.image ?? container.configuration.image,

0 commit comments

Comments
 (0)