Skip to content

[🤖 Infrapal Bot]Add Dockerfile, GitHub workflow, and Docker Compose #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Deploy to GitHub Container Registry

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write # Required to push to GitHub Container Registry

steps:
# Checkout the code
- name: Checkout Code
uses: actions/checkout@v3

# Authenticate to GitHub Container Registry
- name: Log in to GitHub Container Registry
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin

# Build and tag the Docker image
- name: Build and Tag Docker Image
run: |
docker build -t ruby-on-rails-dynamodb-demo .
docker tag ruby-on-rails-dynamodb-demo:latest ghcr.io/${{ github.repository_owner }}/ruby-on-rails-dynamodb-demo:latest

# Push the Docker image to GHCR
- name: Push Docker Image to GitHub Container Registry
run: |
docker push ghcr.io/${{ github.repository_owner }}/ruby-on-rails-dynamodb-demo:latest
75 changes: 13 additions & 62 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,20 @@
# syntax=docker/dockerfile:1
# check=error=true
# Use the official Ruby base image with the lighter version
FROM ruby:3.1.2-alpine

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t ruby_on_rails_dynamodb_demo .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name ruby_on_rails_dynamodb_demo ruby_on_rails_dynamodb_demo
# Set the working directory
WORKDIR /app

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.2.2
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

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

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
# Copy the Gemfile and Gemfile.lock
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .
# Install dependencies
RUN bundle install --without development test

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




# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER 1000:1000
# Copy the rest of the application files
COPY . .

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Expose the port the app runs on
EXPOSE 3000

# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80
CMD ["./bin/thrust", "./bin/rails", "server"]
# Set the entrypoint
ENTRYPOINT ["bin/docker-entrypoint"]
30 changes: 30 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.8'

services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
ports:
- "3000:3000"
environment:
- AWS_REGION=${AWS_REGION}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
command: ["bin/docker-entrypoint"]

dynamodb:
image: amazon/dynamodb-local
ports:
- "8000:8000"
environment:
- AWS_ACCESS_KEY_ID=your-access-key
- AWS_SECRET_ACCESS_KEY=your-secret-key
command: -jar DynamoDBLocal.jar -sharedDb
volumes:
- dynamodb_data:/home/dybamo/db

volumes:
dynamodb_data:
Loading