Persistent IRB history in ruby Docker containers

When you run ruby (with or without rails) applications inside Docker containers, you may want to temporarilly run an IRB (or rails console, or whatever built around IRB) session to interract with your application in the same contenerized environment.

However, the very nature of the container system makes the session to vanish once you stop the container. The next time you start an IRB session, your previous commands will not be available.

Fortunately, there is a quick and simple solution to that: just bind mount an history file inside the container in the right place, and you will be able to keep your history from one session to another. IRB persists its history by default in the /root/.irb_history file. Thus the solution is to bind mount an external file to keep it between work sessions:

sudo docker run --rm -v ./tmp/irb_history:/root/.irb_history <image> irb
sudo docker compose run -v ./tmp/irb_history:/root/.irb_history <image> bundle exec rails console