Skip to content

Instantly share code, notes, and snippets.

@bpohoriletz
Last active March 7, 2022 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpohoriletz/02879b77505bd430daa36f84ce1b9467 to your computer and use it in GitHub Desktop.
Save bpohoriletz/02879b77505bd430daa36f84ce1b9467 to your computer and use it in GitHub Desktop.
ruby-3.1.0/rails-7.0.1/Potgress app with dv and test database inside Docker
# set ruby/rails version
export DOCKER_RAILS_VERSION="7.0.1"
export DOCKER_RUBY_VERSION="3.1.0"
export RAILS_PROJECT_NAME="rails-7-app-inside-docker-on-osx-part-2"
# create folder for the project and add Gemfile with necessary rails version
mkdir "$RAILS_PROJECT_NAME"
cd "$RAILS_PROJECT_NAME"
echo "ruby '$DOCKER_RUBY_VERSION'
source 'https://rubygems.org'
gem 'rails', '$DOCKER_RAILS_VERSION'" > Gemfile
# create folder for Docker configuration
mkdir .devcontainer
cd .devcontainer
# create Dockerfile for app
echo "FROM ruby:$DOCKER_RUBY_VERSION-slim
# install required dependencies
RUN apt-get update \
&& apt-get install -y make gcc git build-essential libpq-dev \
&& rm -rf /var/lib/apt/lists/*" > Dockerfile
# create configuration for mutagen-compose
echo "version: '3.8'
volumes:
web:
services:
# name of the service
web:
image: ruby-$DOCKER_RUBY_VERSION-rails-$DOCKER_RAILS_VERSION
build:
context: .
dockerfile: Dockerfile
volumes:
- web:/web
# set mounted directory as default
working_dir: /web
command: sleep infinity
environment:
- BUNDLE_PATH=vendor/bundle
ports:
- 3000:3000
web_development:
image: postgres:14
environment:
- LC_ALL=C.UTF-8
- POSTGRES_DB=web_development
- POSTGRES_USER=web_development
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- 54321:5432
web_test:
image: postgres:14
environment:
- LC_ALL=C.UTF-8
- POSTGRES_DB=web_test
- POSTGRES_USER=web_test
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- 54322:5432
# Set up Mutagen synchronization
# https://github.com/mutagen-io/mutagen-examples/blob/main/compose/web-go/compose.yml
x-mutagen:
sync:
defaults:
ignore:
vcs: true
web:
alpha: '..'
beta: 'volume://web'
mode: 'two-way-resolved'"> compose.yml
mutagen-compose up -d --build
mutagen-compose exec web bundle
mutagen-compose exec web bundle exec rails new . -f --database=postgresql
# use proper hosts for the database
# connect to web_development instead of localhost in development
mutagen-compose exec web sed -i 's/#host: localhost/host: web_development/' config/database.yml
# use web_development user instead of root in development for connection
mutagen-compose exec web sed -i 's/#username: web/username: web_development/' config/database.yml
# same here - use proper username and hosthane
mutagen-compose exec web sed -i '/database: web_test/i \
username: web_test\
host: web_test' config/database.yml
mutagen-compose exec web bundle exec rails s -b 0.0.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment