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

为何简单ROS Dockerfile执行时出现"permission denied"权限拒绝错误?

Fixing Permission Denied Error on apt-get update in ROS Kinetic Dockerfile

The issue here is straightforward: you’ve switched to the non-root ros user before running apt-get update, and this command requires root privileges to modify system-level package directories like /var/lib/apt/lists/partial (which are owned by root by default).

Here’s the corrected Dockerfile that fixes this problem, plus some standard best practices for ROS Docker setups:

FROM ros:kinetic-robot-xenial

# Run system updates as root first (required for apt commands)
RUN apt-get update && apt-get upgrade -y && \
    # Clean up to reduce image size
    apt-get clean && rm -rf /var/lib/apt/lists/*

# Create non-root user
ENV USERNAME ros
RUN adduser --ingroup sudo --disabled-password --gecos "" --shell /bin/bash --home /home/$USERNAME $USERNAME
RUN bash -c 'echo $USERNAME:ros | chpasswd'
ENV HOME /home/$USERNAME

# Switch to non-root user for subsequent steps (security best practice)
USER $USERNAME

Key Changes:

  • Moved apt-get update (and added upgrade for completeness) to run as root before switching to the non-root user. This eliminates the permission error since root has full access to system directories.
  • Added cleanup steps (apt-get clean and removing the package lists directory) to keep the Docker image size smaller—this is a standard practice to avoid bloating your image.
  • Retained the non-root user setup, since running containers as non-root is a critical security best practice.

If you need to run additional apt commands later as the non-root user, you can use sudo (since we added the user to the sudo group). To avoid entering a password every time, add this line right before switching users:

RUN echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:54:58