Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:
How to Enter a Running Docker Container With a TTY
Last updated: February 9, 2025
1. Overview
A TTY (TeleTypewriter) is any terminal that sends and receives typed messages. Docker is an operating system (OS) virtualization platform that we can use to develop, package, distribute, and run software as containers. Furthermore, we can connect to a running Docker container using an interactive pseudo-TTY that gives access to the OS running in the container.
In this tutorial, we’ll learn how to enter a running Docker container with a TTY. The only prerequisite is to install Docker on a supported OS platform (Linux, Windows, etc.).
We’ll discuss several methods for accessing a Docker container using a TTY. Further, we can select the method that best fits our use case.
2. Launching a TTY When Starting a Container
We can launch a TTY at the time of starting a container with the docker run command. We do so using the command options –interactive (or -i) and –tty (or -t) together. Further, the -i option keeps STDIN open even if not attached, and the -t option allocates a pseudo-TTY.
Let’s demonstrate with an example using a Docker container for MySQL database. Accordingly, to start a TTY for a Linux container, we suffix the command with bash:
$ docker run
--name mysql
-it
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes
mysql:8.4 bash
The result is a command prompt for a bash TTY:
bash-5.1#
3. Launching a TTY After Starting a Container
We can launch a TTY after starting a container with the docker exec command. We do so using the same command options –interactive (or -i) and –tty (or -t) together.
Again, let’s demonstrate with an example using a Docker container for MySQL database. However, first, we need to start a container. This time, we run a container in detached mode using the -d option:
$ docker run
--name mysql
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes
-d
mysql:8.4
0b48ce54a9b409a291072fd9d517ccf77267f24a528a2894cb213d1201bf0029
The output string listed is the container ID. We can verify that the container is running using the docker ps command to list the running containers:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b48ce54a9b4 mysql:8.4 "docker-entrypoint.s…" 17 seconds ago Up 17 seconds 3306/tcp, 33060/tcp mysql
Afterward, we launch a TTY within the running container using the docker exec command:
$ docker exec
-it
mysql
bash
As before, the result is a command prompt for a bash TTY:
bash-5.1#
We can connect to MySQL and run SQL commands in this TTY against the MySQL database server:
bash-5.1# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.4.4 MySQL Community Server - GPL
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4. Using a Bash Shell With Containers That Exit
Let’s discuss a different use case in which a container exits automatically a short time after being started. This is so because some containers, such as an ubuntu image-based container, are designed to exit. To demonstrate, let’s run an ubuntu container:
$ docker run
--name ubuntu-demo
ubuntu
echo "Hello World"
The container starts, outputs a “Hello World” message, and exits. To verify, let’s list all containers running and exited:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ccec7c3f04c ubuntu "echo 'Hello World'" 15 seconds ago Exited (0) 14 seconds ago ubuntu-demo
Even so, we can launch a TTY at the time of starting an ubuntu container by using the -it options along with opening a bash shell within the container:
$ docker run
-it
--name ubuntu-demo
ubuntu
/bin/bash
-c "echo 'Hello World'; /bin/bash"
Hello World
root@0e4625c6d38d:/#
5. Using the sleep Command
We can start a TTY in an ubuntu container by making the container continue to run using the sleep command in a bash shell within the started container, which suspends program execution for the specified number of seconds:
$ docker run
-d
--name ubuntu-demo
ubuntu
/bin/bash
-c "echo 'Hello World'; sleep 10000"
Let’s verify that the container is running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84726b32ca27 ubuntu "/bin/bash -c 'echo …" 7 seconds ago Up 6 seconds ubuntu-demo
Afterward, we launch a TTY within this container using the docker exec command:
$ docker exec
-it
ubuntu-demo
bash
We can run Linux commands, for example, ls -l to list files at the TTY command prompt:
root@84726b32ca27:/# ls -l
total 48
lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bin
drwxr-xr-x 2 root root 4096 Apr 22 2024 boot
drwxr-xr-x 5 root root 340 Jan 27 13:03 dev
6. Using the cat Command
Furthermore, we use the cat command, a short form for concatenate, to read what a user types at the TTY and output the message read to stdout (standard output). To demonstrate, let’s run an ubuntu container:
$ docker run
-it
--name ubuntu-demo
ubuntu
/bin/bash
-c "echo 'Hello World'; cat"
The container starts and outputs a “Hello World” message, and a TTY terminal is launched that allows a user to type a message. Let’s type some messages, and the messages get output at the TTY itself:
Hello
Hello
Hi
Hi
7. Using the tail Command
We can use the tail command along with the –follow (or -f) option to read from stdin on a TTY, output the last section of the message, and follow to monitor the stdin for more messages. We use the /dev/null device file in Linux, which reports that data has been written and discards all data. To demonstrate, let’s again run an ubuntu container:
$ docker run
-it
--name ubuntu-demo
ubuntu
/bin/bash
-c "echo 'Hello World'; tail -f /dev/null"
The container starts, outputs a “Hello World” message, and monitors further input on the TTY by the user. It displays what a user types:
Hello World
Hello
Hi
8. Launching a TTY in a Windows Container
We can launch a TTY within a Windows container using the -it option as well. However, we use cmd.exe instead of bash to run a command on Windows. Let’s demonstrate with an example:
$ docker run
-it
mcr.microsoft.com/windows/nanoserver:ltsc2022
cmd.exe
As a result, it launches a TTY within the Windows container running the Windows OS:
Microsoft Windows [Version 10.0.20348.2849]
(c) Microsoft Corporation. All rights reserved.
C:\>
9. Conclusion
In this article, we learned about the different methods to launch a TTY within a Docker container. We can launch a TTY before or after starting a container. We also discussed the different Linux commands that let a user type messages at a TTY. Furthermore, we can run a TTY within a Windows OS container.
As always, the Docker commands’ scripts used in this article are available over on GitHub.