Skip to content

Commit

Permalink
fix(docker): simplify user and group creation in Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
MountainGod2 committed Dec 9, 2024
1 parent 0c4f143 commit 435575d
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,9 @@ FROM python:3.13-alpine AS runtime
# Install runtime dependencies (if needed)
RUN apk add --no-cache libffi openssl

# Set environment variables for UID and GID
ENV UID=99
ENV GID=100

# Create a group and user based on the environment variables
RUN if ! getent group "$GID"; then \
addgroup -g "$GID" appgroup; \
else \
echo "Group with GID $GID already exists, skipping addgroup."; \
fi && \
adduser -u "$UID" -G appgroup -D appuser
# Create a non-root user and group
RUN addgroup -g 1001 appgroup && \
adduser -u 1001 -G appgroup -D appuser

# Configure environment variables for the virtual environment
ENV VIRTUAL_ENV=/app/.venv
Expand All @@ -50,11 +42,11 @@ COPY pyproject.toml README.md LICENSE ./

# Create logs directory for the application
RUN mkdir -p /app/logs && \
chown -R "$UID:$GID" /app/logs && \
chown -R appuser:appgroup /app/logs && \
chmod -R 750 /app/logs

# Change ownership of the app directory to the non-root user
RUN chown -R "$UID:$GID" /app
RUN chown -R appuser:appgroup /app

# Install the application into the virtual environment
RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
Expand All @@ -63,7 +55,7 @@ RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
# Copy the entrypoint script into the runtime image and make it executable
COPY docker-entrypoint.sh /app/
RUN chmod +x /app/docker-entrypoint.sh && \
chown "$UID:$GID" /app/docker-entrypoint.sh
chown appuser:appgroup /app/docker-entrypoint.sh

# Switch to the non-root user
USER appuser
Expand Down

0 comments on commit 435575d

Please sign in to comment.