Skip to content

Instantly share code, notes, and snippets.

@rpbaltazar
Created August 16, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpbaltazar/1478c08bf252c175162b21344361ebdc to your computer and use it in GitHub Desktop.
Save rpbaltazar/1478c08bf252c175162b21344361ebdc to your computer and use it in GitHub Desktop.
dockerfile and sample docker-compose for whistler services
---
version: '3'
services:
cognito:
labels:
stack.name: whistler
stack.component: be
be.component: application
application.component: platform
platform.feature_set: mounted
service.type: server
# service.type: ros
image: "${IMAGE_REPOSITORY}/cognito:${IMAGE_TAG}"
command: ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-P", "/tmp/server.pid"]
env_file:
- ../platform/platform.env
depends_on:
- wait
ports:
# - "1234:1234"
# - "9876" # druby for pry-remote
- "3000"
volumes:
- "${ROS_CONTEXT_DIR}/services/cognito:/home/rails/services/app"
- "${ROS_CONTEXT_DIR}/lib:/home/rails/lib"
- bundle-cache:/bundle
build:
context: "${ROS_CONTEXT_DIR}"
args:
rails_env: development
bundle_string: --without production
os_packages: libpq5 tree curl git sudo vim less tcpdump net-tools iputils-ping graphviz
project: cognito
PUID: "${PUID:-1000}"
PGID: "${PGID:-1000}"
volumes:
bundle-cache:
FROM ruby:2.6.3-stretch 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 BUNDLE_PATH=/bundle \
BUNDLE_BIN=/bundle/bin \
GEM_HOME=/bundle
ENV PATH="${BUNDLE_BIN}:${PATH}"
# Install gems that need compiling first b/c they can take a long time to compile
RUN gem install \
bundler:2.0.1 \
nokogiri:1.10.3 \
ffi:1.10.0 \
mini_portile2:2.4.0 \
msgpack:1.2.10 \
pg:1.1.4 \
nio4r:2.3.1 \
puma:3.12.1 \
eventmachine:1.2.7
ARG ROS_DIR=./ros
ARG project=user
COPY services/${project}/Gemfile* ./
# NOTE: Dependent gem's gemspecs need to be copied in so that their dependencies are also installed
COPY ${ROS_DIR}/lib/core/*.gemspec ../../${ROS_DIR}/lib/core/
COPY ${ROS_DIR}/lib/sdk/*.gemspec ../../${ROS_DIR}/lib/sdk/
COPY lib/core/*.gemspec ../../lib/core/
COPY lib/sdk/*.gemspec ../../lib/sdk/
# Remove reference to gems loaded from a path so bundle doesn't blow up
# RUN sed -i '/path/d' Gemfile
# # Don't use the --deployment flag since this is a container. See: http://bundler.io/man/bundle-install.1.html#DEPLOYMENT-MODE
ARG bundle_string='--without development test'
RUN bundle install ${bundle_string} \
&& find /usr/local/bundle -iname '*.o' -exec rm -rf {} \; \
&& find /usr/local/bundle -iname '*.a' -exec rm -rf {} \;
# Runtime container
FROM ruby:2.6.3-slim-stretch
# 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} --non-unique rails \
&& mkdir -p /home/rails/services/app \
&& echo 'set editing-mode vi' > /home/rails/.inputrc \
&& 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='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
COPY --from=base /usr/local/bundle /usr/local/bundle
# Rails operations
WORKDIR /home/rails/services/app
ARG project=user
ARG ROS_DIR=./ros
COPY --chown=rails:rails ${ROS_DIR}/lib/core/. ../../${ROS_DIR}/lib/core/
COPY --chown=rails:rails ${ROS_DIR}/lib/sdk/. ../../${ROS_DIR}/lib/sdk/
COPY --chown=rails:rails lib/core/. ../../lib/core/
COPY --chown=rails:rails lib/sdk/. ../../lib/sdk/
# COPY ${ROS_DIR}/lib/core/. ../../${ROS_DIR}/lib/core/
# COPY ${ROS_DIR}/lib/sdk/. ../../${ROS_DIR}/lib/sdk/
# COPY lib/core/. ../../lib/core/
# COPY lib/sdk/. ../../lib/sdk/
COPY --chown=rails:rails services/${project}/. ./
# COPY services/${project}/. ./
# CircleCI docker version is old, it doesn't expand ARGs or ENVs for "COPY --chown" directive
# RUN chown -R ${PUID}:${PGID} /home/rails /usr/local/bundle
ARG rails_env=production
ENV RAILS_ENV=${rails_env} EDITOR=vim TERM=xterm RAILS_LOG_TO_STDOUT=yes
EXPOSE 3000
USER ${PUID}:${PGID}
# Boot the application; Override this from the command line in order to run other tools
# CMD ["bundle", "exec", "puma", "config.ru"]
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-P", "/tmp/server.pid"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment