docker メモ

構成考えたのでメモ

フォルダ構成
```
cert --  xxx-key.pem
         xxx-key.pem
deploy +-- nginx
           +-- conf
               +-- default.conf
           +-- storage
               +-- access.log
               +-- error.log
           +-- Dockerfile
       +--  rails
           +-- apps
           +-- storage
               +-- logs
                   +-- production.log
                   +-- puma_stderr.log
                   +-- puma_stdout.log
               +-- var_log  (osのmail.logなど)
           +-- Dockerfile
           +-- entrypoint.sh
       +--  mariadb
           +-- cnf
               +-- my.cnf
       +--  mariadb
       +--  docker-compose.yml
       +--  .env (docker-compose.yml用)
       
```

````
# データベース接続情報
DATABASE_URL=mysql2://xxxx:xxxxx@xxxx:3306/xxxx

# Rails 環境設定
RAILS_ENV=production

# データベースユーザー情報
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
MYSQL_DATABASE=
MYSQL_HOST=

# Rails サーバの設定
RAILS_SERVE_PORT=3000

# Nginx の公開ポート
NGINX_PORT=443
````

docker compose
```
services:
  nginx:
    container_name: nginx
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/storage/logs:/var/log/nginx

  app_blue:
    container_name: app_blue
    build: ./rails
    volumes:
      - ./rails/storage/logs:/apps/log
      - ./rails/storage/var_log:/var/log
    tty: true
    stdin_open: true
    working_dir: /apps
    environment:
      RAILS_ENV: ${RAILS_ENV}
      DATABASE_URL: ${DATABASE_URL}
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    depends_on:
      - app-db

  app_green:
    container_name: app_green
    build: ./rails
    volumes:
      - ./rails/storage/logs:/apps/log
      - ./rails/storage/var_log:/var/log
    ports:
      - "3001:3001"
    tty: true
    stdin_open: true
    working_dir: /apps
    environment:
      RAILS_ENV: ${RAILS_ENV}
      DATABASE_URL: ${DATABASE_URL}
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3001 -b '0.0.0.0'"
    depends_on:
      - app-db

  app-db:
    container_name: app-db
    image: mariadb:11.3
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_PORT: 3306
    volumes:
      - ./mariadb/storage/volume:/var/lib/mysql
      - ./mariadb/conf/my.cnf:/etc/mysql/my.cnf

  # phpMyAdmin のコンテナ
  app-pma:
    container_name: app-pma
    image: phpmyadmin/phpmyadmin:latest
    environment:
      PMA_HOST: app-db
      PMA_USER:  ${MYSQL_USER}
      PMA_PASSWORD: ${MYSQL_PASSWORD}
    ports:
      - "18080:80"
    depends_on:
      - app-db

```

DockerFile(nginx)
```
# base imgae（docker imagesコマンドで表示されたREPOSITORYを指定）
FROM  nginx:latest

#説明 「http { 」直後に 追加(/a )
RUN sed -i '/http {/a \    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;' /etc/nginx/nginx.conf

COPY ./conf/default.conf /etc/nginx/conf.d

RUN mkdir /cert

COPY ./cert/xxx.pem /cert/xxx.pem
COPY ./cert/xxx-key.pem /cert/xxx-key.pem

RUN echo "now building..."
```

DockerFile(nginx)
```
FROM ruby:3.1.2

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
    apt-get install -y \
        default-mysql-client \
        vim \
        telnet \
        postfix \
        rsyslog \
        logrotate && \
    rm -rf /var/lib/apt/lists/*

ENV SECRET_KEY_BASE=  xxxx # bundle exec rake secretでつくったもの

RUN mkdir /apps

WORKDIR /apps

# Install Ruby gems
COPY ./apps/Gemfile /apps/Gemfile
COPY ./apps/Gemfile.lock /apps/Gemfile.lock
RUN bundle install


RUN gem update --system && gem install bundler:2.3.14 \
    && bundle config set force_ruby_platform true \
    && bundle install


COPY ./apps /apps

RUN RAILS_ENV=production bundle exec rake assets:precompile

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh && \
    cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
ENTRYPOINT ["entrypoint.sh"]

RUN echo "mail.* -/var/log/mail.log" >> /etc/rsyslog.d/50-default.conf
CMD ["rsyslogd", "-n"]
CMD ["bash"]

#EXPOSE 3000
#CMD ["rails", "server", "-b", "0.0.0.0"]

RUN postconf -e 'maillog_file = /var/log/mail.log' && \
    postconf -e 'relayhost = 192.168.3.1'

````
entrypoint.sh
````
#!/bin/bash
set -e

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

/etc/init.d/postfix start

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

my.conf
````
[mysqld]
ft_min_word_len = 2
innodb_ft_min_token_size = 2
innodb_ft_enable_stopword = OFF
innodb_file_per_table = 1
innodb_file_format = Barracuda
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set=utf8mb4
````