devcontainers / templates

Repository for Dev Container Templates that are managed by Dev Container spec maintainers. See https://github.com/devcontainers/template-starter to create your own!
https://containers.dev/templates
MIT License
924 stars 242 forks source link

[go][bash][vscode-extensions] local cache and history persistence without volumes #124

Open 7d4b9 opened 1 year ago

7d4b9 commented 1 year ago

Hello,

I find that in some case you may prefer to persist your build caches, bash history, etc... inside a local folder instead of inside a volume.

The following approach keeps a subset of cache or history in each project on the local disk.

requirements:

use onCreateCmd:

Add the following in .devcontainer/devcontainers.json:

    "onCreateCommand": ".devcontainer/on-create-cmd.sh",

And now create a script .devcontainer/on-create-cmd.sh:

#!/bin/sh

set -e

sudo chmod o+rw /var/run/docker.sock

rm -rf ${HOME}/.bash_history
touch ${PWD}/.devcontainer/bash_history
ln -s ${PWD}/.devcontainer/bash_history ${HOME}/.bash_history

sudo rm -rf /go
mkdir -p ${PWD}/.devcontainer/go
sudo ln -s ${PWD}/.devcontainer/go /go

mkdir -p ~/.cache
sudo rm -rf ~/.cache/go-build
mkdir -p ${PWD}/.devcontainer/go-build-cache
sudo ln -s ${PWD}/.devcontainer/go-build-cache ~/.cache/go-build

# Vscode Remote-Container extension uses 'vscode-remote'
if [ -d ~/.vscode-server ] ; then
    rm -rf ~/.vscode-server/extensions
    mkdir -p ${PWD}/.devcontainer/vscode-extensions ~/.vscode-server
    ln -s ${PWD}/.devcontainer/vscode-extensions ~/.vscode-server/extensions
fi

# Github Codespaces uses 'vscode-remote'
if [ -d ~/.vscode-remote ] ; then
    rm -rf ~/.vscode-remote/extensions
    mkdir -p ${PWD}/.devcontainer/vscode-extensions ~/.vscode-remote
    ln -s ${PWD}/.devcontainer/vscode-extensions ~/.vscode-remote/extensions
fi

Do not forget to add execution rights for your script:

chmod +x .devcontainer/on-create-cmd.sh

Result inside the devcontainer

image

gitignore

As you can see now the project go build caches, vscode-extensions and bash history are generated inside the project path itself on your local disk.

I recommend that you finally add the following lines to .devcontainer/.gitignore to keep those file from versioning:

go
go-build-cache
bash_history
vscode-extensions

Working example:

You can view a working example of this on one of my personal test project: https://github.com/7d4b9/utrade/tree/dev/.devcontainer

Just open a Codespace container directly on GitHub or checkout the project locally to use your host.

Github Codespace

Keep in mind that here the persistence will occur in the current project checkout host. As it is a VM the data may be lost if this host is changed. However this is changing nothing as it is also the case if the data were kept inside a docker volume if the docker instance is renewed. So this is not a real warn, just an observation :)

bamurtaugh commented 1 year ago

Thanks for sharing this @7d4b9! As it sounds like this could be an update to documentation, we welcome any contributions to https://containers.dev/ - you can open a PR in its backing repo: https://github.com/devcontainers/devcontainers.github.io.