Skip to content

Commit

Permalink
Docker for development setup (first version)
Browse files Browse the repository at this point in the history
  • Loading branch information
pch committed Jan 21, 2024
1 parent 84cbc39 commit 71b6a71
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
54 changes: 54 additions & 0 deletions Dockerfile.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

ENV RAILS_ENV="development" \
BUNDLE_PATH="/gems" \
LANG=C.UTF-8 \
BUNDLE_JOBS=10 \
BUNDLE_RETRY=3

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

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential curl git libpq-dev node-gyp pkg-config python-is-python3

# Install JavaScript dependencies
ARG NODE_VERSION=18.16.0
ARG YARN_VERSION=1.22.19
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master

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

# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Copy application code
COPY . .

# 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 tmp
USER rails:rails

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
6 changes: 4 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ default: &default

development:
<<: *default
database: podapp_development
url: <%= ENV['DATABASE_URL'] || "postgres://localhost:5432" %>
database: boilerplate_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
Expand Down Expand Up @@ -57,7 +58,8 @@ development:
# Do not set this db to the same as development or production.
test:
<<: *default
database: podapp_test
url: <%= ENV['DATABASE_URL'] || ENV['DATABASE_URL_LOCAL'] %>
database: boilerplate_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
Expand Down
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.7'

x-backend: &backend
stdin_open: true
tty: true
build:
context: .
dockerfile: ./Dockerfile.development
volumes:
- .:/rails
- node_modules:/rails/node_modules
- gem_cache:/gems
depends_on:
- database
entrypoint: ./bin/docker-entrypoint

services:
rails:
<<: *backend
ports:
- '3000:3000'
environment:
- RUBY_DEBUG_OPEN=true
- DATABASE_URL=postgres://postgres:postgres@database:5432
command: ./bin/rails server -b 0.0.0.0

js:
<<: *backend
command: yarn build --watch

css:
<<: *backend
command: yarn build:css --watch

database:
image: 'postgres:15'
volumes:
- postgres:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres

volumes:
postgres:
gem_cache:
node_modules:

0 comments on commit 71b6a71

Please sign in to comment.