Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: preserve command history

Tags:

docker

ubuntu

Every time I build a Docker container, the command history (CTRL+R in Ubuntu) is lost. Is there a way to prevent it from resetting the history after each build ?

like image 654
user6800816 Avatar asked Oct 28 '22 15:10

user6800816


1 Answers

Yes, there is a way. Even though it's a little bit tricky.

Basically when a container is removed, its entire filesystem is erased. So you need to find some way to persist the command history file.

First, find the history file used by shell in the container. For me, I am running a busybox container. I find out the history file is /root/ash_history.

$ ls -a /root
.             ..            .ash_history

Then, remove currently running container and re-run it with a host file mounted (so that we can persist the /root/.ash_history file).

docker run -v /path/to/host/file:/root/.ash_history ...

Input some random commands, and remove the container and run it again, you will be able to use CTRL+R in the container.

like image 115
Yuankun Avatar answered Oct 31 '22 04:10

Yuankun