Goldy.exe

How to open a terminal in a running docker container.

Jan 11 2024

Like docker and anime girls? This article will change your life. This totally isn't clickbait... why would I do that.

Ha ha, now that you've been tricked into this computer science lesson, it's time to teach you about the docker system...

just kidding let's get to the real article...

The quick answer.

Getting straight to the point if you have a running container and you would like to open an interactive bash terminal inside of it, you can use this command:

docker exec -it <container> bash

HOW?!?!

In the example above we use docker exec, which is a docker command that executes a command within the container from it's working directory.

Let's say I have a container named "sandy-cheeks" and I wanted to run ls inside her to check out the current files in it's working directory. I can use the docker exec command to achieve that like so:

docker exec sandy-cheeks ls

Output:

sandy.exe  logs.txt

Okay but wth is -it?

Let's explain the use of the -it argument in the original example. We can start by separating the i and t as these two characters are actually two separate command options, which is why this is also possible:

docker exec -i -t <container> bash

but what do they do?

-i is short for the --interactive command option; as it's name suggests it makes the command interactive or in more technical terms it keeps the STDIN (standard input) open. TLDR: it sends whatever we type into the terminal to the process or command we have executed.

Now -t is short for --tty. Docker states this is an option that allocates a "pseudo-TTY". The pseudo-TTY here in question basically forms the bridge between the user's terminal and the process, in this example the bash shell.

tty_in_action

It's here to form an actual user-friendly interface to the process, so these two command options are commonly used together.

docker run -it also exists.

If your container is not currently running you can actually tie in the -it command option with the docker run command too like so:

docker run -it <image> bash

Welp, there's your docker tip. Now let's containerize you 🫵 under docker and block your stdout and stderr so you may no longer speak. 😐

This is Jassim, Goldy over https 🔒, thanks for reading my quick article and byeee! 👋