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 * as Alchemy from "alchemy"; |
| 3 | import * as Config from "effect/Config"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | |
| 8 | const POSTGRES_PORT = 15432; |
| 9 | const POSTGRES_CONTAINER = "alchemy-example-postgres"; |
| 10 | |
| 11 | export default Alchemy.Stack( |
| 12 | "DockerPostgresExample", |
| 13 | { |
| 14 | providers: Layer.merge(Docker.providers(), Alchemy.RandomProvider()), |
| 15 | state: Alchemy.localState(), |
| 16 | }, |
| 17 | Effect.gen(function* () { |
| 18 | const configuredPassword = yield* Config.Redacted("POSTGRES_PASSWORD").pipe(Config.option); |
| 19 | const password = yield* Option.match(configuredPassword, { |
| 20 | onSome: Effect.succeed, |
| 21 | onNone: () => Alchemy.makeRandom("PostgresPassword", { bytes: 16 }), |
| 22 | }); |
| 23 | |
| 24 | const image = yield* Docker.RemoteImage("postgres-image", { |
| 25 | name: "postgres", |
| 26 | tag: "18-alpine", |
| 27 | alwaysPull: false, |
| 28 | }); |
| 29 | const network = yield* Docker.Network("app-network"); |
| 30 | const data = yield* Docker.Volume("postgres-data"); |
| 31 | |
| 32 | const postgres = yield* Docker.Container("postgres", { |
| 33 | name: POSTGRES_CONTAINER, |
| 34 | image, |
| 35 | environment: { |
| 36 | POSTGRES_DB: "app", |
| 37 | POSTGRES_USER: "alchemy", |
| 38 | POSTGRES_PASSWORD: password, |
| 39 | }, |
| 40 | ports: [{ external: POSTGRES_PORT, internal: 5432 }], |
| 41 | volumes: [ |
| 42 | { |
| 43 | hostPath: data.name, |
| 44 | containerPath: "/var/lib/postgresql/data", |
| 45 | }, |
| 46 | ], |
| 47 | networks: [{ name: network.name, aliases: ["postgres"] }], |
| 48 | healthcheck: { |
| 49 | cmd: ["CMD-SHELL", "pg_isready -U alchemy -d app"], |
| 50 | interval: "5 seconds", |
| 51 | timeout: "5 seconds", |
| 52 | retries: 10, |
| 53 | }, |
| 54 | start: true, |
| 55 | }); |
| 56 | return { |
| 57 | container: postgres.name, |
| 58 | image: image.imageRef, |
| 59 | network: network.name, |
| 60 | volume: data.name, |
| 61 | hostPort: postgres.ports, |
| 62 | }; |
| 63 | }), |
| 64 | ); |