Visual Studio Code Remote - Containers supports the idea of connecting to multiple containers in the same source tree. There is even a pattern if you are using Docker Compose for your configuration. Unfortunately GitHub Codespaces does not currently support attaching a second window to a different container in the same Codespaces. However, the fact that the same technology is used in both Remote - Containers and Codespaces allows you to use the Remote - Containers extension with a codespace to achieve the same goal with some subtle tweaks.
This repository includes a script that you can use to connect to multiple containers via the GitHub CLI and the Remote - Containers extension. Any dev container configuration files can be in sub-folders rather than the repository root.
Codespaces will ultimately have first class support for this pattern, so this is a workaround given current limitations.
- Install VS Code (stable) locally.
- Install the GitHub CLI locally.
- Install an OpenSSH client and configure an SSH key for use with GitHub.
-
Create a codespace from this repository if you do not already have one. You can do this using the GitHub CLI as follows:
gh codespace create --repo codespaces-contrib/codespaces-multi-dev-container
Note the name of the codespace. If you have an existing codespace you want to use instead, you can find out its name by running:
gh codespace list
-
Next, copy
codespace-remote-ssh-connect.sh
(macOS / Linux) orcodespace-remote-ssh-connect.ps1
andcodespace-remote-ssh-connect.cmd
(Windows) to your local machine. -
In a local terminal, use the script to set up a connection to the codespace. For example, on macOS / Linux (replacing <codespace-name> with the name from step 1):
bash codespace-remote-ssh-connect.sh <codespace-name>
... or on Windows, use PowerShell/Command Prompt (not WSL) as follows:
.\codespace-remote-ssh-connect.cmd <codespace-name>
-
In the new Remote - SSH VS Code window that appears, start a new VS Code terminal.
-
Run the following for each folder with a dev container you want to open:
code <folder name>
For example:
code codespaces-multi-dev-container/container-1-src code codespaces-multi-dev-container/container-2-src
-
In each new window, click the "Reopen in Container" button in the notification that appears.
-
After everything is up and running, use each window as you would normally.
To adapt for your own use:
- Make a copy of the appropriate
codespace-remote-ssh-connect-*.*
files on your local machine in a spot where you can use them. (You can optionally place them in other repositories to share with others, but this is not required). - You do not need a custom container configuration in the root of your repository for this model to work. However, if your repository has a
.devcontainer
folder or.devcontainer.json
file in the root of the repository with a custom image or Dockerfile, make sure the docker-in-docker is included. - Add your actual dev container configuration files to sub-folders in the repository rather than the repository root.
- Use the script as described above to access them.
There are few to-dos for this sample:
- Cache VS Code Server between development containers to avoid having to download it multiple times.
- Look for ways to further reduce steps.