Skip to content

Instantly share code, notes, and snippets.

@tahb
Last active September 10, 2018 09:55
Show Gist options
  • Save tahb/0b05a4cc6381e9bf783cd79372bc87a5 to your computer and use it in GitHub Desktop.
Save tahb/0b05a4cc6381e9bf783cd79372bc87a5 to your computer and use it in GitHub Desktop.
Docker multi-stage builds with the GOV.UK Design system (2018)
// …
// Override any GOVUK settings here
// use Rails asset helpers
$govuk-font-url-function: 'font-url';
$govuk-image-url-function: 'image-url';
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# GOV.UK Design system
# Add additional assets to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
Rails.application.config.assets.precompile += %w[
govuk-frontend/all.js
govuk-frontend/all.scss
govuk-frontend/all-ie8.scss
]
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
Rails.application.config.assets.precompile +=
%w[application.css application-ie8.css application.js]
version: '3'
services:
web:
build:
context: .
args:
RAILS_ENV: "development"
ports: ["3000:3000"]
environment:
RAILS_ENV: "development"
env_file:
- docker-compose.env
depends_on:
- db
- elasticsearch
- redis
tty: true
stdin_open: true
restart: on-failure
volumes:
- .:/srv/dfe-tvs:cached
- node_modules:/srv/dfe-tvs/node_modules # <--- Don't reset node_modules with your working directory as it will empty it
command: bash -c "rm -f tmp/pids/server.pid && rails s -b 'ssl://web:3000?key=config/localhost/https/local.key&cert=config/localhost/https/local.crt'"
db:
image: postgres
volumes:
- pg_data:/var/lib/postgresql/data/:cached
restart: on-failure
elasticsearch:
image: elasticsearch
depends_on:
- db
ports:
- "9200:9200"
environment:
- discovery.type=single-node
- cluster.name=docker=docker-cluster
volumes:
- elasticsearch:/usr/share/elasticsearch/data
restart: on-failure
redis:
image: redis:latest
command: redis-server
volumes:
pg_data: {}
elasticsearch: {}
node_modules: <--- You need this for the above to work too :)
# Install dependencies into a seperate and isolated Docker stage
# that is thrown away apart from any subsequent COPY commands
FROM mkenney/npm:latest AS dependencies
ENV INSTALL_PATH /deps
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY package.json ./package.json
COPY package-lock.json ./package-lock.json
RUN npm set progress=false && npm config set depth 0
RUN npm install --only=production
FROM ruby:2.4.0 as release
MAINTAINER dxw <rails@dxw.com>
RUN apt-get update && apt-get install -qq -y \
build-essential \
nodejs \
libpq-dev \
--fix-missing --no-install-recommends
ENV PHANTOM_JS="phantomjs-2.1.1-linux-x86_64"
RUN curl -OLk https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
RUN tar xvjf $PHANTOM_JS.tar.bz2
RUN mv $PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
RUN rm -rf $PHANTOM_JS
ENV INSTALL_PATH /srv/dfe-tvs
RUN mkdir -p $INSTALL_PATH
# This must be ordered before rake assets:precompile
COPY --from=dependencies ./deps/node_modules $INSTALL_PATH/node_modules
COPY --from=dependencies ./deps/node_modules/govuk-frontend/assets $INSTALL_PATH/app/assets
WORKDIR $INSTALL_PATH
# set rails environment
ARG RAILS_ENV
ENV RAILS_ENV=${RAILS_ENV:-production}
ENV RACK_ENV=${RAILS_ENV:-production}
COPY Gemfile $INSTALL_PATH/Gemfile
COPY Gemfile.lock $INSTALL_PATH/Gemfile.lock
RUN gem install bundler
# bundle ruby gems based on the current environment, default to production
RUN echo $RAILS_ENV
RUN \
if [ "$RAILS_ENV" = "production" ]; then \
bundle install --without development test --retry 10; \
else \
bundle install --retry 10; \
fi
COPY . $INSTALL_PATH
RUN bundle exec rake DATABASE_URL=postgresql:does_not_exist --quiet assets:precompile
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
EXPOSE 3000
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["rails", "server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment