DevOps: Docker: Baseimage and application build

Prior to using Docker Compose the following Dockerfiles were used to:

  • build a linux distribution base image with core infrastructure components
  • build the rails app with necessary configuration for serving a rails application from nginx

baseimage Dockerfile:


FROM ruby:2.1.9-slim

ENV DEBIAN_FRONTEND noniteractive

RUN apt-get update && \
    apt-get install -y \
      apt-transport-https \
      build-essential \
      curl \
      emacs24-nox \
      emacs-goodies-el \
      git-core \
      imagemagick \
      libbz2-dev \
      libcurl4-openssl-dev \
      libncurses-dev \
      libexpat-dev \
      libffi-dev \
      libpq-dev \
      libreadline-dev \
      libsqlite3-dev \
      libssl-dev \
      libxml2-dev \
      libxslt1-dev \
      libyaml-dev \
      nginx-extras \
      python-software-properties \
      sqlite3 \
      ssh \
      supervisor \
      texinfo \
      vim \
      zlib1g-dev \
      --fix-missing --no-install-recommends \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

EXPOSE 80

# add options to gemrc
RUN echo "gem: --no-document --no-ri --no-rdoc" > ~/.gemrc

and then the application build with configuration specifics for nginx, redis and PostgreSQL:


#
# command line execution:
#
# docker build --build-arg RDS_HOSTNAME=172.17.0.1 \
#              --build-arg RAILS_ENV=staging \
#              --build-arg GIT_BRANCH=master \
#              --build-arg RDS_USERNAME=migration
#              --build-arg RDS_PASSWORD=migration \
#              --build-arg RDS_PORT=5432 \
#              --build-arg RDS_DB_NAME=docker_build \
#              --build-arg REDIS_HOST=172.17.0.1 \
#              -t partnerpath/hammerhead:6.60.0
#
FROM partnerpath/baseimage:6.60.0

#  ENV parameters from command line
ARG RDS_HOSTNAME
ARG RDS_PORT=5432
ARG RDS_DB_NAME
ARG RDS_USERNAME
ARG RDS_PASSWORD
ARG RAILS_ENV
ARG GIT_BRANCH
ARG REDIS_HOST

RUN mkdir -p /home/app/

# clone the application
COPY id_rsa /root/.ssh/
COPY id_rsa.pub /root/.ssh/

RUN \
  chmod 600 /root/.ssh/id_rsa && \
  echo "IdentityFile /root/.ssh/id_rsa" >> /etc/ssh/ssh_config && \
  echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \
  git clone -b $GIT_BRANCH --single-branch git@github.com:PartnerPath/hammerhead.git /home/app/web && \
  mkdir -p /home/app/web/tmp/sockets && \
  mkdir -p /home/app/web/tmp/pids && \
  touch /home/app/web/tmp/sockets/puma.sock
WORKDIR /home/app/web

# we need these variables, so assets precompile would be successful
ENV RAILS_ENV $RAILS_ENV
ENV CLOUDFRONT_PEM_PATH /config/keys/cloudfront/pk-APKAJM4A3ZCDJ2SMDCZQ.pem
ENV CLOUDFRONT_KEYID E1AB4J4T1PLA4M

ENV RDS_DB_NAME $RDS_DB_NAME
ENV RDS_HOSTNAME $RDS_HOSTNAME
ENV RDS_PORT $RDS_PORT
ENV RDS_USERNAME $RDS_USERNAME
ENV RDS_PASSWORD $RDS_PASSWORD

ENV REDIS_HOST $REDIS_HOST
ENV REDIS_PORT 6379

ENV SECRET_TOKEN xxx
ENV SECRET_KEY_BASE xxx
ENV IAM_KEY xxx
ENV IAM_SECRET xxx

# prepare the app
#TODO: update hammerhead to ruby 2.1.9 and remove the next line
COPY override_config/Gemfile /home/app/web/Gemfile

# start working on app with bundle install
RUN bundle install

# overide configuration
COPY ./cloudfront /home/app/web/config/keys/cloudfront
COPY nginx_config/$RAILS_ENV /etc/nginx/

ADD override_config/$RAILS_ENV/application.yml /home/app/web/config/application.yml
COPY override_config/database.yml /home/app/web/config/database.yml

# Override puma startup script
COPY override_config/puma /home/app/web/bin/puma
RUN chmod +x /home/app/web/bin/puma

# script to run app with environment variables
ADD start-app.sh /usr/bin/start-app
RUN chmod +x /usr/bin/start-app



Note: this earlier docker build assumes that redis and PostgreSQL are in place from separate Docker builds