首页 > 解决方案 > 有没有办法在 VSCode Devcontainers 中调试 PostCreateCommand?

问题描述

我目前遇到这个问题:

Command failed: /bin/sh -c ./.devcontainer/postCreateCommand.sh

哪一个是

    "postCreateCommand": "./.devcontainer/postCreateCommand.sh",

devcontainer.json.

但是当我停用“postCreateCommand”并在创建容器后手动运行它时,该脚本有效。这让我想到,问题可能是脚本的路径。但事实也并非如此。因为只有一个 echo 命令的空脚本似乎可以工作。

脚本如下:

echo "Installing Developer Requirements"

apt-get update && apt-get install -y man git
pip3 install -r .devcontainer/dev_pip_requirements.txt

任何想法如何调试"PostCreateCommand"?输出没有什么帮助,我不想开始将此项目简化为最小的工作示例。

我将 git 更改为在 Windows 上以 linux 文件结尾签出所有内容。我三重检查了 shell 脚本是否有 LF 结尾。所以这些也不应该是一个问题。(他们以前是)。

标签: dockervisual-studio-codedocker-composevscode-devcontainer

解决方案


我认为一个原因可能是用户没有足够的权限来执行apt-get update命令。

将配置文件中的 ,更改remoteUserroot,devcontainer.json应该允许在postCreateCommand.sh使用 运行 shell 脚本时成功执行它bash

  • 我已经尝试使用python:3.8-slim-busterDocker 映像,并non-rootDockerfile.

  • 我已将以下内容添加到./.devcontainer/devcontainer.json

    "postCreateCommand": "bash ./.devcontainer/postCreateCommand.sh",
    "remoteUser": "root",
    
  • 我使用了以下内容./.devcontainer/postCreateCommand.sh

    #!/usr/bin/env bash
    
    set -ex
    
    apt-get update && apt-get install -y man git && rm -rf /var/lib/apt/lists/*
    
    pip install --requirement ./.devcontainer/dev_pip_requirements.txt
    
    

一般root不推荐用于生产环境。

这是有关的更多信息postCreateCommand


推荐阅读