Skip to content

Commit 77b21cd

Browse files
committed
Enhance docs with DevContainer guide
- Added an example DevContainer configuration for running Convex in a containerized environment. - Updated `README.md` to include a guide on integrating Convex with DevContainers for reproducible local development. - The DevContainer setup includes necessary mounts, port forwarding, and commands for easy onboarding and consistent development experience. #79
1 parent 916e1d5 commit 77b21cd

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

self-hosted/devcontainer/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Running Convex in a DevContainer for Local Development
2+
3+
If you're working with Convex and want to use a consistent, container-based development environment, this guide provides a minimal setup using [DevContainers](https://containers.dev/) and Docker.
4+
5+
> [!IMPORTANT]
6+
> This approach is meant for **local development** and is not intended for self-hosting Convex in production.
7+
8+
## What is a DevContainer?
9+
10+
A DevContainer is a development environment defined as code and backed by a Docker container. It integrates tightly with Visual Studio Code through the [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers).
11+
12+
When you open a project with a `.devcontainer/devcontainer.json` file, VS Code automatically builds the container, installs dependencies, and mounts your project directory inside it.
13+
14+
This setup is especially useful for teams, open source contributors, or anyone who wants to avoid dependency drift between local machines.
15+
16+
## Why use a DevContainer?
17+
18+
- Reproducible local environment with no host machine setup required
19+
- Isolated from other projects and host system
20+
- Preconfigured runtimes, dependencies, tools and extensions (e.g., Node.js, pnpm, Convex CLI)
21+
- Easy onboarding for new team members or contributors
22+
23+
## Requirements
24+
25+
To use a DevContainer, you need to have the following installed:
26+
27+
- [Docker](https://www.docker.com/products/docker-desktop)
28+
- [Visual Studio Code](https://code.visualstudio.com/)
29+
- [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)
30+
31+
## Minimal DevContainer Example for Convex
32+
33+
The following is a minimal example of a working `.devcontainer/devcontainer.json` setup using a Node.js/TypeScript base image. It binds the necessary Convex and pnpm directories, and explicitly forwards the required ports:
34+
35+
```jsonc
36+
{
37+
"name": "convex-dev",
38+
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm",
39+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
40+
41+
"postCreateCommand": "npm install -g convex && npx convex dev --once",
42+
"postAttachCommand": "git config --global diff.ool ...",
43+
"postStartCommand": "git config --global --add safe.directory /workspaces/${localWorkspaceFolderBasename}",
44+
45+
"mounts": [
46+
"source=${localEnv:HOME}/.ssh,target=/home/node/.ssh,type=bind,consistency=cached",
47+
"source=${localEnv:HOME}/.convex,target=/home/node/.convex,type=bind,consistency=cached",
48+
"source=${localEnv:HOME}/.cache/convex,target=/home/node/.cache,type=bind,consistency=cached"
49+
],
50+
51+
"remoteUser": "node",
52+
"forwardPorts": [3210, 6790, 6791]
53+
}
54+
```
55+
56+
You can adapt the image, remote user, or mounted paths depending on your project needs or base OS image.
57+
58+
### Explanation of the Configuration
59+
60+
This minimal setup includes just a few customizations that are important for Convex to run reliably inside a containerized environment.
61+
62+
#### `.convex` mount
63+
64+
```json
65+
"mounts": [
66+
"source=${localEnv:HOME}/.convex,target=/home/node/.convex,type=bind"
67+
]
68+
```
69+
70+
Convex stores some local state in the `.convex` directory (such as deployment metadata and generated admin keys). Mounting it from your host machine into the container ensures that:
71+
72+
- The state is preserved across container rebuilds.
73+
- You can reuse the same identity and credentials inside and outside the container.
74+
75+
Without this mount, Convex might behave as if it's being run for the first time every time you restart the container.
76+
77+
#### `.cache/convex` mount
78+
79+
```json
80+
"source=${localEnv:HOME}/.cache/convex,target=/home/node/.cache,type=bind,consistency=cached"
81+
```
82+
83+
During `pnpm convex dev`, the Convex CLI downloads necessary artifacts such as backend binaries and the dashboard frontend into the `.cache/convex` directory. By mounting this directory from the host into the container, those files are persisted between container rebuilds and restarts.
84+
85+
This avoids re-downloading the same artifacts every time the container is recreated, which speeds up startup and reduces bandwidth usage.
86+
87+
#### Forwarded ports
88+
89+
```json
90+
"forwardPorts": [3210, 6790, 6791]
91+
```
92+
93+
Convex uses these ports during local development:
94+
95+
- `3210` — the API server
96+
- `6790` — the web dashboard
97+
- `6791` — the internal health check used by the dashboard to determine if a local deployment is available
98+
99+
Forwarding these ports ensures that the services running inside the container are accessible from your host machine and from the dashboard itself.
100+
101+
#### `postCreateCommand`
102+
103+
```json
104+
"postCreateCommand": "npx convex dev --once"
105+
```
106+
107+
This command ensures the Convex development server is started as soon as the container is ready. The `--once` flag runs the server in one-off mode, avoiding watch mode or automatic restarts.
108+
109+
This is useful for initial setup to verify everything is working, but you can always stop it and run `pnpm convex dev` manually when actively working on your functions.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/typescript-node
3+
{
4+
"name": "convex-dev",
5+
6+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
7+
"image": "mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm",
8+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
9+
10+
// Lifecycle events
11+
"postCreateCommand": "npm install -g convex && npx convex dev --once",
12+
"postAttachCommand": "git config --global diff.ool ...",
13+
"postStartCommand": "git config --global --add safe.directory /workspaces/${localWorkspaceFolderBasename}",
14+
15+
"mounts": [
16+
"source=${localEnv:HOME}/.ssh,target=/home/node/.ssh,type=bind,consistency=cached",
17+
"source=${localEnv:HOME}/.convex,target=/home/node/.convex,type=bind,consistency=cached",
18+
"source=${localEnv:HOME}/.cache/convex,target=/home/node/.cache,type=bind,consistency=cached"
19+
],
20+
21+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
22+
"remoteUser": "node",
23+
24+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
25+
"forwardPorts": [3210, 6790, 6791],
26+
27+
// Configure tool-specific properties.
28+
"customizations": {}
29+
}

0 commit comments

Comments
 (0)