-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDockerfile
More file actions
149 lines (122 loc) · 4.46 KB
/
Dockerfile
File metadata and controls
149 lines (122 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# based on Mike Rogers' Dockerfile from:
# - https://github.com/Ruby-Starter-Kits/Docker-Rails-Template/blob/master/Dockerfile
FROM ruby:3.4.5-alpine AS builder
LABEL maintainer="Fabio Lima <fabionl@gmail.com>"
RUN apk --no-cache add --virtual build-dependencies \
build-base \
openssl \
# Nokogiri Libraries
zlib-dev \
libxml2-dev \
libxslt-dev \
# Postgres
postgresql-dev \
# JavaScript
nodejs \
yarn \
# FFI Bindings in ruby (Run C Commands)
libffi-dev \
# Fixes watch file issues with things like HMR
libnotify-dev \
# YAML library for psych gem
yaml-dev
# Dockerize allows us to wait for other containers to be ready before we run our own code.
ENV DOCKERIZE_VERSION v0.6.1
RUN wget -nv https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
# Rails Specific libraries
RUN apk --no-cache add \
# ActiveStorage file inspection
file \
# Time zone data
tzdata \
# HTML to PDF conversion
# ttf-ubuntu-font-family \
# wkhtmltopdf \
# Image Resizing
imagemagick \
vips \
# Nice to have
bash \
git \
# VIM is a handy editor for editing credentials
vim \
# Allows for mimemagic gem to be installed
shared-mime-info
# Install any extra dependencies via Aptfile - These are installed on Heroku
# COPY Aptfile /usr/src/app/Aptfile
# RUN apk add --update $(cat /usr/src/app/Aptfile | xargs)
# Install Bundler v1.3
RUN gem update --system && gem install bundler -v "2.6.9"
FROM builder AS development
# Set common ENVs
ENV BOOTSNAP_CACHE_DIR /usr/src/bootsnap
ENV YARN_CACHE_FOLDER /usr/src/yarn
ENV EDITOR vim
ENV LANG en_US.UTF-8
ENV BUNDLE_PATH /usr/local/bundle
ENV RAILS_LOG_TO_STDOUT enabled
ENV HISTFILE /usr/src/app/log/.bash_history
# Set build args. These let linux users not run into file permission problems
ARG USER_ID=${USER_ID:-1000}
ARG GROUP_ID=${GROUP_ID:-1000}
# Add non-root user and group with alpine first available uid, 1000
RUN addgroup -g $USER_ID -S appgroup \
&& adduser -u $GROUP_ID -S appuser -G appgroup
# Install multiple gems at the same time
RUN bundle config set jobs $(nproc)
# Create app directory in the conventional /usr/src/app
RUN mkdir -p /usr/src/app \
&& mkdir -p /usr/src/app/node_modules \
&& mkdir -p /usr/src/app/app/assets/builds \
&& mkdir -p /usr/src/app/tmp/cache \
&& mkdir -p $YARN_CACHE_FOLDER \
&& mkdir -p $BOOTSNAP_CACHE_DIR \
&& chown -R appuser:appgroup /usr/src/app \
&& chown -R appuser:appgroup $BUNDLE_PATH \
&& chown -R appuser:appgroup $BOOTSNAP_CACHE_DIR \
&& chown -R appuser:appgroup $YARN_CACHE_FOLDER
WORKDIR /usr/src/app
ENV PATH /usr/src/app/bin:$PATH
# Add a script to be executed every time the container starts.
COPY bin/docker/entrypoints/* /usr/bin/
RUN chmod +x /usr/bin/wait-for-postgres.sh
RUN chmod +x /usr/bin/wait-for-web.sh
ENTRYPOINT ["/usr/bin/wait-for-postgres.sh"]
# Define the user running the container
USER appuser
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
FROM development AS production
ENV RAILS_ENV production
ENV RACK_ENV production
ENV NODE_ENV production
COPY Gemfile /usr/src/app
COPY .ruby-version /usr/src/app
COPY Gemfile.lock /usr/src/app
# Install Ruby Gems
RUN bundle config set deployment 'true' \
&& bundle config set without 'development:test' \
&& bundle check || bundle install --jobs=$(nproc)
COPY package.json /usr/src/app
COPY yarn.lock /usr/src/app
# Install Yarn Libraries
RUN yarn install --frozen-lockfile --check-files
# Chown files so non are root.
COPY --chown=appuser:appgroup . /usr/src/app
# Build CSS first
RUN yarn build:css
# Download Import Maps JavaScript dependencies
RUN ./bin/importmap install
# Precompile assets without requiring active record or credentials
RUN RAILS_SERVE_STATIC_FILES=enabled \
SECRET_KEY_BASE=dummy-secret-key-for-asset-compilation \
DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname \
bundle exec rails assets:precompile
# Precompile bootsnap
RUN bundle exec bootsnap precompile --gemfile app/ lib/
# Define the user running the container
USER appuser
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]