virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction

environment

windows10 HOME virtulbox + vagrant CentOS7 Docker 19.03.6 docker-compose 1.17.1 nginx 1.15.8 rails 5.2.4.1 ruby 2.5.1 MySQL 5.7

Vagrantfile Click Menu Bar Packages → Remot FTP → Toggle in ATOM

Click File → Add Project Folder

Select the folder where the vagrantfile is located and edit it

Uncomment line 35

35  config.vm.network "private_network", ip: "192.168.33.10"'

Docker installation

$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum makecache fast
$ yum list docker-ce.x86_64  --showduplicates |sort -r
#Choose from the ones that came out
$ sudo yum install -y docker-ce.x86_64

Start & Auto Start Settings

$ sudo systemctl enable docker
$ sudo systemctl start docker

Docker Compose

$ curl -L https://github.com/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > docker-compose
$ sudo mv docker-compose /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo visudo
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

Changed the above to below

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/local/bin:/usr/bin

Version confirmation

$ docker-compose --version
docker-compose version 1.12.0, build b31ff33

Put in docker group

#If there is no docker group
# sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo systemctl restart docker
$ exit
#Log in again

Directory creation

Working directory

$ sudo mkdir -p /var/webapp
$ sudo chown -R $USER:$USER /var/webapp

Nginx container directory

$ mkdir -p /var/webapp/containers/nginx

Environment variable directory

$ mkdir /var/webapp/environments

Create each file for container creation

The following is done in/var/webapp

Dockerfile for Rails

$ vim Dockerfile
FROM ruby:2.5.1

#Update repository and install dependent modules
RUN apt-get update -qq && \
    apt-get install -y build-essential \
                       nodejs

#Create a working directory with the name webapp directly under the root (application directory in the container)
RUN mkdir /webapp
WORKDIR /webapp

#Host Gemfile and Gemfile.Copy lock to container
ADD Gemfile /webapp/Gemfile
ADD Gemfile.lock /webapp/Gemfile.lock

#Run bundle install
RUN bundle install

#Copy everything in the host's application directory to a container
ADD . /webapp

# puma.Create a directory to place sock
RUN mkdir -p tmp/sockets

Gemfile

$ vim Gemfile
source 'https://rubygems.org'
gem 'rails', '5.2.0'

Gemfile.lock

$ touch Gemfile.lock

Dockerfile for Nginx

$ vim containers/nginx/Dockerfile
FROM nginx:1.15.8

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

#Copy Nginx config file to container
ADD nginx.conf /etc/nginx/conf.d/webapp.conf

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

Nginx config file

$ vim containers/nginx/nginx.conf
#Specify proxy destination
#Send the request received by Nginx to the backend puma
upstream webapp {
  #I want to communicate with sockets, so puma.Specify sock
  server unix:///webapp/tmp/sockets/puma.sock;
}

server {
  listen 80;
  #Specify domain or IP
  server_name 192.168.33.10;

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

  #Specifying the document root
  root /webapp/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 @webapp;
  keepalive_timeout 5;

  #Reverse proxy related settings
  location @webapp {
    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://webapp;
  }
}

Information file for DB connection

$ vim environments/db.env
MYSQL_ROOT_PASSWORD=Any password
MYSQL_USER=Any username
MYSQL_PASSWORD=Any password

docker-compose.yml

$ vim docker-compose.yml
version: '3'
services:
  app:
    build:
      context: .
    env_file:
      - ./environments/db.env
    command: bundle exec puma -C config/puma.rb
    volumes:
      - .:/webapp
      - public-data:/webapp/public
      - tmp-data:/webapp/tmp
      - log-data:/webapp/log
    depends_on:
      - db
  db:
    image: mysql:5.7
    env_file:
      - ./environments/db.env
    volumes:
      - db-data:/var/lib/mysql
  web:
    build:
      context: containers/nginx
    volumes:
      - public-data:/webapp/public
      - tmp-data:/webapp/tmp
    ports:
      - 80:80
    depends_on:
      - app
volumes:
  public-data:
  tmp-data:
  log-data:
  db-data:

Generate and edit Rails applications

$ docker-compose run --rm app rails new . --force --database=mysql --skip-bundle

Change permissions

$ sudo chown -R $USER:$USER .

Editing puma.rb

$ cp /dev/null config/puma.rb
$ vim config/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("../..", __FILE__)
bind "unix://#{app_root}/tmp/sockets/puma.sock"

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

Edit database.yml

$ cp /dev/null config/database.yml
$ vim config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch('MYSQL_USER') { 'Any username' } %>
  password: <%= ENV.fetch('MYSQL_PASSWORD') { 'Any password' } %>
  host: db

development:
  <<: *default
  database: webapp_development

test:
  <<: *default
  database: webapp_test

Build image and launch container

$ docker-compose build

Check if you can create it after building

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webapp_web          latest              3aaeba382d2c        5 seconds ago       109MB
webapp_app          latest              b5378e4870b4        17 seconds ago      1.03GB
<none>              <none>              cff3bcfe8b56        2 minutes ago       990MB
mysql               5.7                 84164b03fa2e        3 days ago          456MB
nginx               1.15.8              f09fe80eb0e7        13 months ago       109MB
ruby                2.5.1               3c8181e703d2        16 months ago       869MB

Container startup

$ docker-compose up -d

Check if the container is running

$ docker-compose ps
    Name                  Command               State          Ports
---------------------------------------------------------------------------
webapp_app_1   bundle exec puma -C config ...   Up
webapp_db_1    docker-entrypoint.sh mysqld      Up      3306/tcp, 33060/tcp
webapp_web_1   /bin/sh -c /usr/sbin/nginx ...   Up      0.0.0.0:80->80/tcp

DB settings

Granting authority

$ vim db/grant_user.sql
GRANT ALL PRIVILEGES ON *.* TO 'Any username'@'%';
FLUSH PRIVILEGES;

Inject queries from the host into the db container

$ docker-compose exec db mysql -u root -p -e"$(cat db/grant_user.sql)"

Check if the authority has been granted

$ docker-compose exec db mysql -u Any username-p -e"show grants;"
+------------------------------------------------+
| Grants for user_name@%                         |
+------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'Any username'@'%' |
+------------------------------------------------+

DB creation

$ docker-compose exec app rails db:create

Check if the Welcome page can be displayed by accessing 192.168.33.10.

無題dwadwadwa-1024x909.png

Container startup

$ docker-compose up -d

Check the running container

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
77a855c2588e        vagrant_web         "/bin/sh -c '/usr/sb…"   18 minutes ago      Up 18 minutes       0.0.0.0:80->80/tcp    vagrant_web_1
0fb43cede898        vagrant_app         "bundle exec puma -C…"   18 minutes ago      Up 18 minutes                             vagrant_app_1
b8f4fb3a8cc7        mysql:5.7           "docker-entrypoint.s…"   54 minutes ago      Up 54 minutes       3306/tcp, 33060/tcp   vagrant_db_1

Enter the container

$ sudo docker exec -it 【CONTAINER ID】 bash
root@0fb43cede898:/webapp# 

Execute the command on the host OS by specifying the container

docker exec -it 0fb43cede898 ruby --version

Recommended Posts

virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Rails + MySQL environment construction with Docker
[Environment construction with Docker] Rails 6 & MySQL 8
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Rails Docker environment construction
Rails6 [API mode] + MySQL5.7 environment construction with Docker
React + Django + Nginx + MySQL environment construction with Docker
MySQL 5.7 (Docker) environment construction memo
[Docker] Rails 5.2 environment construction with docker
[Rails / MySQL] Mac environment construction
Rails on Docker environment construction procedure
SQL statement learning ~ Environment construction ~ Docker + MySQL
Rails environment construction with Docker (personal apocalypse)
Create Rails 6 + MySQL environment with Docker compose
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Laravel + MySQL + phpMyadmin environment construction with Docker
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Rails & React & Webpacker & MySQL Environment Construction Manual
Docker environment construction
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
[Rails & Docker & MySQL environment construction] I started the container, but I can't find MySQL ...?
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
[Rails] Nginx, Puma environment deployment & server study [AWS EC2]
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
SSL in the local environment of Docker / Rails / puma
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Rails6 development environment construction [Mac]
Rails engineer environment construction ruby2.7.1
Rails6 (MySQL, Ubuntu environment, Cloud9)
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
Redmine (Docker) environment construction memo
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
React environment construction with Docker
Easy environment construction of MySQL and Redis with Docker and Alfred
vagrant + ContOS7 + php7.2 + nginx + phalcon4 environment construction and problem recording
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Rails 6 (API mode) + MySQL Docker environment creation by docker-compose (for Mac)
Laravel + Docker Laradock usage environment construction
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Ruby on Rails 6.0 environment construction memo
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
Build a development environment for Django + MySQL + nginx with Docker Compose
Troublesome Rails environment construction flow [Windows 10]
[Environment construction] Uninstall rails from local
[Rails] AWS EC2 instance environment construction
Check MySQL logs in Docker environment
[Rails & Docker & MySQL environment construction] Could not find gem ‘mysql2 (> = 0.4.4, <0.6.0)’ in any of the gem sources listed in your Gemfile.
I built a rails environment with docker and mysql, but I got stuck
[Rails API x Docker] Easy environment construction with shell & operation check with Flutter
Complete roadmap for building environment up to Docker + rails6 + MySQL + bootstrap, jquery
GPU environment construction with Docker [October 2020 version]
[Docker] Use environment variables in Nginx conf
Building Rails 6 and PostgreSQL environment with Docker
Laravel development environment construction with Docker (Mac)
Sapper × Go (echo) × Docker development environment construction
Migrate existing Rails 6 apps to Docker environment