如何在Docker桌面版中打开CLI时将默认的/bin/sh更改为/bin/bash?

20

如果我选择任何一个容器并点击 CLI 按钮,则默认打开 /bin/sh shell。有没有办法手动重新配置 Docker 以打开 /bin/bash


1
不确定是否有选项,但是有一个命令:docker exec -it CONTAINER_NAME bash - anemyte
必须先安装容器命令提示符才能使用它。大多数容器默认使用/bin/sh终端提示符,您可以尝试任何其他默认使用bash的容器,例如Ubuntu或其他某些容器。我怀疑我们无法从Docker本身更改默认设置。 - Vinod
2
@anemyte 是的,我知道,但是从Docker桌面应用程序中点击CLI链接对我来说更方便。 - Hubert Jakubiak
2
Docker Desktop 应该在设置中提供这个选项。在 Visual Studio 中,容器选项卡中的“在终端中打开”选项默认使用 bash。 - Jacob Stamm
3个回答

9

这将取决于基础镜像,如果没有bash可用,则可以构建自定义镜像并添加bash。然后创建一个链接以使用bash而不是sh。

FROM <BASE_IMAGE>
RUN apk add --no-cache bash

RUN ln -sf /bin/bash /bin/sh


6

虽然不完全回答了你的问题,但对我而言,在sh终端中键入bash就可以打开sh上的bash。唯一的麻烦是我必须要两次输入exit


3

我通过使用以下Dockerfile命令创建一个新的镜像,将默认shell更改为bash

# Dockerfile
FROM <parent image>

# make /bin/sh symlink to bash instead of dash:
RUN echo "dash dash/sh boolean false" | debconf-set-selections
RUN DEBIAN_FRONTEND=noninteractive dpkg-reconfigure dash

# set ENV to execute startup scripts
ENV ENV ~/.profile

并创建一个新图像。

$ docker build --tag <image> .
  1. change symbolic link of /bin/sh to bash

    https://superuser.com/questions/715722/how-to-do-dpkg-reconfigure-dash-as-bash-automatically

  2. source ~/.profile file when executing /bin/sh

    Is there an alternative for .bashrc for /bin/sh?

  3. If you want to clear ENV after startup, you may add below line to the ~/.bashrc file. (Be careful not to mess up other scripts)

    unset ENV
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接