为何简单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 addedupgradefor 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 cleanand 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
相关产品推荐
相关产品推荐

