Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
7d4b9 opened this issue Feb 11, 2023 · 1 comment

Comments

@7d4b9
Copy link

7d4b9 commented Feb 11, 2023

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:

  • For this feature you will have to mount your project inside your devcontanier at the same position as it is on your host.
  • This is mandatory to not lose the data if the devcontainer is rebuild.
  • Thus, if the devcontainer is destroyed or rebuilt or if the docker volumes are removed the project specific build caches and bash history data will not be lost. They will still be

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants