Skip to content

Commit

Permalink
Merge pull request #23 from zakariaf/dockerize
Browse files Browse the repository at this point in the history
Add Docker configs for production
  • Loading branch information
zakariaf authored Nov 21, 2022
2 parents 4012e06 + f5f27be commit 341c2f3
Show file tree
Hide file tree
Showing 18 changed files with 609 additions and 136 deletions.
46 changes: 46 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.git/

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/
!/tmp/storage/.keep

/public/assets

# Ignore master key for decrypting credentials and more.
/config/master.key

/app/assets/builds/*
!/app/assets/builds/.keep

# Ignore SampleCov files
/coverage/*

# Vite Ruby
/public/vite*
node_modules
# Vite uses dotenv and suggests to ignore local-only env files. See
# https://vitejs.dev/guide/env-and-mode.html#env-files
*.local

# Ignore uncategorized files
.DS_Store
83 changes: 83 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

# Rather than use the directory name, let's control the name of the project.
export COMPOSE_PROJECT_NAME=baseapp

# Which environment is running? These should be "development" or "production".

# About COMPOSE_PROFILES: https://docs.docker.com/compose/profiles/
# In development we want all services to start but in production you don't
# need the asset watchers to run since assets get built into the image.
#
export RAILS_ENV=production
export COMPOSE_PROFILES=postgres,redis,web,worker,cable

# Should Docker restart your containers if they go down in unexpected ways?
export DOCKER_RESTART_POLICY=unless-stopped
# export DOCKER_RESTART_POLICY=no

# What ip:port should be published back to the Docker host for the app server?
# If you're using Docker Toolbox or a custom VM you can't use 127.0.0.1. This
# is being overwritten in dev to be compatible with more dev environments.
#
# If you have a port conflict because something else is using 3000 then you
# can either stop that process or change 3000 to be something else.
#
# Use the default in production to avoid having puma directly accessible to
# the internet since it'll very likely be behind nginx or a load balancer.
export DOCKER_WEB_PORT_FORWARD=127.0.0.1:3000
# export DOCKER_WEB_PORT_FORWARD=3000

# This is the same as above except for Action Cable.
export DOCKER_CABLE_PORT_FORWARD=127.0.0.1:28080
# export DOCKER_CABLE_PORT_FORWARD=28080

# What CPU and memory constraints will be added to your services? When left at
# 0 they will happily use as much as needed.
# export DOCKER_POSTGRES_CPUS=0
# export DOCKER_POSTGRES_MEMORY=0
# export DOCKER_REDIS_CPUS=0
# export DOCKER_REDIS_MEMORY=0
# export DOCKER_WEB_CPUS=0
# export DOCKER_WEB_MEMORY=0
# export DOCKER_WORKER_CPUS=0
# export DOCKER_WORKER_MEMORY=0
# export DOCKER_CABLE_CPUS=0
# export DOCKER_CABLE_MEMORY=0

## Secret keys
# You can use `rails secret` command to generate a secret key
export SECRET_KEY_BASE=insecure-key
export DEVISE_JWT_SECRET_KEY=my-jwt-secret-key

## Host
export DEFAULT_HOST=localhost

## Action cable
export ACTION_CABLE_URL=ws://localhost:28080
export ACTION_CABLE_ALLOWED_REQUEST_ORIGINS=http:\/\/localhost*
# Examples:
# http:\/\/localhost*
# http:\/\/example.*,https:\/\/example.*

## Puma
# export PORT=3000

## Workers and threads count
export WEB_CONCURRENCY=2
export RAILS_MAX_THREADS=5
export RAILS_MIN_THREADS=5

## Postgres
export POSTGRES_HOST=postgres
export POSTGRES_PORT=5432
export POSTGRES_USER=baseapp
export POSTGRES_PASSWORD=postgres
export POSTGRES_DB=baseapp

## Redis URL
export REDIS_URL=redis://redis:6379/1
export REDIS_CHANNEL_PREFIX=baseapp

# Sidekiq web
export SIDEKIQ_WEB_USERNAME=sidekiq-web-dashboard
export SIDEKIQ_WEB_PASSWORD=sidekiq-web-123
42 changes: 0 additions & 42 deletions .env.example

This file was deleted.

2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

# This file will be used by github workflows

## Host
export DEFAULT_HOST=localhost

Expand Down
15 changes: 1 addition & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

Expand Down Expand Up @@ -39,15 +33,8 @@ yarn-error.log*
# Ignore SampleCov files
/coverage/*

# Ignore env files
.env*
!.env.example
!.env.test

# Vite Ruby
/public/vite
/public/vite-dev
/public/vite-test
/public/vite*
node_modules
# Vite uses dotenv and suggests to ignore local-only env files. See
# https://vitejs.dev/guide/env-and-mode.html#env-files
Expand Down
69 changes: 69 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
ARG RUBY_VERSION
ARG IMAGE_FLAVOUR=alpine

FROM ruby:$RUBY_VERSION-$IMAGE_FLAVOUR AS base

# Install system dependencies required both at runtime and build time
ARG NODE_VERSION
ARG YARN_VERSION
RUN apk add --update \
git \
postgresql-dev \
tzdata \
nodejs=$NODE_VERSION \
yarn=$YARN_VERSION

######################################################################

# This stage will be responsible for installing gems and npm packages
FROM base AS dependencies

# Install system dependencies required to build some Ruby gems (pg)
RUN apk add --update build-base
RUN mkdir /app
WORKDIR /app

COPY .ruby-version Gemfile Gemfile.lock ./

# Install gems
ARG RAILS_ENV
ENV RAILS_ENV="${RAILS_ENV}" \
NODE_ENV="development"

RUN bundle config set without "development test"
RUN bundle install --jobs "$(nproc)" --retry "$(nproc)"

COPY package.json yarn.lock ./

# Install npm packages
RUN yarn install --frozen-lockfile

COPY . ./

RUN SECRET_KEY_BASE=irrelevant DEVISE_JWT_SECRET_KEY=irrelevant bundle exec rails assets:precompile

######################################################################

# We're back at the base stage
FROM base AS app

# Create a non-root user to run the app and own app-specific files
RUN adduser -D app

# Switch to this user
USER app

# We'll install the app in this directory
WORKDIR /app

# Copy over gems from the dependencies stage
COPY --from=dependencies /usr/local/bundle/ /usr/local/bundle/
COPY --chown=app --from=dependencies /app/public/ /app/public/

# Finally, copy over the code
# This is where the .dockerignore file comes into play
# Note that we have to use `--chown` here
COPY --chown=app . ./

# Launch the server
CMD ["rails", "s"]
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ GEM
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
minitest (5.16.3)
msgpack (1.6.0)
net-imap (0.3.1)
Expand All @@ -191,6 +192,9 @@ GEM
net-smtp (0.3.3)
net-protocol
nio4r (2.5.8)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.9-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.9-x86_64-linux)
Expand Down Expand Up @@ -365,6 +369,7 @@ GEM
PLATFORMS
arm64-darwin-21
arm64-darwin-22
ruby
x86_64-linux

DEPENDENCIES
Expand Down
Loading

0 comments on commit 341c2f3

Please sign in to comment.