How to Enter a Docker Container Already Running with a New TTY

TLDR
To enter a running Docker container with a new TTY session, use the docker exec command with the -it flags. This allows you to interact with the container's shell for debugging or administrative tasks.
When working with Docker, you may need to interact with a running container to debug issues, inspect files, or perform administrative tasks. This guide will show you how to attach to a running container with a new TTY session.
Prerequisites
Before proceeding, make sure you have:
- Docker installed on your system.
- A running Docker container you want to access.
- Appropriate permissions to interact with Docker.
Step 1: List Running Containers
To identify the container you want to access, list all running containers using the following command:
docker ps
This will display a list of running containers, including their IDs, names, and other details. For example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123 nginx:latest "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:8080->80/tcp my-nginx
Note the CONTAINER ID or NAMES of the container you want to access.
Step 2: Attach to the Container with a New TTY
To open a new TTY session in the running container, use the docker exec command with the -it flags:
docker exec -it <container-name-or-id> /bin/bash
Replace <container-name-or-id> with the CONTAINER ID or NAMES from the previous step. For example:
docker exec -it my-nginx /bin/bash
This command starts a new interactive shell session (/bin/bash) inside the container.
Example Output
root@abc123:/#
You are now inside the container and can run commands as if you were logged into a separate machine.
Step 3: Use an Alternative Shell (if Necessary)
Not all containers include /bin/bash. If the container uses a minimal base image, you may need to use /bin/sh instead:
docker exec -it <container-name-or-id> /bin/sh
For example:
docker exec -it my-nginx /bin/sh
Step 4: Exit the Container
To exit the container's shell session, type:
exit
This will close the TTY session and return you to your host machine's terminal.
Additional Tips
Inspect Container Logs: Use
docker logs <container-name-or-id>to view logs if you don't need an interactive session.Attach vs. Exec: The
docker attachcommand connects to the container's main process, whiledocker execstarts a new process inside the container.Use Docker Compose: If you're using Docker Compose, you can access a container with:
docker-compose exec <service-name> /bin/bash
By following these steps, you can easily interact with running Docker containers for debugging or administrative purposes.
Related Resources
- How to Access Docker Container Shell: more shell access methods
- Docker TTY Error Fix: troubleshoot TTY issues
- How to Attach and Detach Docker Process: manage attached sessions
- Exploring Docker Container File System: browse container files
- Introduction to Docker Guide: Docker fundamentals
Try it hands-on
Run the commands from this article in the browser. Nothing to install.
Docker Terminal Simulator
Practice Docker commands in an interactive browser terminal. Learn images, containers, logs, exec, Dockerfile builds, volumes, networks, and Docker Compose with a live Docker daemon visualization.
How Docker Works Under the Hood
Watch what really happens when you run docker run -p 8080:80 nginx, one layer at a time. Step down the whole stack: the CLI, the daemon, the registry pull, containerd, the OCI runtime bundle, runc, the running container, and the shared Linux kernel. Every stage shows the real low-level command you can run yourself, so it doubles as a tour of the primitives that make a container: namespaces, cgroups, and runc. A container is not a small VM, and this shows you why.
We earn commissions when you shop through the links below.
Svix
Webhooks as a service
Svix Dispatch sends your webhooks for you: retries with exponential backoff, signed payloads, idempotency keys, and a delivery log your customers can see.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Difference Between RUN and CMD in a Dockerfile
Understand the key differences between RUN and CMD in a Dockerfile, and learn when to use each for building and running Docker containers.
Docker Security Hardening Checklist
Comprehensive security checklist for hardening Docker containers, images, and runtime environments.
60-90 minutes
Running Docker Containers on Your Linux Server
Install Docker and Docker Compose on Ubuntu, run your first container, deploy a WordPress stack with docker-compose, and set up Nginx as a reverse proxy in front of your containers.
60 minutes