|
| 1 | + |
| 2 | +# Running Convex in a DevContainer for Local Development |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +> [!IMPORTANT] |
| 7 | +> This approach is meant for **local development** and is not intended for self-hosting Convex in production. |
| 8 | +
|
| 9 | +## What is a DevContainer? |
| 10 | + |
| 11 | +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). |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +This setup is especially useful for teams, open source contributors, or anyone who wants to avoid dependency drift between local machines. |
| 16 | + |
| 17 | +## Why use a DevContainer? |
| 18 | + |
| 19 | +- Reproducible local environment with no host machine setup required |
| 20 | +- Isolated from other projects and host system |
| 21 | +- Preconfigured runtimes, dependencies, tools and extensions (e.g., Node.js, pnpm, Convex CLI) |
| 22 | +- Easy onboarding for new team members or contributors |
| 23 | + |
| 24 | +## Requirements |
| 25 | + |
| 26 | +To use a DevContainer, you need to have the following installed: |
| 27 | + |
| 28 | +- [Docker](https://www.docker.com/products/docker-desktop) |
| 29 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 30 | +- [Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) |
| 31 | + |
| 32 | +## Minimal DevContainer Example for Convex |
| 33 | + |
| 34 | +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: |
| 35 | + |
| 36 | +```jsonc |
| 37 | +{ |
| 38 | + "name": "convex-dev", |
| 39 | + "image": "mcr.microsoft.com/devcontainers/typescript-node:1-22-bookworm", |
| 40 | + "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", |
| 41 | + |
| 42 | + "postCreateCommand": "npm install -g convex && npx convex dev --once", |
| 43 | + "postAttachCommand": "git config --global diff.ool ...", |
| 44 | + "postStartCommand": "git config --global --add safe.directory /workspaces/${localWorkspaceFolderBasename}", |
| 45 | + |
| 46 | + "mounts": [ |
| 47 | + "source=${localEnv:HOME}/.ssh,target=/home/node/.ssh,type=bind,consistency=cached", |
| 48 | + "source=${localEnv:HOME}/.convex,target=/home/node/.convex,type=bind,consistency=cached", |
| 49 | + "source=${localEnv:HOME}/.cache/convex,target=/home/node/.cache,type=bind,consistency=cached" |
| 50 | + ], |
| 51 | + |
| 52 | + "remoteUser": "node", |
| 53 | + "forwardPorts": [3210, 6790, 6791] |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +You can adapt the image, remote user, or mounted paths depending on your project needs or base OS image. |
| 58 | + |
| 59 | +### Explanation of the Configuration |
| 60 | + |
| 61 | +This minimal setup includes just a few customizations that are important for Convex to run reliably inside a containerized environment. |
| 62 | + |
| 63 | +#### `.convex` mount |
| 64 | + |
| 65 | +```json |
| 66 | +"mounts": [ |
| 67 | + "source=${localEnv:HOME}/.convex,target=/home/node/.convex,type=bind" |
| 68 | +] |
| 69 | +``` |
| 70 | + |
| 71 | +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: |
| 72 | + |
| 73 | +- The state is preserved across container rebuilds. |
| 74 | +- You can reuse the same identity and credentials inside and outside the container. |
| 75 | + |
| 76 | +Without this mount, Convex might behave as if it's being run for the first time every time you restart the container. |
| 77 | + |
| 78 | +#### `.cache/convex` mount |
| 79 | + |
| 80 | +```json |
| 81 | +"source=${localEnv:HOME}/.cache/convex,target=/home/node/.cache,type=bind,consistency=cached" |
| 82 | +``` |
| 83 | + |
| 84 | +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. |
| 85 | + |
| 86 | +This avoids re-downloading the same artifacts every time the container is recreated, which speeds up startup and reduces bandwidth usage. |
| 87 | + |
| 88 | +#### Forwarded ports |
| 89 | + |
| 90 | +```json |
| 91 | +"forwardPorts": [3210, 6790, 6791] |
| 92 | +``` |
| 93 | + |
| 94 | +Convex uses these ports during local development: |
| 95 | + |
| 96 | +- `3210` — the API server |
| 97 | +- `6790` — the web dashboard |
| 98 | +- `6791` — the internal health check used by the dashboard to determine if a local deployment is available |
| 99 | + |
| 100 | +Forwarding these ports ensures that the services running inside the container are accessible from your host machine and from the dashboard itself. |
| 101 | + |
| 102 | +#### `postCreateCommand` |
| 103 | + |
| 104 | +```json |
| 105 | +"postCreateCommand": "npx convex dev --once" |
| 106 | +``` |
| 107 | + |
| 108 | +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. |
| 109 | + |
| 110 | +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. |
0 commit comments