Tips Docker: Keep your bash history
I'm starting by heavily using docker containers for developing, testing and reviewing web applications.
There was one thing that bugged me for a long time: the fact that I lost the history of commands I typed in the shell (bash) of the containers each time I stopped/restarted them. I don't have a good memory and don't like to repeat myself. Also, I'm constantly searching in my history (with
history|grep thing or
CTRL+R) and wasn't able to do it correctly in a container.
I will present you two solutions to resolve this annoying issue.
Share the .bash_history file with the host (docker-compose)
The idea is simple: store bash history on the host and bind-mount this file into the container.
In the following example, I use a file named
.docker_bash_history that I stored in my home directory. Important the file must exist before mounting it.
You can achieve this with the following
docker-compose.yml example file:
One advantage of this approach is that the bash history can be shared between all your containers. You can also bind-mount your
.bash_history file of the host instead of using a dedicated file (
Store .bash_history into a named volume
Another way is to persist the
.bash_history into a named volume and rely on the
HISTFILE environment variable to specify where the history file is:
The same approach can be used without
$ docker run -it -v ~/.docker_bash_history:/root/.bash_history ubuntu bash
That's it !