Docker - Login into new TTY with a Running Container

Sometime we need to access the container from another shell in order to run some tail -f log/* or examine some files. so this post will help you to run a another shell with same container id.

You can run a new tty using docker exec -it [container-id] bash command.

To make life easier, I created the script below and placed it at /usr/bin/docker-tty.



if [ "$#" -ne $EXPECTED_NUM_ARGS ]; then
    # user didn't specify which container ID, assume the latest one
    CONTAINER_ID=`/usr/bin/docker ps -q --no-trunc | /bin/sed -n 1p`
    /usr/bin/docker exec -it $CONTAINER_ID env TERM=xterm bash
    # enter the container the user specified
    /usr/bin/docker exec -it $1 env TERM=xterm bash

Now make it executeable:

sudo chmod +x /usr/bin/docker-tty

Now you can just run docker-tty $CONTAINER_ID to enter into new tty. but if you want to enter the last container you you run the command docker-tty without having to lookup the id.

No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...