How to enter a Docker container already running with a new TTY?

Better Stack Team
Updated on April 7, 2023

To enter a Docker container already running with a new TTY, you can use the docker exec command. Here's an example:

 
docker exec -it <container_id_or_name> sh

Replace <container_id_or_name> with the ID or name of the container you want to enter. The -it option tells Docker to allocate a new TTY for the container and run a shell inside the container.

In this example, the sh command is used to start a shell inside the container. Depending on the operating system and software installed in the container, you may need to use a different command to start a shell. For example, on an Ubuntu-based container, you can use bash instead of sh.

Once you run this command, you should see a prompt that looks similar to the following:

 
root@<container_id_or_name>:/#

This indicates that you are now inside the container and can run commands as if you were logged in to the container directly. When you are done working inside the container, you can exit the shell by running the exit command. This will return you to the host machine's shell.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack