Is there any way to share a .bash_history
volume with a docker container so that everytime I go into a shell I have my bash history available for scrolling thro
You can also achieve this with a named volume and tell bash where he can find the bash history file by defining the HISTFILE
environment variable. I explained a bit more here:
https://antistatique.net/en/we/blog/2019/11/12/tips-docker-keep-your-bash-history
My solution is useful when:
.bash_history
with .bash_history
in your container.bash_history
between your builds.bash_history
to git repo but you want to create it automatically inside same directory when a container startsI assume file structure to be:
docker-compose.yml
docker/
\--> bash/
\--> .bashrc
\--> .bash_history
docker-compose.yml
web-service:
build: .
volumes:
- ./docker/bash/.bashrc:/home/YOUR_USER_NAME/.bashrc
- ./docker/bash:/home/YOUR_USER_NAME/bash
./docker/bash/.bashrc - it will automatically create .bash_history:
export HISTFILE=~/bash/.bash_history
touch $HISTFILE
Optionally, you can add to .gitignore:
docker/bash/.bash_history
To keep IPython history, you can set the IPYTHONDIR
environment variable to somewhere within your mapped volume.
The docker-compose.override.yml
would look like this:
version: '2'
services:
some-service:
environment:
- IPYTHONDIR=/app/.ipython
volumes:
- .:/app
For bash
volumes:
- ./.data/shell_history/php_bash_history.txt:/home/www-data/.bash_history #bash
For sh
volumes:
- ./.data/shell_history/nginx_bash_history.txt:/root/.ash_history #sh
In your docker-compose.override.yml:
version: '2'
services:
whatever:
…
volumes:
- …
- ~/.bash_history:/root/.bash_history
It is the example from the documentation about volume: Mount a host file as a data volume:
docker run --rm -it -v ~/.bash_history:/root/.bash_history ubuntu /bin/bash
This will drop you into a bash shell in a new container, you will have your bash history from the host and when you exit the container, the host will have the history of the commands typed while in the container.