[RUBY] [Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker

[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker

table of contents


Construction image

This time we will create 3 containers.

image.png

Operating environment / prerequisites

【Operating environment】 OS : macOS 10.14.6 Docker : 19.03.8 ruby : 2.6.5 rails : 5.2.4

[Prerequisites] Docker for Mac installed

STEP1. Create a Dockerfile

First, create a Docker folder and an empty file. The contents of the file will be described later.

image.png

STEP1-1. nginx Dockerfile

Below is the nginx docker file. findpro-bangkok is the app name, so change it to your own app name.

ruby:Dockerfile:nginx



FROM nginx:1.15.8

#Delete in the include directory
RUN rm -f /etc/nginx/conf.d/*

#Copy Nginx config file to container
COPY /docker/nginx/nginx.conf /etc/nginx/conf.d/findpro-bangkok.conf

#Place public files such as images in nginx
RUN mkdir -p /findpro-bangkok/public
COPY ./public /findpro-bangkok/public

#Start Nginx after build is complete
CMD /usr/sbin/nginx -g 'daemon off;' -c /etc/nginx/nginx.conf

STEP1-2. nginx configuration file

Create nginx.conf that describes the nginx settings.

nginx.conf


#Specify proxy destination
#Send the request received by Nginx to the backend puma
upstream puma {
  #I want to communicate with sockets, so puma.Specify sock
  server unix:///findpro-bangkok/tmp/sockets/puma.sock;
}

server {
  listen 80 default;
  #Specify domain or IP
  server_name localhost;

  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log;

  #Specifying the document root
  root /findpro-bangkok/public;

  client_max_body_size 100m;
  error_page 404             /404.html;
  error_page 505 502 503 504 /500.html;
  try_files  $uri/index.html $uri @puma;
  keepalive_timeout 5;

  #Reverse proxy related settings
  location @puma {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_pass http://puma;
  }
}

STEP1-3. Rails Dockerfile

Next, create a Dockerfile for rails.

Dockerfile


FROM ruby:2.6.5

RUN apt-get update -qq && \
  apt-get install -y apt-utils \
  build-essential \
  libpq-dev \
  nodejs \
  vim

#Change to your own app name
ENV RAILS_ROOT /findpro-bangkok

RUN mkdir $RAILS_ROOT
WORKDIR $RAILS_ROOT

COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler:2.1.4
RUN bundle install

#Copy all the files in the project folder into the Rails container
COPY . .

#Create a directory for socket communication
RUN mkdir -p tmp/sockets

STEP2. Puma settings

Edit the puma config file in the following config folder.

image.png

puma.rb


threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }.to_i
threads threads_count, threads_count
port        ENV.fetch('PORT') { 3000 }
environment ENV.fetch('RAILS_ENV') { 'development' }
plugin :tmp_restart

app_root = File.expand_path('..', __dir__)
bind "unix://#{app_root}/tmp/sockets/puma.sock"

stdout_redirect "#{app_root}/log/puma.stdout.log", "#{app_root}/log/puma.stderr.log", true

STEP3. Create docker-compose

docker-compose is a mechanism to execute builds of multiple containers at once.

Create a docker-compose file directly under the project folder.

image.png

docker-compose.yml


version: '3'
volumes:
  tmp_data:
  public_data:
services:
  nginx:
    build:
      context: ./
      dockerfile: ./docker/nginx/Dockerfile
    ports:
      - '80:80'
    volumes:
      - public_data:/findpro-bangkok/public
      - tmp_data:/findpro-bangkok/tmp/sockets
    depends_on:
      - app
    links:
      - app
  app:
    build:
      context: ./
      dockerfile: ./docker/rails/Dockerfile
    command: bundle exec puma
    volumes:
     #.:/findpro-Syncing the current folder of the project and the folder of the container with bangkok.This allows the changes to be reflected in the container without having to build again..
      - .:/findpro-bangkok:cached
      #tmp on host computer for socket communication with nginx_Create a file called data and share it with the nginx container.
      - tmp_data:/findpro-bangkok/tmp/sockets
      - public_data:/findpro-bangkok/public
    tty: true
    stdin_open: true
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: 'postgres'

STEP4. Database settings

Finally, set the database in database.yml. Since this time is a development environment, the settings up to development are described.

image.png

# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  port: 5432
  username: postgres
  password: postgres
  pool: 5

  # For details on connection pooling, see Rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: findpro-bangkok_development

###Omitted below

STEP5. Build the container

Now, let's start the created container.

First, go to the project folder in the terminal and execute the following command.

[JS-MAC findpro-bangkok]$ docker-compose build

Execute the following command after the build is successful.

[JS-MAC findpro-bangkok]$ docker-compose up

STEP6. Creating a database

Finally we need to create a database Start another new terminal and enter the Rails container.

[@JS-MAC findpro-bangkok]$ docker ps #Rails container(findpro-bangkok_app)Check the ID of
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                NAMES
4549423f21ed        findpro-bangkok_nginx   "/bin/sh -c '/usr/sb…"   44 minutes ago      Up 3 seconds        0.0.0.0:80->80/tcp   findpro-bangkok_nginx_1
22ecc47308ae        findpro-bangkok_app     "bundle exec puma"       44 minutes ago      Up 5 seconds                             findpro-bangkok_app_1
755cb83a9bf4        postgres                "docker-entrypoint.s…"   44 minutes ago      Up 5 seconds        5432/tcp             findpro-bangkok_db_1
[@JS-MAC findpro-bangkok]$ docker exec -it 22e /bin/sh #Log in to your Rails container.22e is an acronym for app container ID.
# rails db:create #Create a database.
Database 'findpro-bangkok_development' already exists
Database 'findpro-bangkok_test' already exists
# rails db:migrate #Perform migration.

STEP7. Confirmation

Finally, type localhost in chrome to confirm.

Recommended Posts

[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to build Rails 6 environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Rails] How to build an environment with Docker
How to build an environment with Docker, which is the minimum required to start a Rails application
Build a Node-RED environment with Docker to move and understand
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
How to install Pry after building Rails development environment with Docker
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Steps to build a Ruby on Rails development environment with Vagrant
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
How to build API with GraphQL and Rails
How to build a Ruby on Rails environment using Docker (for Docker beginners)
I tried to build a Firebase application development environment with Docker in 2020
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
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)
How to build Java development environment with VS Code
Try to build a Java development environment using Docker
Build a development environment to create Ruby on Jets + React apps with Docker
Build a Node.js environment with Docker
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build environment with vue.js + rails + docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Build Rails environment with Docker Compose
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
I tried to create a padrino development environment with Docker
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Build a development environment for Django + MySQL + nginx with Docker Compose
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
Building Rails 6 and PostgreSQL environment with Docker
[Rails] How to use rails console with docker
Build a simple Docker + Django development environment
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
I built a rails environment with docker and mysql, but I got stuck
How to execute with commands of normal development language in Docker development environment
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
[Node.js express Docker] How to define Docker environment variables and load them with node.js
Build a Laravel / Docker environment with VSCode devcontainer
Build a simple Docker Compose + Django development environment
Rails6.0 ~ How to create an eco-friendly development environment
[Win10] Build a JSF development environment with NetBeans
Rails development environment created with VSCode and devcontainer
Prepare a scraping environment with Docker and Java
A reminder of Docker and development environment construction
Build a development environment for Docker, java, vscode
How to build a Pytorch environment on Ubuntu
[First team development ②] Build an environment with Docker
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code