Docker and .bash_history

前端 未结 6 1788
感动是毒
感动是毒 2020-12-30 19:58

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 thro

相关标签:
6条回答
  • 2020-12-30 20:26

    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

    0 讨论(0)
  • 2020-12-30 20:29

    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

    I assume file structure to be:

    docker-compose.yml
    docker/
       \--> bash/
          \--> .bashrc
          \--> .bash_history
    

    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:

    export HISTFILE=~/bash/.bash_history
    touch $HISTFILE
    

    Optionally, you can add to .gitignore:

    docker/bash/.bash_history
    
    0 讨论(0)
  • 2020-12-30 20:30

    To keep IPython history, you can set the IPYTHONDIR environment variable to somewhere within your mapped volume.

    The docker-compose.override.yml would look like this:

    version: '2'
    services:
      some-service:
        environment:
          - IPYTHONDIR=/app/.ipython
        volumes:
          - .:/app
    
    0 讨论(0)
  • 2020-12-30 20:30

    For bash

    volumes:
            - ./.data/shell_history/php_bash_history.txt:/home/www-data/.bash_history #bash
    

    For sh

    volumes:
        - ./.data/shell_history/nginx_bash_history.txt:/root/.ash_history #sh
    
    0 讨论(0)
  • 2020-12-30 20:33

    In your docker-compose.override.yml:

    version: '2'
    services:
      whatever:
        …
        volumes:
          - …
          - ~/.bash_history:/root/.bash_history
    
    0 讨论(0)
  • 2020-12-30 20:43

    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 host and when you exit the container, the host will have the history of the commands typed while in the container.

    0 讨论(0)
提交回复
热议问题