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.

#!/bin/bash 

EXPECTED_NUM_ARGS=1;

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
else
    # enter the container the user specified
    /usr/bin/docker exec -it $1 env TERM=xterm bash
fi

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 spawned.so 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...

Popular