-
Notifications
You must be signed in to change notification settings - Fork 83
feat(docker): add Docker resources #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
austinm911
wants to merge
2
commits into
alchemy-run:main
Choose a base branch
from
austinm911:codex/docker-v2-resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Docker PostgreSQL Example | ||
|
|
||
| This example provisions PostgreSQL 18 Alpine with Docker resources: | ||
|
|
||
| - `Docker.RemoteImage` pulls `postgres:18-alpine` | ||
| - `Docker.Network` creates an app network | ||
| - `Docker.Volume` creates persistent database storage | ||
| - `Docker.Container` starts PostgreSQL with a redacted password, a network alias, a named volume, and a host port | ||
| - `Docker.Container.inspect` returns the bound host port | ||
|
|
||
| Docker resources use the active Docker CLI context. That can be Docker Desktop, a remote Docker host, an SSH context, or a CI runner daemon. | ||
|
|
||
| These resources are separate from `Cloudflare.Container`. The bridge between Docker and cloud container platforms is an image reference or registry digest, not a swappable container object. | ||
|
|
||
| ## Commands | ||
|
|
||
| ```sh | ||
| bun install | ||
| POSTGRES_PASSWORD=change-me bun run --filter docker-postgres-example deploy | ||
| bun run --filter docker-postgres-example destroy | ||
| ``` | ||
|
|
||
| If `POSTGRES_PASSWORD` is omitted, the example uses a redacted development default. | ||
|
|
||
| The stack publishes PostgreSQL on `localhost:15432`: | ||
|
|
||
| ```sh | ||
| psql postgres://alchemy:change-me@localhost:15432/app | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as Alchemy from "alchemy"; | ||
| import * as Docker from "alchemy/Docker"; | ||
| import * as Config from "effect/Config"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Redacted from "effect/Redacted"; | ||
|
|
||
| const POSTGRES_PORT = 15432; | ||
| const POSTGRES_CONTAINER = "alchemy-example-postgres"; | ||
| const EMPTY_RUNTIME: Docker.ContainerRuntimeInfo = { ports: {} }; | ||
|
|
||
| export default Alchemy.Stack( | ||
| "DockerPostgresExample", | ||
| { providers: Docker.providers(), state: Alchemy.localState() }, | ||
| Effect.gen(function* () { | ||
| const password = yield* Config.redacted("POSTGRES_PASSWORD").pipe( | ||
| Config.withDefault(Redacted.make("alchemy-postgres")), | ||
| ); | ||
|
|
||
| const image = yield* Docker.RemoteImage("postgres-image", { | ||
| name: "postgres", | ||
| tag: "18-alpine", | ||
| alwaysPull: false, | ||
| }); | ||
| const network = yield* Docker.Network("app-network"); | ||
| const data = yield* Docker.Volume("postgres-data"); | ||
|
|
||
| const postgres = yield* Docker.Container("postgres", { | ||
| name: POSTGRES_CONTAINER, | ||
| image, | ||
| environment: { | ||
| POSTGRES_DB: "app", | ||
| POSTGRES_USER: "alchemy", | ||
| POSTGRES_PASSWORD: password, | ||
| }, | ||
| ports: [{ external: POSTGRES_PORT, internal: 5432 }], | ||
| volumes: [ | ||
| { | ||
| hostPath: data.name, | ||
| containerPath: "/var/lib/postgresql/data", | ||
| }, | ||
| ], | ||
| networks: [{ name: network.name, aliases: ["postgres"] }], | ||
| healthcheck: { | ||
| cmd: ["CMD-SHELL", "pg_isready -U alchemy -d app"], | ||
| interval: "5s", | ||
| timeout: "5s", | ||
| retries: 10, | ||
| }, | ||
| start: true, | ||
| }); | ||
|
|
||
| const runtime = yield* Docker.Container.inspect(POSTGRES_CONTAINER).pipe( | ||
| Effect.catchTag("DockerCommandError", () => | ||
| Effect.succeed(EMPTY_RUNTIME), | ||
| ), | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is Docker.Container.inspect? Should this be Docker.inspectContainer?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| return { | ||
| container: postgres.name, | ||
| image: image.imageRef, | ||
| network: network.name, | ||
| volume: data.name, | ||
| hostPort: runtime.ports["5432/tcp"] ?? POSTGRES_PORT, | ||
| connectionString: `postgres://alchemy:***@localhost:${POSTGRES_PORT}/app`, | ||
| }; | ||
| }), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "docker-postgres-example", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "license": "Apache-2.0", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/alchemy-run/alchemy-effect.git", | ||
| "directory": "examples/docker-postgres" | ||
| }, | ||
| "type": "module", | ||
| "scripts": { | ||
| "deploy": "alchemy deploy", | ||
| "destroy": "alchemy destroy", | ||
| "build": "tsgo --noEmit -p tsconfig.json" | ||
| }, | ||
| "dependencies": { | ||
| "alchemy": "workspace:*", | ||
| "effect": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "include": ["alchemy.run.ts"], | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| "rootDir": ".", | ||
| "module": "Preserve", | ||
| "moduleResolution": "Bundler", | ||
| "target": "ESNext" | ||
| }, | ||
| "references": [ | ||
| { | ||
| "path": "../../packages/alchemy/tsconfig.json" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use the
Randomresource to generate itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done