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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github