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"'
$ 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
$ 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
$ docker-compose --version
docker-compose version 1.12.0, build b31ff33
#If there is no docker group
# sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo systemctl restart docker
$ exit
#Log in again
Working directory
$ sudo mkdir -p /var/webapp
$ sudo chown -R $USER:$USER /var/webapp
$ mkdir -p /var/webapp/containers/nginx
$ mkdir /var/webapp/environments
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
$ 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
$ 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;
  }
}
$ 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:
$ 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
$ 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
$ 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
$ 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
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'@'%' |
+------------------------------------------------+
$ docker-compose exec app rails db:create

$ docker-compose up -d
$ 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
$ sudo docker exec -it 【CONTAINER ID】 bash
root@0fb43cede898:/webapp# 
docker exec -it 0fb43cede898 ruby --version
        Recommended Posts