Build a development environment for Docker + Rails6 + Postgresql

Premise

We introduced Docker so that team members who have not built a Rails environment can easily run the project locally without the hassle of building the environment. Since I am building the Rails environment locally, I created the project locally so that both Docker and local direct can work. It is assumed that Docker is already installed.

environment

Create new project

$ mkdir myapp; cd $_
$ rails new . --skip-coffee --skip-turbolinks --database=postgresql

Introduced dotenv-rails to manage environment variables

Depending on the environment variable, you can choose to run it with Docker or directly locally.

Gemfile


gem 'dotenv-rails'

install gem

$ bundle install

The following is an example of setting environment variables when running with Docker.

.env


DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASSWORD=secret

Set DATABASE_HOST = localhost to run directly locally.

File creation / editing for Docker

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    ports:
      - "5433:5432"
    environment:
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'secret'
      POSTGRES_DB: 'db'

  webpacker:
    build: .
    command: bundle exec bin/webpack-dev-server
    volumes:
      - .:/myapp
    ports:
      - "8080:8080"

  web:
    build: .
    command: /bin/sh -c "rm -f /myapp/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
      - webpacker

dockerfile


FROM ruby:2.7.1
 
ENV LANG C.UTF-8

#Install the required libraries
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

#yarn package management tool installation
RUN apt-get update && apt-get install -y curl apt-transport-https wget && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y yarn

#Work directory settings
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
EXPOSE  3000

Added host, ʻusername, and passwrdsettings todatabase.yml`

/config/database.yml


:abridgement

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV.fetch("DATABASE_HOST") { "127.0.0.1" } %>               #Postscript
  username: <%= ENV.fetch("DATABASE_USER") { "postgres" } %>            #Postscript
  password: <%= ENV.fetch("DATABASE_PASSWORD") { "" } %>                #Postscript
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

:abridgement

image construction

$ docker-compose build

Package installation

$ docker-compose run --rm web bin/yarn install

Database creation

$ docker-compose run --rm web rails db:create

Start-up

$ docker-compose up

You can access http: // localhost: 3000 /.

When moving directly locally

Edit .env and launch it with yarn install, rails db: create, rails s.

Recommended Posts

Build a development environment for Docker + Rails6 + Postgresql
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Build a development environment for Docker, java, vscode
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
Build a simple Docker + Django development environment
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build a development environment for Django + MySQL + nginx with Docker Compose
Build a WordPress development environment quickly with Docker
Build a simple Docker Compose + Django development environment
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a Ruby on Rails development environment on AWS Cloud9
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
Build a Node.js environment with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build Java development environment (for Mac)
Build Unity development environment on docker
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Building Rails 6 and PostgreSQL environment with Docker
Build a WAS execution environment from Docker
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Build a Java development environment on Mac
Build an Ultra96v2 development environment on Docker 1
How to build Rails 6 environment with Docker
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Build a development environment where Ruby on Rails breakpoints work on Windows
Build a browser test environment using Capybara in the Docker development environment
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Steps to build a Ruby on Rails development environment with Vagrant
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
Rails Docker environment construction
Build a Laravel / Docker environment with VSCode devcontainer
Creating a java web application development environment with docker for mac part1
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Build a container for Docker x Laravel phpMyAdmin
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
[Win10] Build a JSF development environment with NetBeans
A reminder of Docker and development environment construction
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Create a java web application development environment with docker for mac part2
Create a docker environment for Oracle 11g XE
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Build a Doker-based development environment on Windows 10 Home 2020 ver. Part 1 Until WSL2-based Docker build
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
Procedure for introducing Docker into the development environment of existing Rails applications [Rails, MySQL, Docker]
Build a development environment to create Ruby on Jets + React apps with Docker