Dev Containers
CodeSandbox natively supports the Dev Containers specification (opens in a new tab), allowing you to customize your sandbox, install system-level dependencies, and run additional services.
Configuration
To configure your sandbox environment, create a .devcontainer/devcontainer.json
file inside the root of the sandbox:
{
"name": "Node.js",
"image": "mcr.microsoft.com/devcontainers/javascript-node:18",
"features": {
"ghcr.io/devcontainers/features/python:1": {}
}
}
In this example, we're installing Node v18 as base, with Python on top using Dev Container Features.
Alternatively, you can use a Dockerfile
to build the Docker image when the sandbox boots:
{
"name": "Node.js",
"build": {
"dockerfile": "./Dockerfile"
}
}
Using Dev Containers in the SDK
When creating a sandbox, all shells will automatically run inside the Docker container specified in the Dev Container configuration.
const sandbox = await sdk.sandboxes.create({
template: "node" // Template with Dev Container configuration
});
await sandbox.commands.run("node --version");
Since we use memory snapshots, the Docker container will already be running when you run your shell.
Docker Compose
You can run additional services using Docker Compose by adding a docker-compose.yml
configuration to your Dev Container:
{
"name": "Full Stack App",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace"
}
With a corresponding docker-compose.yml
:
services:
app:
image: mcr.microsoft.com/devcontainers/javascript-node:18
command: sleep infinity
db:
image: postgres:14
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: password
We will automatically start all services defined in your Docker Compose configuration when the Sandbox starts.