Rust Playground is an isolated environment for building, testing, and playing around with rust. With environment preconfigured, writing code is simply starting the container, and connect your vs code to the created tunnel. We may clean up the sandbox after tinkering with just one command, and it will be reseated and be ready for next code.
To get started, docker is required to be installed on the host machine that we planed to host it on. Then, follow the following steps to spin up the container. And a GitHub account is required to connect the tunnel to the cloud so we can access it.
- Clone this repo into your machine and cd into it.
git clone https://github.com/kelvinchin12070811/rust-playground
cd rust-playground
- Build the docker image.
docker build .
We may also give it a tag for easy to maintain later (Recommended).
docker build -t <your name>/rust-playground
- Run the docker image.
Once the image has build, we can then spin up the image as bellow:
Bash/ zsh:
docker run /
-d /
-h <name you want the vs code tunnel be> /
--volume "<local playground>:/home/playground" /
--volume "<vs code binary data>:/root/.vscode" /
--volume "<vs code server data>:/root/.vscode-server" /
--name <name for the container> /
<your github username>/rust-playground:latest
example:
docker run /
-d /
-h rust-playground /
--volume "~/documents/rust-playground/playground:/home/playground" /
--volume "~/documents/rust-playground/.vscode:/root/.vscode" /
--volume "~/documents/rust-playground/.vscode-server:/root/.vscode-server" /
--name rust-playground /
bob1234/rust-playground:latest
For Windows host and powershell:
docker run `
-d `
-h rust-playground `
--volume "C:/rust-playground/playground:/home/playground" `
--volume "C:/rust-playground/.vscode:/root/.vscode" `
--volume "C:/rust-playground/.vscode-server:/root/.vscode-server" `
--name rust-playground `
bob1234/rust-playground
This will require 3 docker volumes to store the following data:
- Playground workspace - The main location to store your code and will be wiped when called.
- .vscode folder - This is where the server version of vs code will be installed, also with the tunnel authentication token.
- .vscode-server - This is the folder where vs code used to store the server side data such as extensions.
To enable reset feature, set the environment variable FEATURE_RESET
as following:
Bash/ zsh
docker run /
-d /
-h rust-playground /
-e FEATURE_RESET=1 /
--volume "~/documents/rust-playground/playground:/home/playground" /
--volume "~/documents/rust-playground/.vscode:/root/.vscode" /
--volume "~/documents/rust-playground/.vscode-server:/root/.vscode-server" /
--name rust-playground /
bob1234/rust-playground:latest
For Windows host and powershell:
docker run `
-d `
-h rust-playground `
-e FEATURE_RESET=1 `
--volume "C:/rust-playground/playground:/home/playground" `
--volume "C:/rust-playground/.vscode:/root/.vscode" `
--volume "C:/rust-playground/.vscode-server:/root/.vscode-server" `
--name rust-playground `
bob1234/rust-playground
- Authenticate the tunnel for first time use
Once the container is started, we will need to authorize the tunnel to allow it to be accessed from the vs code or remotely from vscode.dev.
We first tail the docker log to get the auth link generated by the vscode cli tool.
docker logs -n 50 your-container-tunnel
We will get the following:
*
* Visual Studio Code Server
*
* By using the software, you agree to
* the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and
* the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement).
*
To grant access to the server, please log into https://github.com/login/device and use code ****-****
Follow the instruction on the screen and login to the given link, we will need to login to our github account and authorize the connection.
Once again, run the following command again:
docker logs -n 50 your-container-tunnel
We will get output as bellow:
[2023-01-01 05:20:30] info Creating tunnel with the name: <the-assigned-name-of-your-tunnel>
Open this link in your browser https://vscode.dev/tunnel/<the-assigned-name-of-your-tunnel>
Now we can access to the tunnel via the link given. We can also able to access the tunnel via vs code's remote development extension or from vscode.dev. Login to the same github account on prompt and we are ready to rock it.
As the docker file pull the rust image via the latest tag, to update the rust version, simply rebuild the image and redeploy it. VS Code cli should updated by it self when needed.
The folder we going to write our rust code is at /home/playground
. If you are not in this directory, you can use the
hop
command to quickly jump into that folder. There's also a code-hop
command which will launch new vs code on the
playground directory.
Once finished with the mock, reset
command helps to clean up the playground and restore it to initial state. This is
a dangerous command so please backup any things that might be needed after. This command is by default disabled but can
be enabled with the environment variable FEATURE_RESET
.
Bellow is a list of custom commands that may come into help:
Command | Description |
---|---|
hop | Quickly jump to the playground folder. |
code-hop | Open new VS Code Window (or tab if on vscode.dev) in the playground folder. |
cls | Alias to clear, just to save a few typing. |
reset |
Default disabled, reset the playground folder to initial state. Can be enabled by setting the environment
variable FEATURE_RESET to 1 .
|
about | Nothing fancy, just prints the about text. |
This container did get some reference from the article Run VS Code Remote Tunnels in a container by Anh Dang on setting up the vs code cli in the container. You may refer to the article on the details on how to setup a container with vscode tunnel.