Building Rails 6 and PostgreSQL environment with Docker

Build environment with Rails6 + PostgreSQL with Docker

As a memorandum of my own, I will leave an article on how to build an environment with Rails 6 + PostgreSQL with Docker. This time, we will create the app name as shopping_app.

Each version of the environment to create

Directory structure

 .
 ├── Dockerfile
 ├── docker-compose.yml
 └── shopping_app
     ├── Gemfile
     └── Gemfile.lock

Creating a Dockerfile

First, create a Dockerfile.

FROM ruby:2.7.2
ENV LANG C.UTF-8


RUN 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

RUN apt-get update -qq && \
    apt-get install -y build-essential \
            libpq-dev \
            nodejs \
            postgresql-client yarn

RUN mkdir /app
RUN mkdir /app/shopping_app

ENV APP_ROOT /app/shopping_app
WORKDIR $APP_ROOT

ADD ./shopping_app/Gemfile $APP_ROOT/Gemfile
ADD ./shopping_app/Gemfile.lock $APP_ROOT/Gemfile.lock

RUN bundle install

ADD . $APP_ROOT

Create docker-compose.yml

Create docker-compose.yml. This time the port is set to 1501.

version: '3'
services:
  postgres:
    image: postgres
    ports:
      - "3306:3306"
    volumes:
      - ./tmp/db:/var/lib/postgresql/data #For MacOS
    environment:
      POSTGRES_USER: 'admin'
      POSTGRES_PASSWORD: 'admin-pass'
    restart: always
  app:
    build: .
    image: rails
    container_name: 'app'
    command: bundle exec rails s -p 1501 -b '0.0.0.0'
    ports:
      - "1501:1501"
    environment:
      VIRTUAL_PORT: 80
    volumes:
      - ./shopping_app:/app/shopping_app
    depends_on:
      - postgres
    restart: always

volumes:
  app_postgre:
    external: true

Creating a Gemfile

source 'https://rubygems.org'
gem 'rails', '6.0.3'

You can leave Gemfile.lock empty.

Build container to create app

$ docker-compose run app rails new . --force --database=postgresql --skip-bundle

webpacker install

docker-compose run app rails webpacker:install

Database creation and configuration

Set the database.yml of the created app.

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  #Describe the following
  user: admin
  password: admin-pass
  host: postgres

build image

docker-compose build

Creating Database

docker-compose run app rails db:create

This completes the construction. Launch the app with the following command

docker-compose up

http://localhost:1501/ If you access, it will be displayed on the following screen.

スクリーンショット 2020-10-18 20.16.20.png

that's all.

Recommended Posts

Building Rails 6 and PostgreSQL environment with Docker
Create Rails5 and postgresql environment with Docker and make pgadmin available
Notes on building Rails6 / PostgreSQL with Docker Compose
[Docker] Rails 5.2 environment construction with docker
Rails + MySQL environment construction with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Building an environment for creating apps with Rails and Vue
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
Environment construction summary with rvm and postgresql
How to build Rails 6 environment with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to install Pry after building Rails development environment with Docker
Rails Docker environment construction
Rails deploy with Docker
Try building Express + PostgreSQL + Sequelize with Docker [Part 2]
Build a development environment for Docker + Rails6 + Postgresql
(Basic authentication) environment variables in rails and Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Rails development environment created with VSCode and devcontainer
Try building Express + PostgreSQL + Sequelize with Docker [Part 1]
Prepare a scraping environment with Docker and Java
[Rails] How to build an environment with Docker
I built a rails environment with docker and mysql, but I got stuck
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Launching the production environment with docker + rails (ver5.2) and errors that occurred
Pytorch execution environment with Docker
Building an environment for WordPress, MySQL and phpMyAdmin with Docker Compose on EC2
Run Rails whenever with docker
Build docker environment with WSL
A memorandum when building an environment with Ruby3.0 x Rails6.1 x Docker x CentOS Stream
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build WordPress environment with Docker (Local) and AWS (Production)
React environment construction with Docker
Comfortable Docker environment created with WSL2 CentOS7 and Docker Desktop
[Docker] Use whenever with Docker + Rails
Installing and building Docker (memo)
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
Create a Vue3 environment with Docker!
Node.js environment construction with Docker Compose
Build Couchbase local environment with Docker
Environment construction with Docker for beginners
Build PlantUML environment with VSCode + Docker
Hello World with Docker and C
Create SolrCloud verification environment with Docker
Rails on Docker environment construction procedure
Create Laravel environment with Docker (docker-compose)
Microservices With Docker and Cloud Performance
Build docker + laravel environment with laradock
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
PostgreSQL environment construction with Docker (from setup to just before development)
Building a haskell environment with Docker + VS Code on Windows 10 Home
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]