This time we will create 3 containers.

【Operating environment】
OS : macOS 10.14.6
Docker : 19.03.8
ruby : 2.6.5
rails : 5.2.4
[Prerequisites] Docker for Mac installed
First, create a Docker folder and an empty file. The contents of the file will be described later.

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
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;
  }
}
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
Edit the puma config file in the following config folder.

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
docker-compose is a mechanism to execute builds of multiple containers at once.
Create a docker-compose file directly under the project folder.

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'
Finally, set the database in database.yml. Since this time is a development environment, the settings up to development are described.

# 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
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
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.
Finally, type localhost in chrome to confirm.
Recommended Posts