Getting started with Docker
Docker is available for Devboxes and Repositories. With Docker, you can get
root access and install any package to use within CodeSandbox, including databases and new languages.
Here (opens in a new tab) you can find a sandbox that uses Docker.
To get started with Docker, you need to create a new file called
Dockerfile inside the
.codesandbox folder. As an example, you could put these contents in:
FROM ubuntu # Install htop by default RUN apt update -y && apt install -y htop
This Dockerfile is intended for development, not for deployment. This means that you probably don't need to
ADD. We handle that for you by mounting the project directory at
/workspace in the container.
At this time, CodeSandbox currently only supports Debian and Ubuntu based images, for the best compatibility and user experience.
Now that you've created a Dockerfile and saved it, you should see a notification pop up, asking you to rebuild the container. Whenever the Dockerfile changes, we need to rebuild the container from that Dockerfile.
You can press "Yes", and it will build the container for you. Alternatively you can also rebuild the container by opening the command palette (CMD/Ctrl + Shift + P), and searching for "Rebuild Container".
With a Dockerfile in
.codesandbox, the VM will also always make sure to create the container when it boots.
Now that the container has been built, all your new terminal sessions will be executed from inside this container. You can test this by opening a new terminal, and verifying that
htop is installed (from our example).
Note that you also have
root access inside the terminal, that's because we give root access by default inside a container. With this, you can install new packages and use them in your sandbox from the terminal as well. Keep in mind that it's still better to configure this in the
Dockerfile in the long term, as these temporary container changes won't persist between Dockerfile rebuilds.
Once the environment is set up with
.codesandbox/Dockerfile, you can run additional services that are required for the dev environment using the native docker compose integration in CodeSandbox.
Create a new file
docker-compose.yml in the
.codesandbox folder. For example, you can add the following contents in the file to run an nginx server.
services: nginx: image: nginx ports: - 8080:80
Now that you've created the
docker-compose.yml and saved it, you should see a notification pop up, asking you to restart the
docker compose command. Whenever the
docker-compose.yml changes, this prompt will be shown and you can restart docker compose.
You can press "Yes" and it will run
docker compose up for you. You should now see a DevTool open with Docker Compose logs. Once the
nginx image is pulled and run, port
8080 should open and a notification should show with the URL to access the port.