Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an interactive bash shell in a Docker container

I'm trying to connect to a running container or start a new container in interactive mode with the bash shell -- not the sh shell. I want to run:

docker exec -it <container_name> /bin/bash

or

docker run -it <container_name> <image_name>

or

docker run -it <container_name> <image_name> /bin/bash

and get an interactive bash shell.

What I've tried so far:

Per this post I've tried

Adding this to my Dockerfile

SHELL ["/bin/bash", "-c"]

Adding this to my Dockerfile

RUN ["/bin/bash", "-c", "echo I am now using bash!"]

But every time I try to run a container in interactive mode (docker run -it or attach to a running container (docker exec -it), I land in the sh shell.

How can I get an interactive bash shell that is running inside a docker container?

Update: Minimal working Dockerfile

FROM ubuntu

SHELL ["/bin/bash", "-c"]
like image 507
Jacob Stern Avatar asked Nov 02 '25 07:11

Jacob Stern


1 Answers

You are in fact running an interactive bash with commands like:

docker container run -it ubuntu /bin/bash

When you run bash in a docker container, that shell is in a container. So it won't have the command history from outside of the container, that history is maintained on the host filesystem. It also won't have your prompt, the PS1 variable is not automatically exported into the container's environment. And it won't have your .bashrc configuration from your host, since that isn't inside the container. Instead you get a bash shell that is out of the box from a minimal ubuntu host.

like image 72
BMitch Avatar answered Nov 03 '25 20:11

BMitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!