Skip to content

Commit 6f50d40

Browse files
authored
Lockdown rails app in production for security (rails#47594)
Current Dockerfile generated by Rails runs as a non-root user which prevents modification of the operating system but leaves wide open all gems and the application itself. This change locks down the application gems and only opens up access to the following directories: db, log, storage, tmp This is a even more secure alternative to rails#47580
1 parent 18ae56a commit 6f50d40

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

railties/lib/rails/generators/rails/app/templates/Dockerfile.tt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ RUN apt-get update -qq && \
7272
apt-get install --no-install-recommends -y <%= dockerfile_deploy_packages.join(" ") %> && \
7373
rm -rf /var/lib/apt/lists /var/cache/apt/archives
7474

75-
# Run and own the application files as a non-root user for security
76-
RUN useradd rails --home /rails --shell /bin/bash
77-
USER rails:rails
78-
7975
# Copy built artifacts: gems, application
80-
COPY --from=build --chown=rails:rails /usr/local/bundle /usr/local/bundle
81-
COPY --from=build --chown=rails:rails /rails /rails
76+
COPY --from=build /usr/local/bundle /usr/local/bundle
77+
COPY --from=build /rails /rails
78+
79+
# Run and own only the runtime files as a non-root user for security
80+
RUN useradd rails --home /rails --shell /bin/bash && \
81+
chown -R rails:rails db log storage tmp
82+
USER rails:rails
8283

8384
# Entrypoint prepares the database.
8485
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

0 commit comments

Comments
 (0)