Keywords: Docker | Python Installation | Permission Management | Selenium | Container Security
Abstract: This paper comprehensively analyzes the permission errors encountered when using selenium/node-chrome base images during apt-get update operations. Through in-depth examination of Dockerfile user management mechanisms, three solutions are proposed: using sudo, switching back to root user, or building custom images. With code examples and practical recommendations, the article helps developers understand core concepts of Docker permission management and provides best practices for securely installing Python in container environments.
Problem Background and Error Analysis
When using selenium/node-chrome:3.7.1-argon as the base image for Docker image construction, executing the RUN apt-get update command results in a permission error: E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied). This error code indicates that the current user lacks sufficient permissions to access the APT package management system directories.
Root Cause Investigation
Analysis of the official Dockerfile source code reveals that the selenium/node-chrome image performs user context switching during construction, changing from the default root user to the seluser user. This design follows security best practices by using non-privileged users for application execution at runtime. However, during the build phase when additional software packages need installation, this user switching causes permission issues.
Solution Comparison
Solution One: Using sudo Command
RUN sudo apt-get update
RUN sudo apt-get install -y python3
While this approach is straightforward, frequent use of sudo in Dockerfiles is considered poor practice as it increases security risks and may compromise container isolation.
Solution Two: Switching Back to Root User
USER root
RUN apt-get update
RUN apt-get install -y python3
USER seluser
This is the recommended solution. By temporarily switching back to the root user before Python installation, necessary permissions are obtained for package management operations. After installation completes, switching back to a non-privileged user maintains runtime security.
Solution Three: Building Custom Base Images
For long-term projects, consider building custom base images based on official Ubuntu or Python images, pre-installing Selenium, Chrome, and Python environments. This approach avoids complexities from user switching but requires more maintenance effort.
Complete Dockerfile Example
FROM selenium/node-chrome:3.7.1-argon
# Switch back to root user for installation permissions
USER root
# Update package lists and install Python3
RUN apt-get update && apt-get install -y python3 python3-pip
# Install required Python packages
RUN pip3 install requests beautifulsoup4
# Switch back to non-privileged user for runtime security
USER seluser
# Copy application code
COPY . /app
WORKDIR /app
# Set container startup command
CMD ["python3", "app.py"]
Best Practice Recommendations
When installing software packages in Docker containers, follow these principles: prefer official images as base to minimize custom installations; combine multiple RUN commands to reduce image layers; clean cache files promptly after installation to minimize image size; always use non-privileged users at runtime to ensure security.
Development Workflow Optimization
As suggested in reference articles, complete development and testing in local environments before containerizing applications. Use virtual environments (like virtualenv) for Python development, ensuring application functionality before building Docker images. This separated development workflow significantly improves efficiency and reduces debugging complexity in container environments.