Skip to content

Commit 254e743

Browse files
committed
Update dockerfile to use a rootless user
1 parent e505e32 commit 254e743

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Dockerfile

+18-5
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,32 @@ RUN apt-get update -qq && \
6161
apt-get install --no-install-recommends -y curl libpq-dev libvips libjemalloc2 && \
6262
apt-get clean
6363

64+
ENV USERNAME rails_api_base
65+
ENV USER_UID 1000
66+
ENV USER_GID 1000
67+
68+
# Create a rootless user.
69+
RUN groupadd --gid $USER_GID $USERNAME && \
70+
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
71+
6472
# Create app directory.
65-
RUN mkdir -p $APP_HOME
73+
RUN mkdir -p $APP_HOME && chown -R $USERNAME:$USERNAME $APP_HOME && chmod -R 700 $APP_HOME
74+
75+
# Change to the rootless user.
76+
USER $USERNAME
6677

6778
# Setup work directory.
6879
WORKDIR $APP_HOME
6980

7081
# 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/
82+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 . .
83+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/public/ $APP_HOME/public/
84+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/tmp/ $APP_HOME/tmp/
85+
COPY --from=builder --chown=$USERNAME:$USERNAME --chmod=700 $APP_HOME/vendor/ $APP_HOME/vendor/
7586

87+
USER root
7688
RUN ln -s /usr/lib/*-linux-gnu/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
89+
USER $USERNAME
7790

7891
# Deployment options
7992
ENV RAILS_LOG_TO_STDOUT true

Dockerfile.dev

+15-4
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,33 @@ ENV WORK_ROOT /src
2121
ENV APP_HOME $WORK_ROOT/app/
2222
ENV LANG C.UTF-8
2323

24+
ENV USERNAME rails_api_base
25+
ENV USER_UID 1000
26+
ENV USER_GID 1000
27+
28+
# Create a rootless user.
29+
RUN groupadd --gid $USER_GID $USERNAME && \
30+
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
31+
2432
# Create app directory.
25-
RUN mkdir -p $APP_HOME
33+
RUN mkdir -p $APP_HOME && chown -R $USERNAME:$USERNAME $APP_HOME && chmod -R 700 $APP_HOME
34+
35+
# Change to the rootless user.
36+
USER $USERNAME
2637

2738
# Setup work directory.
2839
WORKDIR $APP_HOME
2940

3041
RUN gem install foreman bundler
3142

3243
# Copy dependencies files and install libraries.
33-
COPY --link package.json yarn.lock ./
44+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 package.json yarn.lock ./
3445
RUN yarn install --frozen-lockfile
3546

36-
COPY --link Gemfile Gemfile.lock ./
47+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 Gemfile Gemfile.lock ./
3748
RUN bundle install -j 4
3849

39-
COPY --link . .
50+
COPY --link --chown=$USERNAME:$USERNAME --chmod=700 . .
4051

4152
RUN yarn build
4253

0 commit comments

Comments
 (0)