[Docker] Rails 5.2 environment construction with docker

Premise

docker and docker-compose installed

1. Prepare file

Put Gemfile, Gemfile.lock, Dockerfile, Docker-compose.yml under ~ / Desktop / product-register.

  1. Dockerfile
FROM ruby:2.5
RUN apt-get update && apt-get install -y \
      build-essential \
      libpg-dev \
      nodejs \
      postgresql-client \
      yarn
WORKDIR /product-register
COPY Gemfile Gemfile.lock /product-register/
RUN bundle install
  1. Docker-compose.yml
version: '3'
services:
  web:
    build: .    //image If you get an image:Image name
    ports:
      - '3000:3000'
    volumes:
      - '.:/product-register'
    tty: true
    stdin_open: true

Note: Indentation and space are severe

  1. Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 5.2'

2. Create a container and create a Rails app

$ docker-compose up -d 
$ docker-compose exec web bash
:/product-register# rails new . --force --database=postgresql -skip-bundle //gemfile is updated
$ docker-compose down //Drop the container once to update the gemfile
$ docker-compose up --build -d To use the newly built image of dockerfile--Add build
$ docker-compose exec web bash //Enter again
:/product-register# rails s -b 0.0.0.0 //I get an error because the db is not set yet

3. DB settings

1. Modify config / database.yml of the created app

adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: db                  
  user: postgres       
  port: 5432
  password: <%= ENV.fetch("DATABASE_PASSWORD") %> 

2. Add the db part of the docker-compose.yml file

version: '3'

volumes:
  db-data:             //I am making docker volume
services:
  web:
    build: .
    ports:
      - '3000:3000'
    volumes:
      - '.:/app'
    environment:
      - 'DATABASE_PASSWORD=postgres'
    tty: true
    stdin_open: true
    depends_on:
      - db
    links:
      - db
  
  db:
    image: postgres
    volumes:
      - 'db-data:/var/lib/postgresql/data'    //I'm mounting the docker volume I made above
    environment:                      
      - 'POSTGRES_PASSWORD=postgres'   //Seems to have been needed recently

Recommended Posts

[Docker] Rails 5.2 environment construction with docker
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
Rails Docker environment construction
Rails environment construction with Docker (personal apocalypse)
React environment construction with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Docker environment construction
Node.js environment construction with Docker Compose
Environment construction with Docker for beginners
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Rails on Docker environment construction procedure
GPU environment construction with Docker [October 2020 version]
Laravel development environment construction with Docker (Mac)
Create Rails 6 + MySQL environment with Docker compose
Rails deploy with Docker
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
How to build Rails 6 environment with Docker
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Spring Boot environment construction with Docker (January 2021 version)
Environment construction command memo with Docker on AWS
React + Django + Nginx + MySQL environment construction with Docker
[Rails] How to build an environment with Docker
Wordpress local environment construction & development procedure with Docker
Pytorch execution environment with Docker
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
MySQL 5.7 (Docker) environment construction memo
Run Rails whenever with docker
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Rails / MySQL] Mac environment construction
[Docker] postgres, pgadmin4 environment construction
[Docker] Use whenever with Docker + Rails
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Stable development environment construction manual for "Rails6" with "Docker-compose"
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Database environment construction with Docker in Spring boot (IntellJ)
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Laravel + Docker Laradock usage environment construction
Build PlantUML environment with VSCode + Docker
Create SolrCloud verification environment with Docker
Ruby on Rails 6.0 environment construction memo
Create Laravel environment with Docker (docker-compose)
Troublesome Rails environment construction flow [Windows 10]
[Environment construction] Uninstall rails from local
[Rails] AWS EC2 instance environment construction
Build docker + laravel environment with laradock
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Easy environment construction of MySQL and Redis with Docker and Alfred
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)