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

Docker镜像构建失败(用户权限配置问题)及解决方案咨询

问题描述

使用IMX提供的Dockerfile构建镜像时出现错误,Dockerfile内容如下:

FROM ubuntu:20.04

# Update system and add the packages required for Yocto builds.
# Use DEBIAN_FRONTEND=noninteractive, to avoid image build hang waiting
# for a default confirmation [Y/n] at some configurations.

ENV DEBIAN_FRONTEND=noninteractive
RUN apt update
RUN apt install -y gawk wget git-core diffstat unzip texinfo \
    gcc-multilib build-essential chrpath socat cpio python python3 \
    python3-pip python3-pexpect xz-utils debianutils iputils-ping \
    libsdl1.2-dev xterm tar locales net-tools rsync sudo vim curl

# Set up locales
RUN locale-gen en_US.UTF-8 && \
    update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

# Yocto needs 'source' command for setting up the build environment, so replace
# the 'sh' alias to 'bash' instead of 'dash'.
RUN rm /bin/sh && ln -s bash /bin/sh

# Install repo
ADD https://storage.googleapis.com/git-repo-downloads/repo /usr/local/bin/
RUN chmod 755 /usr/local/bin/repo

# Add your user to sudoers to be able to install other packages in the container.
ARG USER
RUN echo "${USER} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${USER} && \
    chmod 0440 /etc/sudoers.d/${USER}

# Set the arguments for host_id and user_id to be able to save the build artifacts
# outside the container, on host directories, as docker volumes.
ARG host_uid \
    host_gid
RUN groupadd -g $host_gid nxp && \
    useradd -g $host_gid -m -s /bin/bash -u $host_uid $USER

# Yocto builds should run as a normal user.
USER $USER

# Add user git info
RUN git config --global user.name "ccisn"
RUN git config --global user.email "ccisn@hslu.ch"
RUN git config --list

RUN mkdir /home/ccisn
RUN mkdir /home/ccisn/yocto_imx8

COPY . /imx-docker


# docker run --rm -ti -v  /var/run/docker.sock:/var/run/docker.sock dockers:imx 

ARG DOCKER_WORKDIR
WORKDIR ${DOCKER_WORKDIR}

构建时触发如下错误:

=> ERROR [ 8/16] RUN echo "${USER} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${USER} &&     chmod 0440 /etc/sudoers.d/${USER}    

------
 > [ 8/16] RUN echo "${USER} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${USER} &&     chmod 0440 /etc/sudoers.d/${USER}:
#12 0.483 /bin/sh: /etc/sudoers.d/: Is a directory
------
executor failed running [/bin/sh -c echo "${USER} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${USER} &&     chmod 0440 /etc/sudoers.d/${USER}]: exit code: 1

 *  The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command docker build --pull --rm -f "imx-docker\Dockerfile" -t dockers:imx "imx-docker"" terminated with exit code: 1. 

已知无法以root身份使用该Docker镜像,求解决办法。

问题原因与修复方案

1. 错误根源

ARG USER定义的构建参数未传入具体值,导致${USER}为空字符串。执行命令时变为echo " ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/,而/etc/sudoers.d/是系统目录,无法将内容写入目录,因此触发报错。

2. 修复步骤

方法一:构建时传入USER参数

执行构建命令时,通过--build-arg指定USER的值(示例中使用ccisn,需和后续配置的用户名一致),同时传入宿主UID和GID:

docker build --pull --rm -f "imx-docker\Dockerfile" -t dockers:imx --build-arg USER=ccisn --build-arg host_uid=1000 --build-arg host_gid=1000 "imx-docker"

注:Windows下可通过PowerShell命令(Get-WmiObject -Class Win32_UserAccount -Filter "Name='你的用户名'").SID获取用户ID,或直接使用通用值1000(无冲突情况下)。

方法二:修改Dockerfile设置USER默认值

在ARG USER行添加默认值,确保构建时未传参也能正常执行:

ARG USER=ccisn

后续代码中已写死/home/ccisn目录,和默认值保持一致,避免路径不匹配。

3. 额外优化

  • 删除RUN mkdir /home/ccisn:useradd -m参数已自动创建用户家目录,重复创建可能引发权限问题。
  • 给DOCKER_WORKDIR添加默认值:ARG DOCKER_WORKDIR=/home/ccisn/yocto_imx8,防止WORKDIR为空导致运行异常。

4. 验证修复

构建完成后,运行容器时以普通用户身份登录,已配置免密sudo权限,可直接执行sudo命令完成需要权限的操作,无需切换root。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 05:33:57