Movatterモバイル変換


[0]ホーム

URL:


TryMCP servers to extend agent mode in VS Code!

Dismiss this update

Persist bash history

You can also use a mount to persist yourbash command history across sessions / container rebuilds.

First, update yourDockerfile so that each time a command is used inbash, the history is updated and stored in a location we will persist.

If you have a root user, update yourDockerfile with the following:

RUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \    && echo"$SNIPPET" >>"/root/.bashrc"

If you have a non-root user, update yourDockerfile with the following. Replaceuser-name-goes-here with the name of anon-root user in the container.

ARG USERNAME=user-name-goes-hereRUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \    && mkdir /commandhistory \    && touch /commandhistory/.bash_history \    && chown -R $USERNAME /commandhistory \    && echo"$SNIPPET" >>"/home/$USERNAME/.bashrc"

Next, add a local volume to store the command history. This step varies depending on whether or not you are using Docker Compose.

  • Dockerfile or image: Use themounts property (VS Code 1.41+) in yourdevcontainer.json file.

      "mounts": [      "source=projectname-bashhistory,target=/commandhistory,type=volume"  ]
  • Docker Compose: Update (orextend) yourdocker-compose.yml with the following for the appropriate service.

    version:'3'services:  your-service-name-here:    volumes:      -projectname-bashhistory:/commandhistory     # ...volumes:  projectname-bashhistory:

Finally, if you've already built the container and connected to it, runDev Containers: Rebuild Container from the Command Palette (F1) to pick up the change. Otherwise runDev Containers: Open Folder in Container... to connect to the container.

Note: If your host machine is running Linux (including WSL on Windows) and its user's UID and GID do not match those of the user in the dev container, the dev container user's UID and GID will be updated to those of the host user and you need to apply the same update to the volume by adding the following to the devcontainer.json.

```json  "postCreateCommand": {    "Fix Volume Permissions": "sudo chown -R $(whoami): /commandhistory"  }```

Video: How to make your bash history persist in a dev container

07/09/2025

[8]ページ先頭

©2009-2025 Movatter.jp