[RUBY] Super beginner builds Rails6 + Postgresql environment with Docker to the end

Until the start of construction

I wanted to touch on Docker, so I decided to build the above environment. I repeated trial and error because I had no previous knowledge ... However, I managed to build it, so I will leave a note. I added ? to the annotations and explanations for the parts that I didn't understand well.

Required folders and files

Prepare a folder with an arbitrary name, and here it is my_app.

$ mkdir my_app

Prepare the following files in my_app

$ touch xxx(Required file name)

- my_app
    - .dockerignore
    - Dockerfile
    - docker-compose.yml
    - Gemfile
    - Gemfile.lock
    - entrypoint.sh

Edit each file as follows.

.dockerignore Prevents local modules and debug logs from being copied to the Docker image. The required modules are different between the Mac environment and the Linux environment on Docker ... Is it? Anyway, without this, I get an error related to Node.js 12.x.

.dockerignore:.dockerignore


node_modules
npm-debug.log

.Dockerfile Install postgresql and yarn and Node.js required for rails6 Webpacker. I can only roughly understand each command listed (Fu) I think I need to study ...

Dockerfile


FROM ruby:2.7.1

#Required library installation
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

#Install yarn package management tool
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

# Node.install js
RUN curl -SL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get install -y nodejs

#Build working folder in container
RUN mkdir /my_app
WORKDIR /my_app
COPY Gemfile /my_app/Gemfile
COPY Gemfile.lock /my_app/Gemfile.lock
RUN bundle install
COPY . /my_app

#entrypoint.sh and connection settings
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml Here, the user name and password used in Postgresql are postgres. There are web and db in the service.

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/my_app
    ports:
      - "3000:3000"
    depends_on:
      - db

Gemfile

Gemfile


source 'https://rubygems.org'
gem 'rails', '~> 6'

Gemfile.lock You don't need to edit the contents, just generate the file.

entrypoint.sh

entrypoint.sh


#!/bin/bash
set -e

#The server that has remained since the last rails server startup.Processing to erase pid?
rm -f /my_app/tmp/pids/server.pid

#Run the main process of the container(Command specified by CMD in Dockerfile)
exec "$@"

Creating a Rails app

run builds the web alone? And runs rails new inside the web container

$ docker-compose run web rails new . --force --no-deps --database=postgresql --skip-bundle

--no-deps: Do not start linked service (do not start db linked to web now?) --skip-bundle: Do not run bundle

bundle install The Gemfile has been rewritten with the previous rails new. By building the image, bundle install is also done, so execute the following command.

$ docker-compose build

Database settings

I think the working folder already has a set of familiar rails files, so Edit config / database.yml as follows.

config/database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  #docker-compose.Match the Postgresql user name and password written in yml
  username: postgres
  password: postgres
  pool: 5

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

Webpacker installation and configuration

Install webpacker

Run bundle exec rails webpacker: install inside the web container.

$ docker-compose run web bundle exec rails webpacker:install

webpacker settings

In my case, if I do not make this setting, an error will occur when starting the server. Edit the config / webpacker.yml generated by rails webpacker: install as follows:

config/webpacker.yml


development:
  dev_server:
    host: webpacker
    hmr: true

Also add the following code to config / environments / development.rb. But maybe this process alone is not necessary.

config/environments/development.rb


config.webpacker.check_yarn_integrity = false

Container startup

$ docker-compose up

At this point you can access http: // localhost: 3000 /, Since the DB has not been created yet, an error screen will be displayed. It's really rudimentary, but it's been a long way, so I was pretty impatient (laughs)

DB creation

$ docker-compose run web rails db:create

Construction completed

If you see "Yay! You're on Rails!" At http: // localhost: 3000 /, you're successful! If you have created unnecessary docker images or containers, please delete them yourself.

I think there are many points that cannot be reached for the first posted article. Thank you for staying with us until the end. If you find any mistakes in the article, please let us know.

reference

Docker + Rails6 + PostgreSQL environment construction Problem and solution that container falls due to Yarn error immediately after docker-compose up Make Node.js Web Application Docker I want to use an existing rails6 app with a combination of Docker and webpacker docker-compose I didn't understand the difference between'up',' build'and'start', so I summarized it.

Recommended Posts

Super beginner builds Rails6 + Postgresql environment with Docker to the end
Building Rails 6 and PostgreSQL environment with Docker
How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
[Docker] Rails 5.2 environment construction with docker
I made a development environment with rails6 + docker + postgreSQL + Materialize.
How to build Rails, Postgres, ElasticSearch development environment with Docker
Create Rails5 and postgresql environment with Docker and make pgadmin available
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
[Beginner] Procedure to log in to the virtual environment built with Vagrant
I tried to build the environment of PlantUML Server with Docker
How to install Pry after building Rails development environment with Docker
PostgreSQL environment construction with Docker (from setup to just before development)
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
Prepare the format environment with "Rails" (VScode)
How to build an environment with Docker, which is the minimum required to start a Rails application
Wait for PostgreSQL to start with Docker and then start the WEB service
Problems I was addicted to when building the digdag environment with docker
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
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
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Rails] rails new to create a database with PostgreSQL
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Read environment variables with ruby ​​file [Super beginner]
The code I used to connect Rails 3 to PostgreSQL 10
Notes on building Rails6 / PostgreSQL with Docker Compose
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] How to use PostgreSQL in Vagrant environment
Try to summarize the common layout with rails
Easy to display hello world with Rails + Docker
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to build docker environment with Gradle for intelliJ
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
Rails Docker environment construction
Docker the development environment of Ruby on Rails project
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
Rails deploy with Docker
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
[First team development ③] Share the development environment created with Docker
I introduced Docker to Rails 6, so I summarized it (beginner)
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
Addicted to the webpacker that comes standard with Rails 6
SSL in the local environment of Docker / Rails / puma
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Docker command to create Rails project with a single blow in environment without Ruby
[Docker + Rails] How to deal with Rails server startup failure
Debug the VSCode + Docker + PHP development environment with XDebug.
How to deal with the error yaml.scanner.ScannerError: while scanning for the next token that appeared in Rails environment construction with Docker
I tried to build an environment using Docker (beginner)
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
How to set environment variables when using Payjp with Rails
Wait for the container service to start with docker healthcheck