Visual Studio Code 远程 - 容器 - 更改 shell

7
在“VS Code远程开发”中启动附加容器时,有人找到了在启动vscode集成终端时更改容器外壳的方法吗?
似乎运行类似于以下内容。
docker exec -it <containername> /bin/bash

我正在寻找与之等价的内容

docker exec -it <containername> /bin/zsh

我发现附加容器的唯一设置是:
"remote.containers.defaultExtensions": []

1
可能是如何为VS Code SSH远程配置不同的Shell?的重复问题。 - Matt Bierner
4个回答

5
2023更新 - 在devcontainer.json中,设置现在需要嵌套在{'customizations': {'vscode': {'settings': {}}}}下面。
示例:
"customizations": {
        "vscode": {
            "settings": {
                "terminal.integrated.defaultProfile.linux": "zsh",
                "terminal.integrated.profiles.linux": { "zsh": { "path": "/bin/zsh" } }
            }
        }
    },

https://containers.dev/supporting


当我在vscode中运行这个命令,并启动一个新的终端时,它仍然是bash。vscode ➜ /workspaces/alpine-4 $ echo $SHELL. /bin/bash. vscode ➜ /workspaces/alpine-4 $ /bin/zsh. vscode ➜ /workspaces/alpine-4 $ echo $SHELL. /bin/bash. - undefined

4

我很乐意为这个主题做出贡献,因为我花了相当长的时间在网上搜寻一个涉及VS Code新API终端集成配置文件(terminal.integrated.profiles.linux)的好解决方案。

截至2022年1月20日,注释和未注释的JSON都可以正常使用。未被注释的行是使用Dev容器使其正常工作的新的非废弃方式。

{
"settings": { 
        // "terminal.integrated.shell.linux": "/bin/zsh"
        "terminal.integrated.defaultProfile.linux": "zsh", 
        "terminal.integrated.profiles.linux": {
            "zsh": {
                "path": "/bin/zsh"
            },
        }
    }
}

如果有人感兴趣,我还找出了如何将 oh my ZSH 构建到镜像中。

Dockerfile:

# Setup Stage - set up the ZSH environment for optimal developer experience
FROM node:16-alpine AS setup

RUN npm install -g expo-cli
# Let scripts know we're running in Docker (useful for containerized development)
ENV RUNNING_IN_DOCKER true
# Use the unprivileged `node` user (pre-created by the Node image) for safety (and because it has permission to install modules)
RUN mkdir -p /app \
    && chown -R node:node /app
# Set up ZSH and our preferred terminal environment for containers
RUN apk --no-cache add zsh curl git
# Set up ZSH as the unprivileged user (we just need to start it, it'll initialize our setup itself)
USER node
# set up oh my zsh
RUN cd ~ && wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh && sh install.sh
# initialize ZSH
RUN /bin/zsh ~/.zshrc

# Switch back to root
USER root

非常好,谢谢。对我来说,只需设置 terminal.integrated.defaultProfile.linux 即可,似乎 terminal.integrated.profiles.linux 有很好的默认值(而 path 可以只是一个命令而不是绝对路径)。 - Hal

3

I worked around it with

RUN echo "if [ -t 1 ]; then" >> /root/.bashrc
RUN echo "exec zsh" >> /root/.bashrc
RUN echo "fi" >> /root/.bashrc

仍然有兴趣知道是否有一种方法可以针对每个容器进行设置。 最初的回答。

2
我在开发环境中使用 Docker 容器,并在我的 Dockerfile 中将 shell 设置为 bash
# …
ENTRYPOINT ["bash"]

当VS Code连接到我的容器时,它坚持使用让我疯狂的/bin/ash shell......然而解决方法(至少对我来说)非常简单但不明显:enter image description here。从.devcontainer.json参考文档中得知,在我的情况下,我只需要在.devcontainer.json文件中添加以下条目即可:
{"settings": {
    "terminal.integrated.shell.*": "/bin/bash"
  }}

完整的.devcontainer.json文件(供参考)

{
  "name": "project-blueprint",
  "dockerComposeFile": "./docker-compose.yml",
  "service": "dev",
  "workspaceFolder": "/workspace/dev",
  "postCreateCommand": "yarn",
  "settings": {
    "terminal.integrated.shell.*": "/bin/bash"
  }
}

7
这个设置对我不再起作用了。这里是新版本,可以正常运行:"settings": { "terminal.integrated.defaultProfile.linux": "bash", "terminal.integrated.profiles.linux": { "bash": { "path": "bash" } } } - Jessitron
2
你如何将zsh设置为默认shell? - Jeff

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