codespace icon indicating copy to clipboard operation
codespace copied to clipboard

Persist state and local keys across devcontainer rebuilds in VSCode

Open maxlyth opened this issue 7 months ago • 0 comments

This devcontainer feature stores the Tailscale state in /workspaces/.tailscale/ but that is outside the structure that is automatically mapped by VSCode to the local project directory so the state was lost each time the container is rebuilt.

I have a few other states that I need to persist across container rebuilds such as shell history and authkeys for CLI tools such as Supabase and Doppler. My solution is to bind mount these to a .devcontainer/volumes/ folder.

You can also use Docker Volumes but these are opaque, easy to loose, and I prefer to keep all config relating to a project together. You probably want to add the volumes directory to .gitignore as there are likely privileged keys stored in there and each project contributor keeps their own separate keys.

To setup, once Tailscale is connected, inside the devcontainer run: mkdir .devcontainer/volumes && sudo cp -r /workspaces/.tailscale .devcontainer/volumes

and then add the following to the mounts section of devcontainer.json "source=${localWorkspaceFolder}/.devcontainer/volumes/.tailscale,target=/workspaces/.tailscale,type=bind"

If someone knows a better way to do this then please chime in.

maxlyth avatar Jul 26 '24 16:07 maxlyth