Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MacOS使用非根用户构建docker映像时卡住_Macos_Docker - Fatal编程技术网

MacOS使用非根用户构建docker映像时卡住

MacOS使用非根用户构建docker映像时卡住,macos,docker,Macos,Docker,当基于python:3.7-buster构建docker文件并添加非根用户以使VSCode能够附加到容器时,docker构建过程在以下位置添加非根用户时卡住: Step 9/10 : RUN groupadd --gid $USER_GID $USERNAME && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME && apt-get update && apt-g

当基于
python:3.7-buster
构建docker文件并添加非根用户以使VSCode能够附加到容器时,docker构建过程在以下位置添加非根用户时卡住:

Step 9/10 : RUN groupadd --gid $USER_GID $USERNAME     && useradd --uid $USER_UID --gid 
$USER_GID -m $USERNAME     && apt-get update     && apt-get install -y sudo     && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME     && chmod 0440 /etc/sudoers.d/$USERNAME
 ---> Running in dc359488e628
Get:1 http://deb.debian.org/debian buster InRelease [121 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [286 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7907 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [10.9 kB]
Fetched 8442 kB in 2s (5238 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  sudo
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 1244 kB of archives.
After this operation, 3882 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian buster/main amd64 sudo amd64 1.8.27-1+deb10u3 [1244 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 1244 kB in 0s (6119 kB/s)
Selecting previously unselected package sudo.
(Reading database ... 24611 files and directories currently installed.)
Preparing to unpack .../sudo_1.8.27-1+deb10u3_amd64.deb ...
Unpacking sudo (1.8.27-1+deb10u3) ...
Setting up sudo (1.8.27-1+deb10u3) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
相同的dockerfile适用于具有Docker的Linux和具有Docker和WSL2的Windows。几天前它确实在MacOS上构建了,但现在它被卡住了,并占用了容器的所有磁盘空间,docker.raw文件一直增长到docker Desktop中定义的最大磁盘映像大小

% docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          2         1         876.7MB   876.7MB (100%)
Containers      1         0         422.3GB   422.3GB (100%)
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
创建非根用户说明取自:

Dockerfile示例:

FROM python:3.7-buster

ARG USERNAME=user-name-goes-here
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN mkdir -p /workspace

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
    #
    # [Optional] Add sudo support. Omit if you don't need to install software after connecting.
    && apt-get update \
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME

# ********************************************************
# * Anything else you want to do like clean up goes here *
# ********************************************************

# [Optional] Set the default user. Omit if you want to keep the default as root.
USER $USERNAME
Docker生成命令:

tag=imagetag

docker build \
    --build-arg USERNAME=$USER \
    --build-arg USER_UID=$(id -u) \
    --build-arg USER_GID=$(id -g) \
    --file ./dockerfile \
    --tag $tag .
Docker运行命令:

containername=some-container
imagename=imagetag
workdir=/workspace
command="bash"

docker run \
    --rm \
    --tty \
    --name ${containername} \
    --workdir=${workdir} \
    --volume $(realpath ../):${workdir}/ \
    --user $(id -u):$(id -g) \
    ${imagename} ${command}
有人知道有什么不同的方法可以让它在MacOS上再次工作吗?如何解决磁盘空间问题?
目前,释放空间的唯一解决方案是
docker system prune
并从docker Desktop疑难解答中选择“清理/清除数据”选项。

您不应该在docker容器中使用
sudo
;主
docker run
命令和调试
docker exec
都使用
-u
选项来允许作为任意用户ID运行,该用户ID可以是
root
0
@DavidMaze如果您知道用于将VSCode附加到运行容器的其他解决方案,请告诉我。我的意思不是将shell附加到容器,而是附加VSCode,这样我就可以使用Python解释器来调试Python代码。我已经在
docker run
命令中使用了选项
--user