-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile.alpine
150 lines (111 loc) · 3.38 KB
/
Dockerfile.alpine
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
150
##
## Base
##
FROM ruby:3.1.2-alpine3.16 as base
# labels from https://github.com/opencontainers/image-spec/blob/master/annotations.md
LABEL org.opencontainers.image.authors=conradwt@gmail.com
LABEL org.opencontainers.image.created=$CREATED_DATE
LABEL org.opencontainers.image.revision=$SOURCE_COMMIT
LABEL org.opencontainers.image.title="Zero To GraphQL Using Ruby"
LABEL org.opencontainers.image.url=https://hub.docker.com/u/conradwt/zero-to-graphql-using-ruby
LABEL org.opencontainers.image.source=https://github.com/conradwt/zero-to-graphql-using-ruby
LABEL org.opencontainers.image.licenses=MIT
LABEL com.conradtaylor.ruby_version=$RUBY_VERSION
# set this with shell variables at build-time.
# If they aren't set, then not-set will be default.
ARG CREATED_DATE=not-set
ARG SOURCE_COMMIT=not-set
# environment variables
ENV APP_PATH /home/darnoc/app
ENV BUNDLE_PATH /usr/local/bundle/gems
ENV TMP_PATH /tmp/
ENV RAILS_LOG_TO_STDOUT true
ENV RAILS_PORT 3000
ENV USER=darnoc
ENV UID=1000
ENV GID=1000
# creates an unprivileged user to be used exclusively to run the Rails app
RUN \
addgroup \
-g "${GID}" \
-S "${USER}" \
&& adduser \
-s /bin/sh \
-u "${UID}" \
-G "${USER}" \
-h "/home/${USER}" \
-D "${USER}" \
&& su "${USER}"
# copy entrypoint scripts and grant execution permissions
# COPY ./dev-docker-entrypoint.sh /usr/local/bin/dev-entrypoint.sh
# COPY ./test-docker-entrypoint.sh /usr/local/bin/test-entrypoint.sh
# RUN chmod +x /usr/local/bin/dev-entrypoint.sh && chmod +x /usr/local/bin/test-entrypoint.sh
#
# https://pkgs.alpinelinux.org/packages?name=&branch=v3.16
#
# install build and runtime dependencies
RUN apk add --no-cache libxml2 libxslt && \
apk add --no-cache --virtual .gem-installdeps build-base libxml2-dev libxslt-dev && \
gem install nokogiri --platform=ruby -- --use-system-libraries && \
rm -rf $GEM_HOME/cache && \
apk del .gem-installdeps
RUN apk -U add --no-cache \
build-base=0.5-r3 \
bzip2=1.0.8-r1 \
ca-certificates=20211220-r0 \
curl=7.83.1-r1 \
fontconfig=2.14.0-r0 \
gcompat=1.0.0-r4 \
libpq=14.4-r0 \
musl-dev=1.2.3-r0 \
postgresql14-dev=14.4-r0 \
tini=0.19.0-r0 \
tzdata=2022a-r0 && \
rm -rf /var/cache/apk/* && \
mkdir -p $APP_PATH
ENV RAILS_ENV=production
EXPOSE ${RAILS_PORT}
ENV PORT ${RAILS_PORT}
WORKDIR ${APP_PATH}
COPY Gemfile* ./
RUN gem install bundler && \
rm -rf ${GEM_HOME}/cache/*
RUN bundle config set without 'development test'
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install --jobs 20 --retry 5
COPY . .
RUN chmod +x ./entrypoint.sh
RUN chown -R ${USER}:${USER} ${APP_PATH}
ENTRYPOINT ["/sbin/tini", "--", "./entrypoint.sh"]
##
## Development
##
FROM base as dev
ENV RAILS_ENV=development
# RUN bundle config list
RUN bundle config --delete without
RUN bundle config --delete with
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install --jobs 20 --retry 5
USER ${USER}:${USER}
CMD ["bin/rails", "server", "-b", "0.0.0.0"]
##
## Test
##
FROM dev as test
USER ${USER}:${USER}
CMD ["bundle", "exec", "rspec"]
##
## Pre-Production
##
FROM test as pre-prod
USER root
RUN rm -rf ./spec
##
## Production
##
FROM base as prod
COPY --from=pre-prod ${APP_PATH} ${APP_PATH}
HEALTHCHECK CMD curl http://127.0.0.1/ || exit 1
USER ${USER}:${USER}
CMD ["bin/rails", "server", "-b", "0.0.0.0", "-e", "production"]