我正在尝试创建一个docker容器作为我的远程开发容器。我不断收到错误,但不确定如何解决。如果我没有在 devcontainer.json 文件中设置"remoteUser": "developer"
设置,那么它可以工作,因为它没有尝试以远程用户身份运行开发容器。不过,我不想以根用户身份运行开发容器。
mkdir:无法创建目录"/home/developer":权限被拒绝
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:ReposSample
[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:ReposSample --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:ReposSample --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:Usersmichael.vscodeextensionsms-vscode-remote.remote-containers-0.140.1distshutdownMonitorProcess \.pipevscode-remote-containers-035ad203a5af805d0d3da68a329a3c395d0dd338-sock singleContainer Info c:UsersmichaelAppDataRoamingCodelogs20201003T145211exthost1ms-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
不会创建用户主目录。/home
由root
用户拥有,因此当您以非特权developer
用户身份docker exec
容器并尝试在/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
创建时,包含要在用户主目录中复制的文件和目录。