-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDockerfile.dev
115 lines (92 loc) · 4.54 KB
/
Dockerfile.dev
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
FROM ruby:2.7.1-buster as base
# WORKDIR needs to be the same as in the final base image or compiled gems will point to an invalid directory
# NOTE: For the compiled gems to be shared across services then the WORKDIR needs to be same for all images
RUN mkdir -p /home/rails/services/app
WORKDIR /home/rails/services/app
ENV GEM_HOME=/usr/local/bundle/ruby/2.7.0
ENV PATH $GEM_HOME/bin:$PATH
# Install gems that need compiling first b/c they can take a long time to compile
RUN gem install \
nokogiri:1.10.10 \
ffi:1.13.1 \
grpc:1.31.1 \
mini_portile2:2.5.0 \
msgpack:1.3.3 \
pg:1.2.3 \
nio4r:2.5.2 \
puma:4.3.5 \
eventmachine:1.2.7
RUN gem install bundler:2.1.4
# sources are based on where the code resides on the host system relative to the build directory
ARG source_path=ros
# targets are based on where the Gemfile expects to find them within the container
ARG target_path=../..
# NOTE: Copy in a generic Gemfile and the dependent gem's gemspecs so that their dependencies are also installed
COPY ${source_path}/services/Gemfile* ./
COPY ${source_path}/lib/core/*.gemspec ${target_path}/lib/core/
COPY ${source_path}/lib/sdk/*.gemspec ${target_path}/lib/sdk/
# Don't use the --deployment flag since this is a container. See: http://bundler.io/man/bundle-install.1.html#DEPLOYMENT-MODE
ARG GEM_SERVER
ARG bundle_string='development test'
RUN bundle config set without ${bundle_string}
# Build a layer with gems from just the common Gemfile
# Remove reference to git in spec.files
RUN sed -i '/git/d' ../../lib/sdk/*.gemspec \
&& bundle install \
&& find /usr/local/bundle -iname '*.o' -exec rm -rf {} \; \
&& find /usr/local/bundle -iname '*.a' -exec rm -rf {} \; \
&& mv Gemfile ..
# Remove reference to gems loaded from a path so bundle doesn't blow up
# RUN sed -i '/path/d' Gemfile
ARG project=iam
COPY ${source_path}/services/${project}/Gemfile* ./
COPY ${source_path}/services/${project}/ros-${project}.gemspec ./
RUN bundle install \
&& find /usr/local/bundle -iname '*.o' -exec rm -rf {} \; \
&& find /usr/local/bundle -iname '*.a' -exec rm -rf {} \;
# Runtime container
FROM ruby:2.7.1-slim-buster
# Install OS packages and create a non-root user to run the application
# To compile pg gem: libpq-dev
# To install pg client to run via bash: postgresql-client
ARG os_packages='libpq5 git less'
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install --yes --no-install-recommends ${os_packages} \
&& apt-get clean
ARG PUID=1000
ARG PGID=1000
RUN [ $(getent group $PGID) ] || addgroup --gid ${PGID} rails \
&& useradd -ms /bin/bash -d /home/rails --uid ${PUID} --gid ${PGID} rails \
&& mkdir -p /home/rails/services/app \
&& echo 'set editing-mode vi' > /home/rails/.inputrc.vi \
&& echo "alias ivi='cp /home/rails/.inputrc.vi /home/rails/.inputrc; set -o vi'" > /home/rails/.bash_aliases \
&& echo "alias rspec='spring rspec $@'\nalias src='ss; rc'\nalias ss='spring stop'\nalias rs='rails server -b 0.0.0.0 --pid /tmp/server.pid'\nalias rc='ivi; spring rails console'\nalias rk='spring rake'" >> /home/rails/.bash_aliases \
&& chown ${PUID}:${PGID} /home/rails -R \
&& echo 'rails ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
# Rails operations
WORKDIR /home/rails/services/app
ARG source_path=.
ARG target_path=../..
# TODO: Replace rails:rails with ${PUID}:${PGID} when CircleCI is at 19.03
COPY --chown=rails:rails ${source_path}/lib/core/. ${target_path}/lib/core/
COPY --chown=rails:rails ${source_path}/lib/sdk/. ${target_path}/lib/sdk/
COPY --chown=rails:rails ${source_path}/services/Gemfile ../Gemfile
COPY --chown=rails:rails ${source_path}/.rubocop.yml ${target_path}/.rubocop.yml
COPY --chown=rails:rails ${source_path}/.rubocop_todo.yml ${target_path}/.rubocop_todo.yml
# workaround for buildkit not setting correct permissions
RUN sed -i '/git/d' ${target_path}/lib/sdk/*.gemspec \
&& chown rails: /home/rails/lib
EXPOSE 3000
# Boot the application; Override this from the command line in order to run other tools
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-P", "/tmp/server.pid"]
# CircleCI docker version is old, it doesn't expand ARGs or ENVs for "COPY --chown" directive
# TODO: Replace rails:rails with ${PUID}:${PGID} when CircleCI is at 19.03
COPY --chown=rails:rails --from=base /usr/local/bundle /usr/local/bundle
USER ${PUID}:${PGID}
# Copy in the project files
ARG project=user
COPY --chown=rails:rails ${source_path}/services/${project}/. ./
ARG rails_env=production
ENV GEM_HOME=/usr/local/bundle/ruby/2.7.0
ENV PATH $GEM_HOME/bin:$PATH
ENV RAILS_ENV=${rails_env} EDITOR=vim TERM=xterm RAILS_LOG_TO_STDOUT=yes