Run Bash Shell In Docker

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: May 20, 2024

The short answer

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

Bash
$ docker exec -it <container> bash

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

Use Warp's Workflows feature to easily recall the 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 CTRL-SHIFT-R, typing start bash docker, then pressing ENTER to use the suggested command:

Running a Bash shell on container startup

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

Bash
$ docker run -it <image> bash

Where image is the name of the image you want to start a container from.

Running sh and other shells

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

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

Overriding the entrypoint

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

For example, if the Docker image has the following ENTRYPOINT:

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

Then, by default, the container will start by running the node app.js command.

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

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

Running a single command in a container with Bash

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 -c flag (short for command) of the bash utility which will execute the specified command.

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

Where:

  • container is either the name or the identifier of a container.
  • command is the command you want to run in the container.

For example, to restart a running service:

Bash
$ 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.

Running Bash as a 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 bash image available on Docker hub.

Bash
$ docker run -it bash
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Start a Docker Container

Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.