Skip to content

Commit

Permalink
George/docker (#10)
Browse files Browse the repository at this point in the history
* updated project to work with Docker

* docker working with turbo-track and auto asset reloading

* reverted theme-show-notifications to true

* merged in main, updates to Dockerfile which install node_modules directly into the container

* added x86_64-linux platform

* restored db defaults for local development

* updated docker defaults to ensure that local setup still works

* removed comments in Gem file

* moved the debugging gems to the development only env

* migrated to an arm64 safe version of debase for debugging

* ran bundle lock --add-platform x86_64-linux

* set debase require to false

* added steps to remove debugging info from Gem file

* updated file path for Gemfile

* reverted brakeman-analysis.yml, added new workflow to remove debugging gems

* converted cleanup-gemfile to be reusable action

* added gem cleanup

* updated cleanup action to include run

* running bundle install after removing debase

* moved gemcleanup to setup-languages step

* temporarily disabling the bundler cache

* minor formatting fixes

* No username

---------

Co-authored-by: Matt Gardner <[email protected]>
  • Loading branch information
George Byers and allthesignals authored May 1, 2024
1 parent 37548ca commit 3f70472
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 24 deletions.
10 changes: 10 additions & 0 deletions .docker/.entrypoints/docker-rails.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -e

if [ -f /rails/tmp/pids/server.pid ]; then
rm /rails/tmp/pids/server.pid
fi

/rails/bin/rails db:migrate
/rails/bin/dev
1 change: 1 addition & 0 deletions .docker/db/init-postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE "app_rails";
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules

12 changes: 12 additions & 0 deletions .github/actions/cleanup-gemfile/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# action.yml file for a custom composite action
name: 'Remove Debug Gems'
description: 'Removes debug-related gems from the Gemfile'
runs:
using: 'composite'
steps:
- name: Remove Debug Gems
shell: bash
run: |
sed -i '/debase-ruby_core_source/d' ./Gemfile
sed -i '/debase/d' ./Gemfile
sed -i '/ruby-debug-ide/d' ./Gemfile
11 changes: 7 additions & 4 deletions .github/actions/setup-languages/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ description: Set up ruby, javascript, and dependencies
runs:
using: composite
steps:
- name: Cleanup gemfile
uses: ./.github/actions/cleanup-gemfile

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
# bundler-cache automatically installs gems
bundler-cache: true
cache-version: 1

- name: Install Dependencies
shell: bash
run: bundle install

- name: Set up node
uses: actions/setup-node@v4
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/cleanup-gemfile

- id: setup
uses: ./.github/actions/setup-project
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ spec/examples.txt
secrets.auto.tfvars
terraform.tfstate
terraform.tfstate.backup

#IDE Specific (RubyMine)
.idea

#IDE Specific (VS Code)
.vscode
138 changes: 138 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.1.0

FROM node:20.12.0-bullseye-slim AS node-stage
WORKDIR /rails
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git

RUN apt-get install --reinstall ca-certificates -y && \
update-ca-certificates

# print the node version
RUN node --version
COPY package.json ./
RUN yarn install

##########################################################################################
# BASE: Shared base docker image
##########################################################################################
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle"

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000

##########################################################################################
# BUILD: Throw-away build stage
##########################################################################################
FROM base as build
ARG DOCKERIZE_VERSION=v0.7.0
#copy the node binary from the node-stage to the base image
COPY --from=node-stage /usr/local/bin/node /usr/local/bin/node

# Install dockerize
RUN apt-get update \
&& apt-get install -y wget \
&& wget -O - https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz | tar xzf - -C /usr/local/bin \
&& apt-get autoremove -yqq --purge wget && rm -rf /var/lib/apt/lists/*

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config redis jq rbenv cu npm


# Clean up
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

##########################################################################################
# DEV: Used for development and test
##########################################################################################
FROM build as dev

WORKDIR /rails

ENV RAILS_ENV="development"

# Install packages needed for development
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y postgresql-client graphviz && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

RUN gem install bundler

# Install application gems for development
COPY Gemfile Gemfile.lock ./

RUN gem update --system
RUN bundle lock --add-platform ruby && \
bundle lock --add-platform x86_64-linux

RUN bundle config set --local without production && \
bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# copy node from node-stage to dev
COPY --from=node-stage /usr/local/bin/node /usr/local/bin/node
# Copy application code
COPY . .
# Copy node_modules from node-stage
COPY --from=node-stage /rails/node_modules ./
ENTRYPOINT [".docker/.entrypoints/docker-rails.sh"]

##########################################################################################
# RELEASE-BUILD: Throw-away build stage for RELEASE
##########################################################################################
FROM build as release-build

# Install application gems for production
COPY Gemfile Gemfile.lock ./

RUN bundle config set --local without development test && \
bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile --gemfile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


##########################################################################################
# RELEASE: Used for production
##########################################################################################
FROM base as release

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install -y --no-install-recommends unzip python3-venv python-is-python3 curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" && \
unzip awscli-bundle.zip && \
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws && \
rm -rf ./awscli-bundle awscli-bundle.zip

# Install custom db migrate script
COPY bin/db-migrate /usr/bin/

# Copy built artifacts: gems, application
COPY --from=release-build /usr/local/bundle /usr/local/bundle
COPY --from=release-build /rails /rails

RUN rm /rails/tmp/pids/server.pid

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER rails:rails

ENTRYPOINT [".docker/.entrypoints/docker-rails.sh"]
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ group :development, :test do
end

group :development do
gem "debase-ruby_core_source"
gem "debase", "~> 0.2.5.beta2", require: false
gem "ruby-debug-ide"
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"

Expand Down
23 changes: 14 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ GEM
cssbundling-rails (1.4.0)
railties (>= 6.0.0)
date (3.3.4)
debase (0.2.5.beta2)
debase-ruby_core_source (>= 0.10.12)
debase-ruby_core_source (3.3.1)
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
Expand Down Expand Up @@ -119,7 +122,7 @@ GEM
irb (1.12.0)
rdoc
reline (>= 0.4.2)
jbuilder (2.11.5)
jbuilder (2.12.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jsbundling-rails (1.3.0)
Expand Down Expand Up @@ -153,12 +156,10 @@ GEM
nio4r (2.7.1)
nokogiri (1.16.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
parallel (1.24.0)
parser (3.3.0.5)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
pg (1.5.6)
Expand Down Expand Up @@ -212,7 +213,7 @@ GEM
psych (>= 4.0.0)
redis (4.8.1)
regexp_parser (2.9.0)
reline (0.5.2)
reline (0.5.4)
io-console (~> 0.5)
rexml (3.2.6)
rspec-core (3.13.0)
Expand Down Expand Up @@ -243,8 +244,8 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.2)
parser (>= 3.3.0.4)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
Expand Down Expand Up @@ -272,6 +273,8 @@ GEM
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-debug-ide (0.7.3)
rake (>= 0.8.1)
ruby-graphviz (1.2.5)
rexml
ruby-progressbar (1.13.0)
Expand Down Expand Up @@ -326,7 +329,6 @@ GEM

PLATFORMS
arm64-darwin-23
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
Expand All @@ -335,6 +337,8 @@ DEPENDENCIES
bundler-audit (~> 0.9)
climate_control (~> 1.0)
cssbundling-rails
debase (~> 0.2.5.beta2)
debase-ruby_core_source
debug
dotenv-rails (~> 2.7)
i18n-tasks (~> 1.0)
Expand All @@ -350,6 +354,7 @@ DEPENDENCIES
rubocop
rubocop-rails-omakase
rubocop-rspec
ruby-debug-ide
secure_headers (~> 6.3)
sidekiq (~> 6.4)
sprockets-rails
Expand All @@ -363,4 +368,4 @@ RUBY VERSION
ruby 3.1.0p0

BUNDLED WITH
2.3.6
2.3.26
6 changes: 3 additions & 3 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
web: env RUBY_DEBUG_OPEN=true bin/rails server
web: env RUBY_DEBUG_PORT=1234 RUBY_DEBUG_STOP_AT_LOAD=true RUBY_DEBUG_OPEN=true RUBY_DEBUG_HOST=0.0.0.0 bin/rails server -p 3000 -b 0.0.0.0
js: yarn build --watch
css: yarn build:css --verbose --watch
worker: bundle exec sidekiq
css: yarn build:css --watch
worker: RUBY_DEBUG_OPEN=true bundle exec sidekiq
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//= link_tree ../images
//= link_tree ../builds
//= link application.postcss.css
2 changes: 1 addition & 1 deletion config/cable.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
development:
adapter: redis
url: redis://localhost:6379/1
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>

test:
adapter: test
Expand Down
1 change: 1 addition & 0 deletions config/credentials/development.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Btn0OVg1L730VpNEbQ+H15cFKg1y3q/z6IwIaDc9c4iExboCtzTUsYU/NO4sXKVZ0m0ShItWOKR4IxwCqw7GrIRKckilMnNhJKKMZJ9clj6CieYC9efIb8WeUe3c11AA--KLu1KB+ynGorNBrB--+jEPdTuPzExEtrlSeJzR9g==
11 changes: 7 additions & 4 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ default: &default

development:
<<: *default
database: iv_cbv_payroll_development
adapter: postgresql
encoding: unicode
database: <%= ENV.fetch("DB_NAME") { "iv_cbv_payroll_development" } %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: iv_cbv_payroll
username: <%= ENV.fetch("DB_USERNAME") { nil } %>

# The password associated with the postgres role (username).
#password:
password: <%= ENV.fetch("DB_PASSWORD") { nil } %>

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
host: <%= ENV.fetch("DB_HOST") { "localhost" } %>

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
Expand Down
4 changes: 4 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
# Check if we use Docker to allow docker ip through web-console

# if the env var DOCKERIZED is set to true then we allow the web console to be accessed from the docker network
config.web_console.allowed_ips = '192.168.65.1' if ENV["DOCKERIZED"] == "true"
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded any time
Expand Down
Loading

0 comments on commit 3f70472

Please sign in to comment.