HOWTO · Docker

How to Launch Bash Terminal in New Docker Container

This tutorial demonstrates to launch bash terminal in new docker containers.

On this page

To open Bash in a Docker container, choose the command from the container’s state. Use docker run -it IMAGE bash to start a new container with an interactive Bash session. Use docker exec -it CONTAINER bash to start a separate Bash session in a container that is already running. Use docker attach CONTAINER only when you want to connect your terminal to the container’s main process; it does not start another shell.

Replace IMAGE with an image that is available to Docker and contains Bash. Replace CONTAINER with a running container’s name or ID. The commands below are syntax templates: their placeholders must be replaced, and they are not shown as executed output.

Use the Bash Interactive Shell

Use docker run -it IMAGE bash when you want a new container and an interactive Bash prompt in it. The -i option keeps standard input open, and -t allocates a terminal, which makes the session behave like an interactive command line. bash is the command Docker starts inside the new container.

For example, substitute an image name for IMAGE in docker run -it IMAGE bash. The image must include Bash; a minimal image may provide only sh or another shell. This command creates and starts a container, so it is different from opening a shell in a container that already exists. If you want to reuse a stopped container, start it first, then use docker exec once it is running.

Use the docker exec Command

Use docker exec -it CONTAINER bash to start Bash as a new process in an already running container. docker exec does not start a stopped container. To find running container names and IDs, use docker ps; use docker ps -a when you also need to see stopped containers. Start the intended stopped container with docker start CONTAINER, then run the docker exec command.

If Bash is not installed in the image, try docker exec -it CONTAINER sh when the image provides sh. The shell command must exist in the container. Both docker exec forms create a separate interactive process, so they are useful for inspection or troubleshooting while the container’s main process continues to run.

Use the docker attach Command

Use docker attach CONTAINER to connect your terminal’s input and output streams to the main process of a running container. This is useful when that process already provides the interactive session you need. For example, if the container was started with a shell as its main process, attach reconnects to that process.

Attach does not launch Bash or any other new command. If the container’s main process is an application, attach connects to that application’s streams; it does not give you a separate shell prompt. Choose docker exec -it CONTAINER bash (or sh if available) when you need to launch a shell alongside the main process. In short: docker run starts a new container, docker exec starts a command in a running container, and docker attach connects to the existing main process.