Skip to content

Commit 619d4e9

Browse files
committed
Implement Docker rootless user with reviewer feedback improvements
This commit implements the rootless user functionality for both production and development Docker images with all improvements from PR #837 review: Changes made: - Added rootless user setup using 'rails' username (not 'rails_api_base') - Used ARG instead of ENV for USERNAME, USER_UID, and USER_GID build variables - Set proper file ownership and permissions (700) for security - Added explanatory comment for temporary root switch in jemalloc symlink creation - Applied --chown and --chmod flags to all COPY operations Security improvements: - Application now runs as non-root user (UID/GID 1000) - Reduced attack surface by limiting privileges - Follows Docker security best practices Files modified: - Dockerfile: Added rootless user for production image - Dockerfile.dev: Added rootless user for development image 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent c114ef8 commit 619d4e9

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

Dockerfile

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,36 @@ RUN apt-get update -qq && \
6161
apt-get install --no-install-recommends -y curl libpq-dev libvips libjemalloc2 libyaml-dev && \
6262
apt-get clean && rm -rf /var/lib/apt/lists/*
6363

64-
# Create app directory.
65-
RUN mkdir -p "${APP_HOME}"
64+
# Setup rootless user for security
65+
ARG USERNAME=rails
66+
ARG USER_UID=1000
67+
ARG USER_GID=1000
68+
69+
RUN groupadd --gid $USER_GID $USERNAME && \
70+
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
71+
72+
# Create app directory with proper ownership
73+
RUN mkdir -p "${APP_HOME}" && \
74+
chown -R $USERNAME:$USERNAME $APP_HOME && \
75+
chmod -R 700 $APP_HOME
76+
77+
# Switch to non-root user
78+
USER $USERNAME
6679

6780
# Setup work directory.
6881
WORKDIR $APP_HOME
6982

70-
# Copy everything from the builder image
71-
COPY --link . .
72-
COPY --from=builder $APP_HOME/public/ $APP_HOME/public/
73-
COPY --from=builder $APP_HOME/tmp/ $APP_HOME/tmp/
74-
COPY --from=builder $APP_HOME/vendor/ $APP_HOME/vendor/
83+
# Copy everything from the builder image with proper ownership
84+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 . .
85+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/public/ $APP_HOME/public/
86+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/tmp/ $APP_HOME/tmp/
87+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/vendor/ $APP_HOME/vendor/
7588

89+
# Temporarily switch to root to create system-level symlink for jemalloc
90+
# This is required because symlink creation in /usr/lib requires root privileges
91+
USER root
7692
RUN ln -s /usr/lib/*-linux-gnu/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
93+
USER $USERNAME
7794

7895
# Deployment options
7996
ENV RAILS_LOG_TO_STDOUT=true

Dockerfile.dev

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,37 @@ ENV WORK_ROOT=/src
2020
ENV APP_HOME=$WORK_ROOT/app/
2121
ENV LANG=C.UTF-8
2222

23-
# Create app directory.
24-
RUN mkdir -p $APP_HOME
23+
# Setup rootless user for security
24+
ARG USERNAME=rails
25+
ARG USER_UID=1000
26+
ARG USER_GID=1000
27+
28+
RUN groupadd --gid $USER_GID $USERNAME && \
29+
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
30+
31+
# Create app directory with proper ownership
32+
RUN mkdir -p $APP_HOME && \
33+
chown -R $USERNAME:$USERNAME $APP_HOME && \
34+
chmod -R 700 $APP_HOME
35+
36+
# Switch to non-root user
37+
USER $USERNAME
2538

2639
# Setup work directory.
2740
WORKDIR $APP_HOME
2841

2942
RUN gem install foreman bundler
3043

3144
# Copy dependencies files and install libraries.
32-
COPY --link package.json yarn.lock .yarnrc.yml ./
45+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 package.json yarn.lock .yarnrc.yml ./
3346

3447
RUN corepack enable
3548
RUN yarn install --immutable && yarn cache clean
3649

37-
COPY --link Gemfile Gemfile.lock .ruby-version ./
50+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 Gemfile Gemfile.lock .ruby-version ./
3851
RUN bundle install -j 4
3952

40-
COPY --link . .
53+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 . .
4154

4255
RUN yarn build
4356

0 commit comments

Comments
 (0)