|
| 1 | +--- |
| 2 | +title: Docker & Dev Containers |
| 3 | +description: Learn how to configure your sandbox environment using Dev Containers. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Callout } from 'nextra-theme-docs' |
| 7 | + |
| 8 | +# Dev Containers |
| 9 | + |
| 10 | +CodeSandbox natively supports the [Dev Containers specification](https://containers.dev/), allowing you to customize your sandbox, install system-level dependencies, and run additional services. |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +To configure your sandbox environment, create a `.devcontainer/devcontainer.json` file inside the root of the sandbox: |
| 15 | + |
| 16 | +```json |
| 17 | +{ |
| 18 | + "name": "Node.js", |
| 19 | + "image": "mcr.microsoft.com/devcontainers/javascript-node:18", |
| 20 | + "features": { |
| 21 | + "ghcr.io/devcontainers/features/python:1": {} |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +In this example, we're installing Node v18 as base, with Python on top using Dev Container Features. |
| 27 | + |
| 28 | +Alternatively, you can use a `Dockerfile` to build the Docker image when the sandbox boots: |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "name": "Node.js", |
| 33 | + "build": { |
| 34 | + "dockerfile": "./Dockerfile" |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Using Dev Containers in the SDK |
| 40 | + |
| 41 | +When creating a sandbox, all shells will automatically run inside the Docker container specified in the Dev Container configuration. |
| 42 | + |
| 43 | +```ts |
| 44 | +const sandbox = await sdk.sandbox.create({ |
| 45 | + template: "node" // Template with Dev Container configuration |
| 46 | +}); |
| 47 | + |
| 48 | +await sandbox.shells.run("node --version"); |
| 49 | +``` |
| 50 | + |
| 51 | +Since we use memory snapshots, the Docker container will already be running when you run your shell. |
| 52 | + |
| 53 | +## Docker Compose |
| 54 | + |
| 55 | +You can run additional services using Docker Compose by adding a `docker-compose.yml` configuration to your Dev Container: |
| 56 | + |
| 57 | +```json |
| 58 | +{ |
| 59 | + "name": "Full Stack App", |
| 60 | + "dockerComposeFile": "docker-compose.yml", |
| 61 | + "service": "app", |
| 62 | + "workspaceFolder": "/workspace" |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +With a corresponding `docker-compose.yml`: |
| 67 | + |
| 68 | +```yaml |
| 69 | +services: |
| 70 | + app: |
| 71 | + image: mcr.microsoft.com/devcontainers/javascript-node:18 |
| 72 | + command: sleep infinity |
| 73 | + |
| 74 | + db: |
| 75 | + image: postgres:14 |
| 76 | + ports: |
| 77 | + - 5432:5432 |
| 78 | + environment: |
| 79 | + POSTGRES_PASSWORD: password |
| 80 | +``` |
| 81 | +
|
| 82 | +### Using Docker Compose in the SDK |
| 83 | +
|
| 84 | +The SDK will automatically start all services defined in your Docker Compose configuration: |
| 85 | +
|
| 86 | +```ts |
| 87 | +const sandbox = await sdk.sandbox.create({ |
| 88 | + template: "fullstack" // Template with Dev Container configuration |
| 89 | +}); |
| 90 | + |
| 91 | +// Wait for all services to be ready |
| 92 | +await sandbox.setup.waitForFinish(); |
| 93 | + |
| 94 | +// You can now connect to the services |
| 95 | +const portInfo = await sandbox.ports.waitForPort(5432); |
| 96 | +console.log(`Database available at: ${portInfo.hostname}:${portInfo.port}`); |
| 97 | +``` |
| 98 | +
|
| 99 | +## Examples |
| 100 | +
|
| 101 | +### Full-Stack Development Environment |
| 102 | +
|
| 103 | +Here's an example of setting up a full-stack development environment with Node.js and PostgreSQL: |
| 104 | +
|
| 105 | +```ts |
| 106 | +const sandbox = await sdk.sandbox.create({ |
| 107 | + template: "fullstack" |
| 108 | +}); |
| 109 | + |
| 110 | +// Wait for environment setup |
| 111 | +const progress = await sandbox.setup.waitForFinish(); |
| 112 | +if (progress.state !== "FINISHED") { |
| 113 | + throw new Error("Environment setup failed"); |
| 114 | +} |
| 115 | + |
| 116 | +// Start the development server |
| 117 | +const devTask = await sandbox.tasks.runTask("dev"); |
| 118 | + |
| 119 | +// Wait for both the app and database to be ready |
| 120 | +const [appPort, dbPort] = await Promise.all([ |
| 121 | + sandbox.ports.waitForPort(3000), |
| 122 | + sandbox.ports.waitForPort(5432) |
| 123 | +]); |
| 124 | + |
| 125 | +console.log(` |
| 126 | +App running at: ${appPort.getPreviewUrl()} |
| 127 | +Database available at: ${dbPort.hostname}:${dbPort.port} |
| 128 | +`); |
| 129 | +``` |
| 130 | + |
| 131 | +### Custom Environment with System Dependencies |
| 132 | + |
| 133 | +Example of a sandbox that needs specific system packages: |
| 134 | + |
| 135 | +```ts |
| 136 | +const sandbox = await sdk.sandbox.create({ |
| 137 | + template: "custom-env" |
| 138 | +}); |
| 139 | +
|
| 140 | +// The Dev Container will install required packages during setup |
| 141 | +await sandbox.setup.waitForFinish(); |
| 142 | +
|
| 143 | +// Run a command that uses the installed packages |
| 144 | +const result = await sandbox.shells.run("ffmpeg -version"); |
| 145 | +console.log(result.output); |
| 146 | +``` |
| 147 | + |
| 148 | +For more information about Dev Container configuration options and features, visit the [Dev Container specification](https://containers.dev/). Also, take a look at our [snapshot builder](/sdk/snapshot-builder.mdx) to learn how to create efficient snapshots with preloaded Docker images. |
0 commit comments