Keep and preserve ipython and bash history between docker-compose containers

01/31/2021 | Author: Eduardo Enriquez

Preserve history

There is an easy way of keeping the history of commands that we write when we are inside of a container or inside of an ipython shell: docker volumes.

It could be done with a host volume, but the downside is that we should add that file to our gitignore, or we could leave this responsabiliy to docker and use the docker volumes.

Ipython

For ipython is simpler than for bash, we just create a volume and we match th e whole folder of ipython in /root/.ipython

version: '3.8'

volumes:
  ipython_history:

services:
  ipython:
    image: ipython/ipython
    volumes: ipython_history:/root/.ipython/profile_default

Bash

For keeping the bash or ash (alpine shell) history is a little bit harder because we don'\nt have the history file before we run the container for the first time. So we need a combination of an env variable,

version: '3.8'

volumes:
  shell_history:

services:
  bash:
    image: python
    environment:
      HISTFILE: /root/hist/.bash_history
    volumes:
      - shell_history:/root/hist

 For both things plus django

Here you could see both things together

version: '3.8'

volumes:
  shell_history:
  ipy_history:

services:
  django:
    command: python manage.py runserver 0.0.0.0:8080
    build: .
    ports:
      - 8080:8080
    environment:
      HISTFILE: /root/hist/.bash_history
    volumes:
      - history:/root/hist/
      - ipy_history:/root/.ipython/profile_default