How to build Rails 6 environment with Docker

Introduction

I will introduce how to build a Rail 6 environment with Docker. Webpack was introduced from Rails6, and the procedure is a little different from Rails5, so I also considered that.

Docker official page Quickstart: Compose and Rails

environment

Docker3 Rails6.0.3 Ruby2.7.1 DB:PostgeSQL

1. Create the required files

This time, we will build the environment in a folder named "myapp". If you want to change the name, please change "myapp" accordingly.

The file structure to be created from now on is as follows.

file organization


myapp
  --Dockerfile
  --Gemfile
  --Gemfile.lock
  --entrypoint.sh
  --docker-compose.yml

First, create a folder.

Terminal


$ mkdir myapp
$ cd myapp

1.1 Dockerfile

Terminal


$ touch Dockerfile

Dockerfile


FROM ruby:2.7.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client

##nodejs and yarn are required when installing webpack
#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_7.x | bash - && \
apt-get install nodejs

RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

1.2 Gemfile

Terminal


$ touch Gemfile

Gemfile


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

1.3 Gemfile.lock

Terminal


$ touch Gemfile.lock

Nothing is listed here.

1.4 entrypoint.sh

Terminal


$ touch entrypoint.sh

entrypoint.sh


#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

1.5 docker-compose.yml

Terminal


$ touch docker-compose.yml

docker-compose.yml


version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    
    #######add to#########################
    environment:
      POSTGRES_HOST_AUTH_METHOD: 'trust'
    ###################################

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

2. Build the project

2.1 rails new

Terminal


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

About command options --force: Overwrite the file even if it exists --no-deps: Do not start the linked service --database = postgresql: DB specifies PostgreSQL --skip-bundle: skip the bundle install (after creating the app)

2.2 docker-compose build Now that a new Gemfile has been created, use the following command to bundle install.

Terminal


$ docker-compose build

2.3 Change the connection settings to DB

config/database.yml


default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  #####add to#####
  host: db
  username: postgres
  passowrd:
  #############

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

2.4 Confirmation of Docker container startup

Terminal


$ docker-compose up

It is OK if the container starts and connects to the server. I haven't created the DB yet, so I get the following error. スクリーンショット 2020-06-09 10.33.12.png

2.5 DB creation

Since I was able to connect to the server, I created a DB. After disconnecting with Ctrl + C

Terminal


  #Stop Docker once
$ docker-compose stop
$ docker-compose down
 
  #DB creation
$ docker-compose run web rake db:create

  #Launch Docker again
$ docker-compose up

3. Make sure the container & server is running

If you connect to [http: // localhost: 3000 /](http: // localhost: 3000 /), you can confirm that it has started successfully. The version is Rails6, Ruby2.7.1 as set. スクリーンショット 2020-06-09 10.36.35.png

Other

You need to use Docker commands when developing within Docker.

$ docker-compose run web bundle exec rails command

Example


$ docker-compose run web bundle exec rails g model User
$ docker-compose run web bundle exec rails g controller Homepages index
$ docker-compose run web bundle exec rails g rspec:install 

reference

Docker official page Quickstart: Compose and Rails Steps for building a Ruby on Rails environment with Docker [Rails 6 compatible] I was addicted to building a Rails 6 development environment with Docker Postgres Docker Image has changed and can no longer be started [Rails] ActiveRecord::NoDatabaseError [Docker]

Recommended Posts

How to build Rails 6 environment with Docker
[Rails] How to build an environment with Docker
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
How to build docker environment with Gradle for intelliJ
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)
[Rails] How to use rails console with docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
How to build API with GraphQL and Rails
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to build Java development environment with VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker + Rails] How to deal with Rails server startup failure
Rails + MySQL environment construction with Docker
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
[Environment construction with Docker] Rails 6 & MySQL 8
How to get along with Rails
Build docker + laravel environment with laradock
How to build CloudStack using Docker
How to build an environment with Docker, which is the minimum required to start a Rails application
How to build an environment of [TypeScript + Vue + Express + MySQL] with Docker ~ Vue edition ~
How to set environment variables when using Payjp with Rails
Build a Node-RED environment with Docker to move and understand
How to share files with Docker Toolbox
Rails environment construction with Docker (personal apocalypse)
Building Rails 6 and PostgreSQL environment with Docker
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Deploy to heroku with Docker (Rails 6, MySQL)
Build a Wordpress development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
I tried to build the environment of PlantUML Server with Docker
Super beginner builds Rails6 + Postgresql environment with Docker to the end
Steps to build a Ruby on Rails development environment with Vagrant
How to write Rails
Rails Docker environment construction
Rails deploy with Docker
How to install Docker
How to uninstall Rails
Build a development environment for Docker + Rails6 + Postgresql
How to link Rails6 Vue (from environment construction)
Build a WordPress development environment quickly with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
How to give your image to someone with docker
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to easily implement numbers with pull-down
How to use docker compose with NVIDIA Jetson
How to use nginx-ingress-controller with Docker for Mac