Docker number of lines in terminal changing inside docker

后端 未结 6 1631
轮回少年
轮回少年 2020-12-13 09:00

I would like to know how to change the following behavior. Let\'s say my terminal has 28 lines. Then I use the following commands:

$ tput li         


        
6条回答
  •  时光说笑
    2020-12-13 10:01

    UPDATE

    you can now install goinside command line tool with:

    sudo npm install -g goinside
    

    and go inside a docker container with a proper terminal size with:

    goinside docker_container_name
    

    Logic behind goinside

    thanks to @VonC answer we've got a solution for this problem with a simple bash snippet that we put in ~/.profile:

    goinside(){
        docker exec -it $1 bash -c "stty cols $COLUMNS rows $LINES && bash";
    }
    export -f goinside
    

    now you are able to get inside a docker container without terminal size issues with:

    $ goinside containername


    remember to source ~/.profile before using the goinside function.


    enabling autocompletion in bash

    (as it's shared in one of comments below) if you want to enable autocompletion for goinside you can use this snippet in .profile:

    goinside(){
        docker exec -it $1 bash -c "stty cols $COLUMNS rows $LINES && bash";
    }
    _goinside(){
        COMPREPLY=( $(docker ps --format "{{.Names}}" -f name=$2) );
    }
    complete -F _goinside goinside;
    export -f goinside;
    

    enabling autocompletion in zsh

    if you are using zsh as your default terminal you can use this snippet inside your ~/.zshrc file:

    autoload bashcompinit
    bashcompinit
    goinside(){
        docker exec -it $1 bash -c "stty cols $COLUMNS rows $LINES && bash";
    }
    _goinside(){
        COMPREPLY=( $(docker ps --format "{{.Names}}" -f name=$2) );
    }
    complete -F _goinside goinside;
    export goinside;
    

提交回复
热议问题