Connect to a Shell inside Docker container

In previous article, we saw how to run a Docker container in Ubuntu. Therein, we used echo command to get the output. But, we didn’t connect with the shell inside the Docker container itself. The command stays the same docker container run but, with some additional options.

The two options we will use is –interactive or -i and –tty or -t.

Connect to a Shell inside the Docker container

Use following syntax to connect to a Shell inside the Docker container –

docker container run --interactive --tty <container-image>

or,

docker container run -i -t <container-image>

For instance, for Ubuntu container it would be –

docker container run --interactive --tty ubuntu

or,

docker container run -i -t ubuntu

Here, –interactive option basically keeps the Standard Input (STDIN) open. In other words, it opens a connection to the Docker container. Besides, –tty is used to allocate a Pseudo-TTY. It helps us connect to a Shell inside the container.

Since, we have provided the example of Ubuntu container therefore, the default Shell would be BASH. But, it (i.e. the Shell) would differ depending on the container image we use.

In conclusion, we have covered how to connect to a Shell inside the Docker container here.

Additional Info –

We can also assign a name to the container, this can be done through –name option. Though it is not necessary to use other options (i.e. –interactive and –tty) mentioned above, but since we have covered these in the article so we include them as well –

docker container run --name <container-name> --interactive --tty <container-image>

For instance,

docker container run --name XYZ --interactive --tty ubuntu

To view the container name defined, use the following command –

docker container ls -l

Look for container name in the last field NAMES.

Similar Posts