Skip to content

Commit

Permalink
Added a Dockerfile and docker-compose.yml for local development (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yatish27 authored May 13, 2024
1 parent 095efd9 commit ac31e7a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 190 deletions.
5 changes: 3 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ POSTGRES_HOST=localhost # May need to be changed to `DB_HOST=db` if using devcon
POSTGRES_PORT=5432
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres

RAILS_HOSTNAME=
RAILS_ENV=development
SECRET_KEY_BASE=sample_secret_key_base
DOCKER_BUILDKIT=1
84 changes: 18 additions & 66 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,77 +1,29 @@
# syntax = docker/dockerfile:1

# Note: This Dockerfile is optimized for production deployment and isn't a good base
# for development enviroments.

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.1
FROM registry.docker.com/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 postgresql-client

# 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 install --no-install-recommends -y build-essential git libpq-dev pkg-config unzip
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim
RUN <<EOF
apt-get update
apt-get install -y build-essential libpq-dev curl unzip
EOF

ARG BUN_VERSION=1.1.8
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
ARG BUN_VERSION=1.1.8
RUN curl -fsSL https://bun.sh/install | bash -s -- "bun-v${BUN_VERSION}"

# Install application gems
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

# Install node modules
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

# Copy application code
COPY . .

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

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




# Final stage for app image
FROM base
WORKDIR /tmp
ADD Gemfile .
ADD Gemfile.lock .
RUN gem install bundler -v 2.5.10
RUN bundle install

# Clean up installation packages to reduce image size
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives
WORKDIR /app
ADD . /app

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

# 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
RUN bin/rails assets:precompile

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
ENV PORT 3000
EXPOSE $PORT

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
CMD rails s -b 0.0.0.0 -p $PORT
25 changes: 7 additions & 18 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV['POSTGRES_HOST'] %>
pool: 5
username: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>
pool: <%= ENV.fetch('RAILS_MAX_THREADS') { 5 } %>
username: <%= ENV.fetch('POSTGRES_USER') { 'postgres' } %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') { 'password' } %>
host: <%= ENV.fetch('POSTGRES_HOST') { 'db' } %>
timeout: 5000

development:
<<: *default
database: "shore_development"

database: shore_development
test:
<<: *default
database: "shore_test"

heroku:
<<: *default
database: "shore_heroku"

staging:
<<: *default
database: "shore_staging"

database: shore_test
production:
<<: *default
database: "shore_production"
database: shore_production
104 changes: 0 additions & 104 deletions docker-compose.yaml

This file was deleted.

58 changes: 58 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
volumes:
dbdata:
driver: local
services:
db:
image: postgres:16.3
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
volumes:
- dbdata:/var/lib/postgresql/data/pgdata
web:
build: .
tty: true
ports:
- "3000:3000"
environment:
- RAILS_ENV=development
- RACK_ENV=development
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- RAILS_SERVE_STATIC_FILES=false
- VITE_RUBY_HOST=vite
# volumes:
# - .:/app
depends_on:
- db
command: bundle exec rails s -b 0.0.0.0 -p 3000
worker:
build: .
environment:
- RAILS_ENV=development
- RACK_ENV=development
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- RAILS_SERVE_STATIC_FILES=false
# volumes:
# - .:/app
depends_on:
- db
command: bundle exec rake solid_queue:start
vite:
build: .
environment:
- RAILS_ENV=development
- RACK_ENV=development
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- RAILS_SERVE_STATIC_FILES=false
- VITE_RUBY_HOST=0.0.0.0
ports:
- "3036:3036"
volumes:
- ./app/frontend:/app/app/frontend
depends_on:
- db
command: bundle exec vite dev

0 comments on commit ac31e7a

Please sign in to comment.