Skip to content

Commit

Permalink
Add Docker and Docker Compose configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandunn committed Aug 27, 2024
1 parent cdc39df commit 2f8d320
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.dockerignore
.ruby-version
Dockerfile
LICENSE
Procfile
Procfile.dev
README.markdown
docker-compose.yml
.github/
.ruby-lsp/
coverage/
public/assets
node_modules/
log/
tmp/
19 changes: 15 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
---
name: CI

on: # yamllint disable-line rule:truthy
pull_request:
workflow_call:

jobs:
Ruby:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -92,6 +88,21 @@ jobs:
- name: Lint the CSS
run: yarn lint:css

Docker:
runs-on: ubuntu-latest

steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker
uses: docker/setup-buildx-action@v3
- name: Build
uses: docker/build-push-action@v6
with:
cache-from: type=gha
cache-to: type=gha,mode=max
push: false

Vulnerabilities:
runs-on: ubuntu-latest

Expand Down
101 changes: 101 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
ARG ENVIRONMENT="production"
ARG NODE_VERSION="20.16.0"
ARG RUBY_VERSION="3.3.4"

FROM node:$NODE_VERSION-alpine3.20 AS node-builder

# Set environment variables.
ENV NODE_ENV=${ENVIRONMENT}

# Change to a build working directory.
WORKDIR /build

# Copy the package into Docker.
COPY .yarnrc.yml package.json yarn.lock .
COPY .yarn .yarn

# Install the Node.js dependencies.
RUN yarn install --immutable

# Copy in files for assets precompilation.
COPY app/assets app/assets
COPY app/javascript app/javascript
COPY config/esbuild.config.js config/esbuild.config.js
COPY config/locales/en.yml config/locals/en.yml

# Build the assets.
RUN yarn build

# Remove dependencies.
RUN rm -rf node_modules

FROM ruby:$RUBY_VERSION-alpine3.20 AS ruby-builder

# Accept optional arguments.
ARG BUNDLE_WITHOUT="development test"

# Set environment variables.
ENV BUNDLE_WITHOUT=${BUNDLE_WITHOUT} \
RAILS_ENV=${ENVIRONMENT} \
REMOVE_JS_BUILD_YARN_INSTALL=true

# Install build dependency requirements.
RUN apk -U upgrade \
&& apk add --no-cache build-base libpq-dev libxml2-dev libxslt-dev tzdata

# Change to a build working directory.
WORKDIR /build

# Configure Bundler.
RUN bundle config --local build.nokogiri --use-system-libraries \
&& bundle config --local without "${BUNDLE_WITHOUT}" \
&& bundle config --local frozen "true"

# Copy the Gemfile into Docker.
COPY Gemfile Gemfile.lock .

# Install the Bundler version specified.
RUN gem install bundler -v $(tail -n1 Gemfile.lock)

# Install the Ruby dependencies.
RUN bundle install

FROM ruby:$RUBY_VERSION-alpine3.20 AS application

# Set environment variables.
ENV NODE_ENV=${ENVIRONMENT} \
RAILS_ENV=${ENVIRONMENT} \
LD_PRELOAD="libjemalloc.so.2" \
RUBY_YJIT_ENABLE="1"

# Install dependency requirements.
RUN apk -U upgrade \
&& apk add --no-cache jemalloc libpq-dev tzdata

# Create a application-specific user.
RUN addgroup -S runner \
&& adduser -S runner -G runner

# Switch to the user.
USER runner

# Create the application directory.
RUN mkdir -p /home/runner/app
WORKDIR /home/runner/app

# Copy the application into Docker.
COPY --chown=runner . .

# Copy in the assets, cache, and dependencies.
COPY --chown=runner --from=node-builder /build/app/assets/builds app/assets/builds
COPY --chown=runner --from=ruby-builder /usr/local/bundle /usr/local/bundle

# Precompile the Bootsnap cache.
RUN rm -rf tmp/cache
RUN bundle exec bootsnap precompile --gemfile app/ lib/

# Expose the server.
EXPOSE 3000

# Run the server.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
database:
image: postgres:16.4-bookworm
environment:
POSTGRES_DB: miroha
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5

application:
platform: linux/x86_64
build:
context: .
dockerfile: Dockerfile
target: application
args:
BUNDLE_WITHOUT: test
ENVIRONMENT: development
ports:
- 3000:3000
environment:
DATABASE_URL: postgres://postgres:postgres@database:5432/miroha
depends_on:
database:
condition: service_healthy

0 comments on commit 2f8d320

Please sign in to comment.