# Run a repository's devcontainer

> For the complete documentation index, see [llms.txt](https://docs-stg.ringleader.dev/llms.txt).


> Build a repository's own devcontainer.json on a workstation, open your shell inside the container, and rebuild or repair it later.


Build a repository's own `devcontainer.json` on a workstation, open your shell inside the
container, and rebuild or repair it later. Use it when your team already maintains a
devcontainer and you don't want to rewrite it as a Ringleader configuration.

## Prerequisites

- [Install Ringleader](https://docs-stg.ringleader.dev/install/), with the Ringleader application running.
- A repository with a `devcontainer.json` that your workstation can clone. For a private
  repository, see [SSH keys for outbound Git](https://docs-stg.ringleader.dev/workstations/connectivity/git-ssh-keys/)
  and [Code sources](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/code-dotfiles-trusted-folders/#code-sources).
- To repair a failed build, the SSH configuration that
  [Connect to a workstation with SSH](https://docs-stg.ringleader.dev/guides/connect-with-ssh/) adds.
- The examples use a workstation named `app-box` in the `local` namespace, which the first
  task creates, and a repository at `git@github.com:acme/app.git`. Replace them with your own.

## Run the devcontainer

You declare the repository as a code source of a new workstation and turn on its
devcontainer. The workstation then clones the code, builds the container and puts your
shell inside it.

1. Create a file called `app-box.yaml` that declares the repository under `spec.sources`:

   ```yaml
   apiVersion: workstations.ringleader.dev/v1
   kind: Workstation
   metadata:
     name: app-box
     namespace: local
   spec:
     sources:
       - name: app
         path: /workspaces/app
         git:
           url: git@github.com:acme/app.git
         devcontainer:
           enabled: true
   ```

2. Optional: If the file isn't at `.devcontainer/devcontainer.json` or `.devcontainer.json`,
   add `configPath` under `devcontainer`, relative to the source's `path`.
3. In the same file, add a size under `spec`:

   ```yaml
   spec:
     providerConfig:
       cpus: 4
       memory: 8   # GiB
       disk: 100   # GiB
   ```

   [Workstation size for a devcontainer](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#workstation-size-for-a-devcontainer)
   says how to choose these values.
4. Apply the file:

   ```bash
   rl apply -f app-box.yaml
   ```

5. Wait until the container is built. The first build takes minutes:

   ```bash
   rl workstation wait app-box -n local --for condition=Configured=True --timeout 30m
   ```

6. Open a shell:

   ```bash
   rl shell app-box -n local
   ```

   You are now inside the container, as the file's `remoteUser`.

[Devcontainer declaration](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-declaration)
says what the declaration adds to the workstation, and
[Devcontainer readiness](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-readiness)
says when the workstation counts as ready.

## Rebuild the devcontainer

After you change `devcontainer.json`, build the container again on the workstation. A change
doesn't rebuild it by itself, as
[Changes to devcontainer.json](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#changes-to-devcontainerjson)
explains.

1. On your computer, restart the `devcontainer` service on the workstation's virtual machine, with
   `--host`:

   ```bash
   rl shell app-box -n local --host -- sudo systemctl restart devcontainer
   ```

2. Open a new shell with `rl shell app-box -n local`. It lands in the rebuilt container.

## Run a command on the virtual machine

Your shell lands in the container. To run one command on the workstation's virtual machine
instead, add `--host`:

```bash
rl shell app-box -n local --host -- uptime
```

`rl file cp --host` copies to and from the virtual machine the same way.
[Devcontainer shell and file locations](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-shell-and-file-locations)
lists where each tool lands.

## Repair a failed build

A devcontainer that fails to build keeps the workstation from reaching `Configured`, but the
`<user>-host` login still works. These steps fix the file and bring the workstation back to
`Configured`. [Devcontainer readiness](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/#devcontainer-readiness)
says why.

1. On your computer, read what failed:

   ```bash
   rl troubleshoot app-box -n local --failed
   ```

   The report shows the failing step and its output.
2. Log in to the virtual machine as the host user, which is your workstation's login user
   followed by `-host`. This example uses the login user `dev`:

   ```bash
   ssh dev-host@app-box
   ```

3. On the virtual machine, edit the file the report names, such as
   `/workspaces/app/.devcontainer/devcontainer.json`.
4. Close the SSH session to return to your computer.
5. Build the container again with the command in
   [Rebuild the devcontainer](#rebuild-the-devcontainer).
6. Wait for Ringleader to try the failed step again:

   ```bash
   rl workstation wait app-box -n local --for condition=Configured=True --timeout 10m
   ```

   The command returns once the workstation is `Configured`. [Retries after a failed step](https://docs-stg.ringleader.dev/workstations/configuration/step-failures-retries/#retries-after-a-failed-step)
   says how the retries are timed.

## Next steps

- [Devcontainer workstations](https://docs-stg.ringleader.dev/workstations/packages-tools-settings/devcontainers/) says
  which `devcontainer.json` settings work, and when the workstation counts as ready.
- [Port and socket forwarding](https://docs-stg.ringleader.dev/workstations/connectivity/port-socket-forwarding/)
  forwards a container's published port to your computer, as a LocalBinding.
- [Shells and remote commands](https://docs-stg.ringleader.dev/workstations/connectivity/shells-remote-commands/)
  runs commands, scripts and persistent sessions on a workstation.

