bash Docker 和 .bash_history
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28279862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Docker and .bash_history
提问by tzenderman
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 through?
有什么方法可以.bash_history
与 docker 容器共享一个卷,以便每次进入 shell 时我都可以滚动浏览 bash 历史记录?
Would be awesome to be able to do the same thing with IPython too.
能够用 IPython 做同样的事情会很棒。
回答by user2915097
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 hostand when you exit the container, the host will have the history of the commands typed while in the container.
这将使您进入新容器中的 bash shell,您将拥有来自主机的 bash 历史记录,当您退出容器时,主机将拥有在容器中键入的命令的历史记录。
回答by édouard Lopez
In your docker-compose.override.yml
:
在您的docker-compose.override.yml
:
version: '2'
services:
whatever:
…
volumes:
- …
- ~/.bash_history:/root/.bash_history
回答by allthethings
To keep IPython history, you can set the IPYTHONDIR
environment variable to somewhere within your mapped volume.
要保留 IPython 历史记录,您可以将IPYTHONDIR
环境变量设置为映射卷内的某个位置。
The docker-compose.override.yml
would look like this:
该docker-compose.override.yml
是这样的:
version: '2'
services:
some-service:
environment:
- IPYTHONDIR=/app/.ipython
volumes:
- .:/app
回答by jozo
My solution is useful when:
我的解决方案在以下情况下很有用:
- you don't want to share your local
.bash_history
with.bash_history
in your container - you use other shell (like fish shell) but you want to save
.bash_history
between your builds - you don't want to commit
.bash_history
to git repo but you want to create it automatically inside same directory when a container starts
- 你不希望分享您的本地
.bash_history
与.bash_history
您的容器 - 您使用其他外壳(如鱼壳),但您想
.bash_history
在构建之间保存 - 您不想提交
.bash_history
到 git repo,但您想在容器启动时在同一目录中自动创建它
I assume file structure to be:
我假设文件结构是:
docker-compose.yml
docker/
\--> bash/
\--> .bashrc
\--> .bash_history
docker-compose.yml
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:
./docker/bash/.bashrc- 它会自动创建 .bash_history:
export HISTFILE=~/bash/.bash_history
touch $HISTFILE
Optionally, you can add to .gitignore:
或者,您可以添加到.gitignore:
docker/bash/.bash_history
回答by GiDo
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
您还可以使用命名卷来实现这一点,并通过定义HISTFILE
环境变量告诉 bash 在哪里可以找到 bash 历史文件。我在这里解释了一点:https:
//antistatique.net/en/we/blog/2019/11/12/tips-docker-keep-your-bash-history
回答by max4ever
For bash
对于 bash
volumes:
- ./.data/shell_history/php_bash_history.txt:/home/www-data/.bash_history #bash
For sh
对于 sh
volumes:
- ./.data/shell_history/nginx_bash_history.txt:/root/.ash_history #sh