Terminus by Warp
Run Bash Shell In Docker

Run Bash Shell In Docker

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

The short answer

To start an interactive Bash shell in a Docker container, you can use the [.inline-code]docker exec[.inline-code] command that allows developers to execute commands in running containers.

 $ docker exec -it <container> bash

Where [.inline-code]container[.inline-code] is either the name or the identifier of a Docker container that can be obtained using the [.inline-code]docker ps[.inline-code] command.

[#easily-recall-syntax]Use Warp's Workflows feature to easily recall the syntax[#easily-recall-syntax]

If you’re using Warp as your terminal and you need to quickly retrieve this command, you can use Warp's Workflows feature by pressing [.inline-code]CTRL-SHIFT-R[.inline-code], typing [.inline-code]start bash docker[.inline-code], then pressing [.inline-code]ENTER[.inline-code] to use the suggested command:

[#bash-shell-on-container]Running a Bash shell on container startup[#bash-shell-on-container]

To start a Docker container with an interactive Bash shell, you can combine the [.inline-code]-i[.inline-code] flag (short for interactive) and the [.inline-code]-t[.inline-code] flag (short for TTY) of the [.inline-code]docker run[.inline-code] command, which instructs Docker to allocate a pseudo-TTY connected to the container’s standard input (i.e. stdin).

 $ docker run -it <image> bash

Where [.inline-code]image[.inline-code] is the name of the image you want to start a container from.

[#sh-and-others]Running [.inline-code]sh[.inline-code] and other shells[#sh-and-others]

Most images usually come pre-packaged with several shell binaries such as [.inline-code]sh[.inline-code], [.inline-code]csh[.inline-code], etc. To start a Docker container with an interactive shell other than Bash, simply replace the [.inline-code]command[.inline-code] argument by the shell you want to use when running the [.inline-code]docker run[.inline-code] command.

 $ docker run -it <image> /bin/sh

[#override-entrypoint]Overriding the entrypoint[#override-entrypoint]

Some Docker images may include an [.inline-code]ENTRYPOINT[.inline-code] instruction that specifies which command to run when the container is started.

For example, if the Docker image has the following [.inline-code]ENTRYPOINT[.inline-code]:

 ENTRYPOINT ['node', 'app.js']

Then, by default, the container will start by running the [.inline-code]node app.js[.inline-code] command.

To override this entry point and start the container with a Bash shell instead of the default command, you can use the [.inline-code]--entrypoint[.inline-code] flag as follows:

 $ docker run --entrypoint /bin/bash -it <image>

[#run-single-command]Running a single command in a container with Bash[#run-single-command]

It may happen that you need to run a single command in a running Docker container. Instead of starting an interactive shell session, you can use the [.inline-code]-c[.inline-code] flag (short for command) of the [.inline-code]bash[.inline-code] utility which will execute the specified command.

 $ docker exec <container> /bin/bash -c '<command>'

Where:

  • [.inline-code]container[.inline-code] is either the name or the identifier of a container.
  • [.inline-code]command[.inline-code] is the command you want to run in the container.

For example, to restart a running service:

 $ docker exec ab4fb7dd8ffa /bin/bash -c 'service ssh restart'

If you want to learn how to execute entire scripts instead of single commands, you can read our other article on how to run a shell script using a Dockerfile.

[#bash-as-container]Running Bash as a container[#bash-as-container]

If you want to run the Bash shell as a standalone container to test new features of more recent versions or test shell scripts against different Bash versions to ensure compatibility, you can use the official [.inline-code]bash[.inline-code] image available on Docker hub.

 $ docker run -it bash

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.