Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor User and Group Creation in Dockerfile for Improved Efficiency and Simplicity #9490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

softjapan
Copy link

This pull request refactors the user and group creation process in the Dockerfile to address inefficiencies, reduce redundancy, and enhance simplicity. The following changes have been made:

  1. Removed Redundant Existence Checks:
  • Eliminated unnecessary getent checks for existing users and groups since the Docker build context typically starts with a clean slate.
  1. Unified User and Group Creation into a Single RUN Layer:
  • Combined groupadd and useradd commands into a single RUN command to minimize Docker image layers and improve build performance.
  1. Added Error Handling:
  • Incorporated || true to prevent the build from failing if a group or user already exists, improving robustness in edge cases.
  1. Enhanced Readability and Maintainability:
  • Simplified and streamlined the commands for better readability and future maintenance.

Before:

ARG GROUP_ID
RUN if ! getent group "$GROUP_ID"; then \
      addgroup --gid "$GROUP_ID" user; \
    fi

ARG USER_ID
RUN if ! getent passwd "$USER_ID"; then \
      adduser --disabled-password --gecos '' --gid "$GROUP_ID" --uid "$USER_ID" user; \
    fi

After:

ARG GROUP_ID
ARG USER_ID

RUN groupadd -g "$GROUP_ID" user || true && \
    useradd -m -u "$USER_ID" -g "$GROUP_ID" -s /bin/bash user || true

Benefits of This Change:
• Improved Efficiency: Fewer layers and streamlined logic result in faster builds and smaller images.
• Reduced Complexity: The refactor removes redundant checks and simplifies user and group creation logic.

Please review and provide feedback!

…y and Simplicity

refactors the user and group creation process in the Dockerfile to address inefficiencies, reduce redundancy, and enhance simplicity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant