2.6 KiB
Order | Area | TOCTitle | PageTitle | ContentId | MetaDescription | DateApproved |
---|---|---|---|---|---|---|
5 | advancedcontainers | Persist bash history | Persist bash history in containers | 68111a8d-530c-4909-916d-3e68758e5e33 | Persist bash history in containers | 10/29/2024 |
Persist bash history
You can also use a mount to persist your bash
command history across sessions / container rebuilds.
First, update your Dockerfile
so that each time a command is used in bash
, the history is updated and stored in a location we will persist.
If you have a root user, update your Dockerfile
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 your Dockerfile
with the following. Replace user-name-goes-here
with the name of a non-root user in the container.
ARG USERNAME=user-name-goes-here
RUN 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 the
mounts
property (VS Code 1.41+) in yourdevcontainer.json
file."mounts": [ "source=projectname-bashhistory,target=/commandhistory,type=volume" ]
-
Docker Compose: Update (or extend) your
docker-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, run Dev Containers: Rebuild Container from the Command Palette (kbstyle(F1)
) to pick up the change. Otherwise run Dev Containers: Open Folder in Container... to connect to the container.