简体   繁体   English

如何在 Docker 容器中获取交互式 bash shell

[英]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.我正在尝试连接到正在运行的容器或使用 bash shell(而不是 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.并获得一个交互式 bash shell。

What I've tried so far:到目前为止我尝试过的:

Perthis post I've tried根据我尝试过的这篇文章

Adding this to my Dockerfile将此添加到我的 Dockerfile

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

Adding this to my Dockerfile将此添加到我的 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.但是每次我尝试以交互模式运行容器( docker run -it或附加到正在运行的容器( docker exec -it )时,我都会进入sh shell。

How can I get an interactive bash shell that is running inside a docker container?如何获得在 docker 容器内运行的交互式 bash shell?

Update: Minimal working Dockerfile更新:最小工作 Dockerfile

FROM ubuntu

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

You are in fact running an interactive bash with commands like:实际上,您正在使用以下命令运行交互式 bash:

docker container run -it ubuntu /bin/bash

When you run bash in a docker container, that shell is in a container.当您在 docker 容器中运行 bash 时,该 shell 位于容器中。 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.它也不会有您的提示, PS1变量不会自动导出到容器的环境中。 And it won't have your .bashrc configuration from your host, since that isn't inside the container.它不会有来自主机的.bashrc配置,因为它不在容器内。 Instead you get a bash shell that is out of the box from a minimal ubuntu host.相反,您会从最小的 ubuntu 主机获得一个开箱即用的 bash shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM