1

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"]
8
  • 1
    Have a full Dockerfile (or, rather, one complete enough to constitute a minimal reproducible example, but no larger than the shortest possible thing that achieves that goal)? Not every container has a copy of bash at all; unless we know what you're deriving FROM, or if you're taking explicit steps to install bash where appropriate, we can't say if yours does or not. Mar 3, 2020 at 21:15
  • BTW, what output do you get from RUN ["/bin/bash", "-c", "echo \"$BASH_VERSION\""]? You say you tried the relevant RUN, but you don't tell us what happened. Mar 3, 2020 at 21:18
  • 1
    How did you determine that the shell you ended up in was sh? Mar 3, 2020 at 21:19
  • I figured it is the sh shell by appearance (white plain text vs. the green boldface text that you normally see in the bash terminal in ubuntu) and the fact that I am unable to repeat the previous command by pressing the up arrow key, etc. Also, I updated with a minimal working Dockerfile. Mar 3, 2020 at 21:23
  • What does echo $BASH_VERSION outputs in the shell you think is sh?
    – KamilCuk
    Mar 3, 2020 at 21:24

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.