在 ubuntu 中尝试 运行 作为 vs 代码开发容器的远程用户时出现错误

I get an error when trying to run as a remote user for vs code development containers in ubuntu

我正在尝试创建一个 docker 容器用作我的远程开发容器。我不断收到错误消息,而且我不确定如何解决它。如果我没有在 devcontainer.json 文件中设置 "remoteUser": "developer" 设置,那么它会起作用,因为它不会尝试 运行 作为远程用户的开发容器。不过,我不想 运行 开发容器作为 root。

mkdir: cannot create directory '/home/developer': Permission denied

Dockerfile:

FROM ubuntu:latest

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get -y update \
    && apt-get -y install --no-install-recommends apt-utils dialog git wget \
    #
    # Add remote development (developer) group/user.
    && groupadd -g 1000 -o developer \
    && useradd -u 1000 -g 1000 developer \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

完整输出:

[18 ms] Start: Resolving remote
[20 ms] Setting up container for folder or workspace: C:\Repos\Sample

[21 ms] Start: Check Docker is running
[21 ms] Start: Run: docker info
[666 ms] Start: Run: docker ps -q -a --filter label=vsch.local.folder=C:\Repos\Sample --filter label=vsch.quality=stable
[1087 ms] Start: Run: docker inspect --type container 1739eb19eccb
[1504 ms] Start: Starting container
[1504 ms] Start: Run: docker start 1739eb19eccb79a4c0c6390275caf5224c0b5d94385ee28064a8fb504704c12a
[3063 ms] Start: Run: docker ps -q -a --filter label=vsch.local.folder=C:\Repos\Sample --filter label=vsch.quality=stable
[3503 ms] Start: Run: docker inspect --type container 1739eb19eccb
[3915 ms] Start: Inspecting container
[3916 ms] Start: Run: docker inspect --type container 1739eb19eccb79a4c0c6390275caf5224c0b5d94385ee28064a8fb504704c12a
[4342 ms] Start: Run: docker exec -i -u developer -e VSCODE_REMOTE_CONTAINERS_SESSION=34411177-6cf8-408f-8914-a454274269cf1601787357110 1739eb19eccb79a4c0c6390275caf5224c0b5d94385ee28064a8fb504704c12a /bin/sh
[4352 ms] Start: Run in container: uname -m
[5739 ms] x86_64
[5740 ms] 
[5740 ms] Start: Run in container: (cat /etc/os-release || cat /usr/lib/os-release) 2>/dev/null
[5743 ms] NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
[5744 ms] 
[5744 ms] Start: Run in container: cat /etc/passwd
[5748 ms] Start: Setup shutdown monitor
[5754 ms] Forking shutdown monitor: c:\Users\michael\.vscode\extensions\ms-vscode-remote.remote-containers-0.140.1\dist\shutdownMonitorProcess \.\pipe\vscode-remote-containers-035ad203a5af805d0d3da68a329a3c395d0dd338-sock singleContainer Info c:\Users\michael\AppData\Roaming\Code\logs201003T145211\exthost1\ms-vscode-remote.remote-containers
[5772 ms] Start: Run in container: test -d /home/developer/.vscode-server
[5778 ms] 
[5778 ms] 
[5778 ms] Exit code 1
[5779 ms] Start: Run in container: test -d /home/developer/.vscode-remote
[5790 ms] 
[5791 ms] 
[5791 ms] Exit code 1
[5791 ms] Start: Run in container: set -o noclobber ; mkdir -p '/home/developer/.vscode-server/data/Machine' && { > '/home/developer/.vscode-server/data/Machine/.writeMachineSettingsMarker' ; } 2> /dev/null
[5796 ms] 
[5796 ms] mkdir: cannot create directory '/home/developer': Permission denied
[5796 ms] Exit code 1
[5797 ms] Start: Run in container: test -d /home/developer/.vscode-server/bin/2af051012b66169dde0c4dfae3f5ef48f787ff69
[5799 ms] 
[5800 ms] 
[5800 ms] Exit code 1
[5800 ms] Installing VS Code Server for commit 2af051012b66169dde0c4dfae3f5ef48f787ff69
[5800 ms] Start: Run in container: mkdir -p /home/developer/.vscode-server/bin/2af051012b66169dde0c4dfae3f5ef48f787ff69_1601787364083
[5815 ms] 
[5815 ms] mkdir: cannot create directory '/home/developer': Permission denied
[5815 ms] Exit code 1
[5818 ms] Command in container failed: mkdir -p /home/developer/.vscode-server/bin/2af051012b66169dde0c4dfae3f5ef48f787ff69_1601787364083

命令useradd -u 1000 -g 1000 developer 不会创建用户主目录。 /homeroot 用户所有,因此在您 docker exec 作为非特权 developer 用户进入容器并尝试在 /home 目录下创建目录后得到“权限被拒绝”。

您可以通过修改 useradd 命令来创建具有正确权限的用户主目录:useradd -u 1000 -d /home/developer -m -k /etc/skel -g 1000 developer.

摘自 useradd man 页:

  • -d:将使用 HOME_DIR 作为用户登录目录的值来创建新用户。默认是将 LOGIN 名称附加到 BASE_DIR 并将其用作登录目录名称。目录 HOME_DIR 不必存在,但如果不存在则不会创建。
  • -m:如果用户的主目录不存在则创建。骨架目录中包含的文件和目录(可以用 -k 选项定义)将被复制到主目录。
  • -k:骨架目录,包含用户主目录下要复制的文件和目录,当主目录由useradd创建时。