Skip to content

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

@7d4b9

Description

@7d4b9

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions