You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置devcontainer以适配Docker entrypoint脚本?

解决方案

Dev Container标准默认会覆盖Dockerfile中定义的ENTRYPOINT和CMD,改用自身的启动命令,这就是你的entrypoint脚本未执行、终端仍以root身份启动的原因。你可以通过修改devcontainer.json来解决这个问题:

修改devcontainer.json配置

在现有配置中添加两个关键项:

{
    "name": "dev",
    "build": {
        "dockerfile": "../Dockerfile"
    },
    "overrideCommand": false,
    "remoteUser": "user",
    "customizations": {
        "codespaces": {
            "openFiles": []
        }
    }
}

配置项说明

  • overrideCommand: false:禁止Dev Container覆盖Dockerfile中的ENTRYPOINT和CMD,确保你的entrypoint.sh脚本在容器启动时被执行,自动创建非root用户并切换身份。
  • remoteUser: "user":指定VS Code终端默认使用的用户为脚本中创建的user,避免手动切换用户的操作。

额外注意事项

如果容器启动时出现gosu: command not found错误,说明你的基础镜像未预装gosu工具,需要在Dockerfile中添加安装步骤:

FROM bitnami/minideb:bullseye
ENV LANG C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends gosu \
    && rm -rf /var/lib/apt/lists/*
COPY entrypoint.sh /bin/entrypoint.sh
RUN chmod +x /bin/entrypoint.sh
ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["/bin/bash"]

验证效果

启动Codespaces实例后,终端会自动以user身份登录,执行whoami命令会返回user,查看/etc/passwd也能看到该用户的条目,与本地手动构建容器的效果一致。

内容的提问来源于stack exchange,提问作者maciek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:58:14