How to enter in a Docker container already running with a new TTY
Categories:
Attaching to a Running Docker Container with a New TTY

Learn how to connect to an already running Docker container and allocate a new pseudo-TTY, enabling interactive shell access for debugging and administration.
When working with Docker containers, you often need to interact with a container that is already running in the background. While docker attach
allows you to connect to the primary process's standard input/output, it doesn't always provide a new interactive shell (TTY) if the container wasn't started with one. This article will guide you through the process of attaching to a running container and allocating a fresh TTY, which is crucial for running new commands or debugging.
Understanding TTY in Docker
A TTY (teletypewriter) or pseudo-TTY (PTY) is a virtual terminal that allows interactive communication between a user and a program. When you run a Docker container with the -it
flags (-i
for interactive, -t
for TTY), Docker allocates a pseudo-TTY for the container's primary process, typically a shell. This enables you to type commands and see their output directly.
However, if a container is started without -it
(e.g., docker run -d my_image
), its primary process might not have a TTY allocated. If you then try to docker attach
to it, you might not get an interactive shell, or the session might terminate if the primary process exits. To run new commands or get a fresh shell, you need to execute a new process within the container with its own TTY.
flowchart TD A[Docker Container Running] --> B{Primary Process Has TTY?} B -- Yes --> C[docker attach: Interactive Session] B -- No --> D[docker attach: Non-Interactive/Limited] D --> E[Need Interactive Shell?] E -- Yes --> F[docker exec -it: Allocate New TTY] F --> G[New Interactive Shell Session]
Decision flow for attaching to Docker containers and TTY allocation.
Using docker exec
for Interactive Access
The docker exec
command is the primary tool for running new commands inside a running container. By combining it with the -it
flags, you can allocate a new pseudo-TTY for the command you execute, typically a shell like bash
or sh
. This allows you to open a completely new interactive session without affecting the container's primary process.
1. Identify the Container
First, you need to know the name or ID of the running container you want to access. You can list all running containers using docker ps
.
2. Execute a New Interactive Shell
Once you have the container's name or ID, use docker exec -it
followed by the container identifier and the shell command you want to run. Common shells are bash
or sh
.
3. Exit the Session
To exit the interactive shell session without stopping the container, simply type exit
and press Enter. This will close the shell process, but the container will continue running its primary process.
# List running containers to find the container ID or name
docker ps
# Example output:
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# a1b2c3d4e5f6 nginx "/docker-entrypoint...." 2 hours ago Up 2 hours 80/tcp my-nginx-container
# Execute a bash shell inside 'my-nginx-container'
docker exec -it my-nginx-container bash
# If bash is not available, try sh
docker exec -it my-nginx-container sh
Commands to identify a container and execute an interactive shell.
bash
first, as it's more feature-rich. If bash
is not installed in the container image, sh
is usually a safe fallback as it's part of most minimal Linux distributions.Distinction Between docker attach
and docker exec
It's important to understand the fundamental difference between docker attach
and docker exec
.
docker attach
: Connects to the standard input, output, and error streams of the primary process running in the container. If the primary process doesn't have a TTY, or if it's a non-interactive process,attach
might not give you a useful interactive shell. Exiting anattach
session can sometimes stop the container if the primary process is configured to exit onSIGHUP
.docker exec
: Runs a new command inside a running container. When used with-it
, it allocates a new TTY for this new command (e.g.,bash
). This means you get a fresh, independent interactive session. Exiting anexec
session will only terminate the executed command (e.g., thebash
shell) and will not affect the container's primary process or stop the container.

Key differences between docker attach
and docker exec
.