Summary
Hi I'm Kei, front-end developer. This Quickstart guide shows you how to use Docker Compose to set up and run a Rails6/PostgreSQL app.
Define the project
First, make files as follows.
├── .env
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── docker-compose.yml
1.Update Gemfile and .env
Don't private your ".env" file.
# Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 6.0.0'
#.env
WORKDIR=app
API_PORT=3000
POSTGRES_PASSWORD=password
2.Update Docker file
FROM ruby:3.0.3-alpine
ARG WORKDIR
ARG RUNTIME_PACKAGES="nodejs tzdata postgresql-dev postgresql git"
ARG DEV_PACKAGES="build-base curl-dev"
ENV HOME=/${WORKDIR} \
LANG=C.UTF-8 \
TZ=Asia/Tokyo
WORKDIR ${HOME}
COPY Gemfile* ./
RUN apk update && \
apk upgrade && \
apk add --no-cache ${RUNTIME_PACKAGES} && \
apk add --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
bundle install -j4 && \
apk del build-dependencies
COPY . ./
CMD ["rails", "server", "-b", "0.0.0.0"]
3.Update docker-compose.yml
version: '3.8'
services:
db:
image: postgres:12.3-alpine
environment:
TZ: UTC
PGTZ: UTC
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
volumes:
- ./tmp/db:/var/lib/postgresql/data
api:
build:
context: ./
args:
WORKDIR: $WORKDIR
environment:
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
volumes:
- ./:/$WORKDIR
depends_on:
- db
ports:
- "$API_PORT:3000"
Build the project
docker-compose build
You can generate automatically the Rails template.
docker-compose run --rm api rails new . -f -B -d postgresql --api
Explanation of "rails new" options here.
<run [Container Name] [Command]>
You can create container and execute command like "rails c..." and so on.
< -B >
Don't run bundle install.
< -d >
Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
< --api >
Create Rails template with API mode.
Setting the database
Update database.yml that are generated "rails new" command.
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: <%= ENV["POSTGRES_PASSWORD"] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
# create db
docker-compose run --rm api rails db:create
# run container
docker-compose up api or docker-compose up api -d
Access localhost:3000.