My Favourite Docker Tip
source link: https://zwischenzugs.com/2015/06/14/my-favourite-docker-tip/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Currently co-authoring a book on Docker: Get 39% off with the code 39miell
The Problem
To understand the problem we’re going to show you a simple scenario where not having this is just plain annoying.
Imagine you are experimenting in Docker containers, and in the midst of your work you do something interesting and reusable. Here’s it’s going to be a simple echo command, but it could be some long and complex concatenation of programs that result in a useful output.
docker run -ti --rm ubuntu /bin/bash echo my amazing command exit
Now you forget about this triumph, and after some time you want to recall the incredible echo command you ran earlier. Unfortunately you can’t recall it and you no longer have the terminal session on your screen to scroll to. Out of habit you try looking through your bash history on the host:
history | grep amazing
…but nothing comes back, as the bash history is kept within the now-removed container and not the host you were returned to.
The Solution – Manual
To share your bash history with the host, you can use a volume mount when running your docker images. Here’s an example:
docker run \ -e HIST_FILE=/root/.bash_history \ -v=$HOME/.bash_history:/root/.bash_history \ -ti \ ubuntu /bin/bash
The -e argument specifies the history file bash is using on the host.
The -v argument maps the container’s root’s bash history file to the host’s, saving its history to your user’s bash history on the host.
This is quite a handful to type every time, so to make this more user-friendly you can set an alias up by putting the above command as an alias into your ‘~/.bashrc’ file.
alias dockbash='docker run -e HIST_FILE=/root/.bash_history -v=$HOME/.bash_history:/root/.bash_history
Making it Seamless
This is still not seamless as you have to remember to type ‘dockbash’ if you really wanted to perform a ‘docker run’ command. For a more seamless experience you can add this to your ‘~/.bashrc’ file:
function basher() { if [[ $1 = 'run' ]] then shift /usr/bin/docker run -e \ HIST_FILE=/root/.bash_history \ -v $HOME/.bash_history:/root/.bash_history \ "$@" else /usr/bin/docker "$@" fi } alias docker=basher
It sets up an alias for docker, which by default points to the ‘real’ docker executable in /usr/bin/docker. If the first argument is ‘run’ then it adds the bash magic.
Now when you next open a bash shell and run any ‘docker run’ command, the commands you run within that container will be added to your host’s bash history.
Conclusion
As a heavy Docker user, this change has reduced my frustrations considerably. No longer do I think to myself ‘I’m sure I did something like this a while ago’ without being able to recover my actions.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK