Homebrew + rbenv + Bundler + dockerで開発環境をマネージドに構築する（番外編-Ruby on Railsアプリ）

#### 〜[後編](f3bc148e59fbad21eab9)〜からの続き

## 前提

- 便宜上、既存のワーキングコピーがあります
- ただし、 `Dockerfile` や `docker-compose.yml` は後編からガラッと変わります

## Docker手順

### Dockerfileのコマンド

| コマンド | 説明 |
|:--------|:----|
| FROM | 使用するイメージとバージョン |
| RUN | コマンドの実行。railsに必要な必要なnodejsとpostgeqsqlをインストールしている |
| WORKDIR | そのままの意味。作業ディレクトリを設定します。 |
| ADD | ローカルのファイルをコンテナへコピーする（昔のCOPYコマンドになります） |
| ENTRYPOINT | 一番最初に実行するコマンド（ここではentrypoint.shを参照） |
| EXPOSE | コンテナがリッスンするport番号 |
| CMD | イメージ内部のソフトウェア実行（つまりRailsのことですね） |

##### １. Dockerfileを作成する

```dockerfile:Dockerfile
FROM ruby:2.6.5

# Setting environment
ENV LANG C.UTF-8
ENV TZ Asia/Tokyo
ENV APP_HOME /var/www/capistrano_sample_app_v1

# Install libraries
RUN apt-get update -qq && \
    apt-get install -y build-essential \ 
                       libpq-dev \        
                       nodejs \           
                       vim \
                       default-mysql-client
RUN gem install bundler -v '2.1.4'

# Create app home
RUN mkdir -p $APP_HOME

WORKDIR $APP_HOME

# Copy Gemfile from origin
ADD Gemfile $APP_HOME/Gemfile

RUN bundle _2.1.4_ install --path vendor/bundle

ADD . $APP_HOME
```

##### ２. docker-composeを作成する

```docker:docker-compose.yml
version: '3'
services:
  mysql:
    # https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
    image: mysql:5.7
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      MYSQL_DATABASE: capistrano_sample
      MYSQL_USER: developer
      MYSQL_PASSWORD: %masking%
      MYSQL_ROOT_PASSWORD: password
    ports:
      - "3306:3306"
    command: mysqld --innodb_file_per_table=1 --innodb_file_format=barracuda --innodb_large_prefix=1
  redis:
    # https://github.com/RedisLabs/docker-library-redis
    image: redis:3.2-alpine
  # memcached:
    # https://github.com/autopilotpattern/memcached/releases
    # image: memcached:1.4-alpine
  app:
    build:
      context: .
      dockerfile: "Dockerfile"
    tty: true
    stdin_open: true
    ports:
      - "8080:8080"
    # environment:
      # RAILS_LOG_TO_STDOUT: "true"
      # STACKDRIVER_LOGGING_MODE: "agent"
    # command: /bin/sh -c "rm -f /capistrano_sample_app_v1/tmp/pids/server.pid && bundle exec rails s -p 8080 -b '0.0.0.0'"
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 8080 -b '0.0.0.0'"
    volumes:
      # ホストのカレントディレクトリをコンテナの/appにマウント
      - "./:/app"
    links:
      - mysql
      - redis
      # - memcached
```

##### 3.docker build

```sh:Terminal
$ docker-compose up --build
Creating network "capistrano_sample_app_v1_default" with the default driver
Pulling mysql (mysql:5.7)...
5.7: Pulling from library/mysql
804555ee0376: Pull complete
c53bab458734: Pull complete
ca9d72777f90: Pull complete
2d7aad6cb96e: Pull complete
8d6ca35c7908: Pull complete
6ddae009e760: Pull complete
327ae67bbe7b: Pull complete
9e05241b7707: Pull complete
e822978df8f0: Pull complete
14ca71ed53be: Pull complete
026afe6fd35e: Pull complete
Digest: sha256:2ca675966612f34b4036bbcfa68cb049c03e34b561fba0f88954b03931823d29
Status: Downloaded newer image for mysql:5.7
Pulling redis (redis:3.2-alpine)...
3.2-alpine: Pulling from library/redis
4fe2ade4980c: Pull complete
fb758dc2e038: Pull complete
989f7b0c858b: Pull complete
42b4b9f869ad: Pull complete
17e06138ef20: Pull complete
c0ecd66db81e: Pull complete
Digest: sha256:e9083e10f5f81d350a3f687d582aefd06e114890b03e7f08a447fa1a1f66d967
Status: Downloaded newer image for redis:3.2-alpine
Building app
Step 1/11 : FROM ruby:2.6.5
 ---> a161c3e3dda8
Step 2/11 : ENV LANG C.UTF-8
 ---> Using cache
 ---> 5e1f7a284c55
Step 3/11 : ENV TZ Asia/Tokyo
 ---> Using cache
 ---> 36064309c74a
Step 4/11 : ENV APP_HOME /var/www/capistrano_sample_app_v1
 ---> Using cache
 ---> b6ad93523f4f
Step 5/11 : RUN apt-get update -qq &&     apt-get install -y build-essential                        libpq-dev                        nodejs                        vim                        default-mysql-client
 ---> Using cache
 ---> 27f7958c4205
Step 6/11 : RUN gem install bundler -v '2.1.4'
 ---> Using cache
 ---> 2152e68a959d
Step 7/11 : RUN mkdir -p $APP_HOME
 ---> Using cache
 ---> 34bf7f30bbcb
Step 8/11 : WORKDIR $APP_HOME
 ---> Using cache
 ---> 1be2dad77d77
Step 9/11 : ADD Gemfile $APP_HOME/Gemfile
 ---> Using cache
 ---> f2549fe43d36
Step 10/11 : RUN bundle _2.1.4_ install --path vendor/bundle
 ---> Using cache
 ---> 55ba3d1dbf0c
Step 11/11 : ADD . $APP_HOME
 ---> 4d459e27e55e
Successfully built 4d459e27e55e
Successfully tagged capistrano_sample_app_v1_app:latest
Creating capistrano_sample_app_v1_mysql_1 ... done
Creating capistrano_sample_app_v1_redis_1 ... done
Creating capistrano_sample_app_v1_app_1   ... done
Attaching to capistrano_sample_app_v1_redis_1, capistrano_sample_app_v1_mysql_1, capistrano_sample_app_v1_app_1
mysql_1  | 2020-01-16 09:55:41+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
redis_1  | 1:C 16 Jan 09:55:40.955 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
mysql_1  | 2020-01-16 09:55:45+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
redis_1  |                 _._                                                  
redis_1  |            _.-``__ ''-._                                             
redis_1  |       _.-``    `.  `_.  ''-._           Redis 3.2.12 (00000000/0) 64 bit
redis_1  |   .-`` .-```.  ```\/    _.,_ ''-._                                   
redis_1  |  (    '      ,       .-`  | `,    )     Running in standalone mode
redis_1  |  |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
redis_1  |  |    `-._   `._    /     _.-'    |     PID: 1
redis_1  |   `-._    `-._  `-./  _.-'    _.-'                                   
redis_1  |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
redis_1  |  |    `-._`-._        _.-'_.-'    |           http://redis.io        
redis_1  |   `-._    `-._`-.__.-'_.-'    _.-'                                   
redis_1  |  |`-._`-._    `-.__.-'    _.-'_.-'|                                  
redis_1  |  |    `-._`-._        _.-'_.-'    |                                  
redis_1  |   `-._    `-._`-.__.-'_.-'    _.-'                                   
redis_1  |       `-._    `-.__.-'    _.-'                                       
redis_1  |           `-._        _.-'                                           
redis_1  |               `-.__.-'                                               
redis_1  | 
redis_1  | 1:M 16 Jan 09:55:40.961 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 16 Jan 09:55:40.961 # Server started, Redis version 3.2.12
redis_1  | 1:M 16 Jan 09:55:40.961 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 16 Jan 09:55:40.961 * The server is now ready to accept connections on port 6379
mysql_1  | 2020-01-16 09:55:45+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
mysql_1  | 2020-01-16 09:55:45+00:00 [Note] [Entrypoint]: Initializing database files
mysql_1  | 2020-01-16T09:55:45.597324Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1  | 2020-01-16T09:55:45.635369Z 0 [Warning] InnoDB: Using innodb_file_format is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
mysql_1  | 2020-01-16T09:55:48.077398Z 0 [Warning] InnoDB: New log files created, LSN=45790
mysql_1  | 2020-01-16T09:55:48.646754Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
mysql_1  | 2020-01-16T09:55:48.952749Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5f8348e1-3846-11ea-8ab5-0242ac130003.
mysql_1  | 2020-01-16T09:55:49.020134Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
mysql_1  | 2020-01-16T09:55:51.294446Z 0 [Warning] CA certificate ca.pem is self signed.
app_1    | from /var/www/capistrano_sample_app_v1/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap.rb:22:in `setup': The 'disable_trace' method is not allowed with this Ruby version. current: 2.6.5, allowed version: < 2.5.0
mysql_1  | 2020-01-16T09:55:51.879252Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
app_1    | => Booting Puma
app_1    | => Rails 5.2.4.1 application starting in development 
app_1    | => Run `rails server -h` for more startup options
app_1    | Puma starting in single mode...
app_1    | * Version 3.12.2 (ruby 2.6.5-p114), codename: Llamas in Pajamas
app_1    | * Min threads: 5, max threads: 5
app_1    | * Environment: development
app_1    | * Listening on tcp://0.0.0.0:8080
app_1    | Use Ctrl-C to stop
mysql_1  | 2020-01-16 09:55:58+00:00 [Note] [Entrypoint]: Database files initialized
mysql_1  | 2020-01-16 09:55:58+00:00 [Note] [Entrypoint]: Starting temporary server
mysql_1  | 2020-01-16 09:55:58+00:00 [Note] [Entrypoint]: Waiting for server startup
mysql_1  | 2020-01-16T09:55:58.626438Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1  | 2020-01-16T09:55:58.627939Z 0 [Note] mysqld (mysqld 5.7.29) starting as process 80 ...
mysql_1  | 2020-01-16T09:55:58.631949Z 0 [Warning] InnoDB: Using innodb_file_format is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
mysql_1  | 2020-01-16T09:55:58.632065Z 0 [Note] InnoDB: PUNCH HOLE support available
mysql_1  | 2020-01-16T09:55:58.632232Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_1  | 2020-01-16T09:55:58.632267Z 0 [Note] InnoDB: Uses event mutexes
mysql_1  | 2020-01-16T09:55:58.632275Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql_1  | 2020-01-16T09:55:58.632281Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql_1  | 2020-01-16T09:55:58.632286Z 0 [Note] InnoDB: Using Linux native AIO
mysql_1  | 2020-01-16T09:55:58.632901Z 0 [Note] InnoDB: Number of pools: 1
mysql_1  | 2020-01-16T09:55:58.633235Z 0 [Note] InnoDB: Using CPU crc32 instructions
mysql_1  | 2020-01-16T09:55:58.634985Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mysql_1  | 2020-01-16T09:55:58.644127Z 0 [Note] InnoDB: Completed initialization of buffer pool
mysql_1  | 2020-01-16T09:55:58.646140Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mysql_1  | 2020-01-16T09:55:58.660112Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_1  | 2020-01-16T09:55:58.702341Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mysql_1  | 2020-01-16T09:55:58.702446Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mysql_1  | 2020-01-16T09:55:58.916889Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mysql_1  | 2020-01-16T09:55:58.917944Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
mysql_1  | 2020-01-16T09:55:58.917995Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
mysql_1  | 2020-01-16T09:55:58.919329Z 0 [Note] InnoDB: 5.7.29 started; log sequence number 2629932
mysql_1  | 2020-01-16T09:55:58.919668Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1  | 2020-01-16T09:55:58.920063Z 0 [Note] Plugin 'FEDERATED' is disabled.
mysql_1  | 2020-01-16T09:55:58.922411Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200116  9:55:58
mysql_1  | 2020-01-16T09:55:58.926524Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
mysql_1  | 2020-01-16T09:55:58.926589Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
mysql_1  | 2020-01-16T09:55:58.927349Z 0 [Warning] CA certificate ca.pem is self signed.
mysql_1  | 2020-01-16T09:55:58.927419Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
mysql_1  | 2020-01-16T09:55:58.929692Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2020-01-16T09:55:58.938200Z 0 [Note] Event Scheduler: Loaded 0 events
mysql_1  | 2020-01-16T09:55:58.938729Z 0 [Note] mysqld: ready for connections.
mysql_1  | Version: '5.7.29'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
mysql_1  | 2020-01-16 09:55:59+00:00 [Note] [Entrypoint]: Temporary server started.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_1  | 
mysql_1  | 2020-01-16 09:56:06+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql_1  | 2020-01-16T09:56:06.599321Z 0 [Note] Giving 0 client threads a chance to die gracefully
mysql_1  | 2020-01-16T09:56:06.599402Z 0 [Note] Shutting down slave threads
mysql_1  | 2020-01-16T09:56:06.599412Z 0 [Note] Forcefully disconnecting 0 remaining clients
mysql_1  | 2020-01-16T09:56:06.599422Z 0 [Note] Event Scheduler: Purging the queue. 0 events
mysql_1  | 2020-01-16T09:56:06.599572Z 0 [Note] Binlog end
mysql_1  | 2020-01-16T09:56:06.600707Z 0 [Note] Shutting down plugin 'ngram'
mysql_1  | 2020-01-16T09:56:06.600763Z 0 [Note] Shutting down plugin 'partition'
mysql_1  | 2020-01-16T09:56:06.600771Z 0 [Note] Shutting down plugin 'BLACKHOLE'
mysql_1  | 2020-01-16T09:56:06.600777Z 0 [Note] Shutting down plugin 'ARCHIVE'
mysql_1  | 2020-01-16T09:56:06.600781Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
mysql_1  | 2020-01-16T09:56:06.600812Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
mysql_1  | 2020-01-16T09:56:06.600817Z 0 [Note] Shutting down plugin 'MyISAM'
mysql_1  | 2020-01-16T09:56:06.600831Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
mysql_1  | 2020-01-16T09:56:06.600836Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
mysql_1  | 2020-01-16T09:56:06.601155Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
mysql_1  | 2020-01-16T09:56:06.601163Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
mysql_1  | 2020-01-16T09:56:06.601167Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
mysql_1  | 2020-01-16T09:56:06.601170Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
mysql_1  | 2020-01-16T09:56:06.601173Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
mysql_1  | 2020-01-16T09:56:06.601176Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
mysql_1  | 2020-01-16T09:56:06.601179Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
mysql_1  | 2020-01-16T09:56:06.601182Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
mysql_1  | 2020-01-16T09:56:06.601186Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
mysql_1  | 2020-01-16T09:56:06.601189Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
mysql_1  | 2020-01-16T09:56:06.601192Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
mysql_1  | 2020-01-16T09:56:06.601195Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
mysql_1  | 2020-01-16T09:56:06.601198Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
mysql_1  | 2020-01-16T09:56:06.601201Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
mysql_1  | 2020-01-16T09:56:06.601204Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
mysql_1  | 2020-01-16T09:56:06.601207Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
mysql_1  | 2020-01-16T09:56:06.601211Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
mysql_1  | 2020-01-16T09:56:06.601214Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
mysql_1  | 2020-01-16T09:56:06.601217Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
mysql_1  | 2020-01-16T09:56:06.601220Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
mysql_1  | 2020-01-16T09:56:06.601223Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
mysql_1  | 2020-01-16T09:56:06.601226Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
mysql_1  | 2020-01-16T09:56:06.601229Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
mysql_1  | 2020-01-16T09:56:06.601232Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
mysql_1  | 2020-01-16T09:56:06.601236Z 0 [Note] Shutting down plugin 'INNODB_CMP'
mysql_1  | 2020-01-16T09:56:06.601239Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
mysql_1  | 2020-01-16T09:56:06.601242Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
mysql_1  | 2020-01-16T09:56:06.601245Z 0 [Note] Shutting down plugin 'INNODB_TRX'
mysql_1  | 2020-01-16T09:56:06.601248Z 0 [Note] Shutting down plugin 'InnoDB'
mysql_1  | 2020-01-16T09:56:06.601457Z 0 [Note] InnoDB: FTS optimize thread exiting.
mysql_1  | 2020-01-16T09:56:06.602096Z 0 [Note] InnoDB: Starting shutdown...
mysql_1  | 2020-01-16T09:56:06.703296Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
mysql_1  | 2020-01-16T09:56:06.703781Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 200116  9:56:06
mysql_1  | 2020-01-16T09:56:08.246243Z 0 [Note] InnoDB: Shutdown completed; log sequence number 12441955
mysql_1  | 2020-01-16T09:56:08.249558Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
mysql_1  | 2020-01-16T09:56:08.249620Z 0 [Note] Shutting down plugin 'MEMORY'
mysql_1  | 2020-01-16T09:56:08.249628Z 0 [Note] Shutting down plugin 'CSV'
mysql_1  | 2020-01-16T09:56:08.249632Z 0 [Note] Shutting down plugin 'sha256_password'
mysql_1  | 2020-01-16T09:56:08.249635Z 0 [Note] Shutting down plugin 'mysql_native_password'
mysql_1  | 2020-01-16T09:56:08.249751Z 0 [Note] Shutting down plugin 'binlog'
mysql_1  | 2020-01-16T09:56:08.253458Z 0 [Note] mysqld: Shutdown complete
mysql_1  | 
mysql_1  | 2020-01-16 09:56:08+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql_1  | 
mysql_1  | 2020-01-16 09:56:08+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql_1  | 
mysql_1  | 2020-01-16T09:56:09.018888Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_1  | 2020-01-16T09:56:09.020190Z 0 [Note] mysqld (mysqld 5.7.29) starting as process 1 ...
mysql_1  | 2020-01-16T09:56:09.024040Z 0 [Warning] InnoDB: Using innodb_file_format is deprecated and the parameter may be removed in future releases. See http://dev.mysql.com/doc/refman/5.7/en/innodb-file-format.html
mysql_1  | 2020-01-16T09:56:09.024328Z 0 [Note] InnoDB: PUNCH HOLE support available
mysql_1  | 2020-01-16T09:56:09.024371Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_1  | 2020-01-16T09:56:09.024378Z 0 [Note] InnoDB: Uses event mutexes
mysql_1  | 2020-01-16T09:56:09.024382Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mysql_1  | 2020-01-16T09:56:09.024388Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql_1  | 2020-01-16T09:56:09.024395Z 0 [Note] InnoDB: Using Linux native AIO
mysql_1  | 2020-01-16T09:56:09.024922Z 0 [Note] InnoDB: Number of pools: 1
mysql_1  | 2020-01-16T09:56:09.025108Z 0 [Note] InnoDB: Using CPU crc32 instructions
mysql_1  | 2020-01-16T09:56:09.026850Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
mysql_1  | 2020-01-16T09:56:09.036547Z 0 [Note] InnoDB: Completed initialization of buffer pool
mysql_1  | 2020-01-16T09:56:09.038578Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
mysql_1  | 2020-01-16T09:56:09.061020Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_1  | 2020-01-16T09:56:10.650530Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
mysql_1  | 2020-01-16T09:56:10.650727Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
mysql_1  | 2020-01-16T09:56:10.920702Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
mysql_1  | 2020-01-16T09:56:10.922096Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
mysql_1  | 2020-01-16T09:56:10.922158Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
mysql_1  | 2020-01-16T09:56:10.922898Z 0 [Note] InnoDB: Waiting for purge to start
mysql_1  | 2020-01-16T09:56:10.973211Z 0 [Note] InnoDB: 5.7.29 started; log sequence number 12441955
mysql_1  | 2020-01-16T09:56:10.973628Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
mysql_1  | 2020-01-16T09:56:10.973931Z 0 [Note] Plugin 'FEDERATED' is disabled.
mysql_1  | 2020-01-16T09:56:10.979959Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200116  9:56:10
mysql_1  | 2020-01-16T09:56:10.981172Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
mysql_1  | 2020-01-16T09:56:10.981228Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
mysql_1  | 2020-01-16T09:56:10.981958Z 0 [Warning] CA certificate ca.pem is self signed.
mysql_1  | 2020-01-16T09:56:10.982025Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
mysql_1  | 2020-01-16T09:56:10.982602Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
mysql_1  | 2020-01-16T09:56:10.982676Z 0 [Note] IPv6 is available.
mysql_1  | 2020-01-16T09:56:10.991740Z 0 [Note]   - '::' resolves to '::';
mysql_1  | 2020-01-16T09:56:10.991902Z 0 [Note] Server socket created on IP: '::'.
mysql_1  | 2020-01-16T09:56:10.994292Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2020-01-16T09:56:11.005402Z 0 [Note] Event Scheduler: Loaded 0 events
mysql_1  | 2020-01-16T09:56:11.005836Z 0 [Note] mysqld: ready for connections.
mysql_1  | Version: '5.7.29'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
```

##### ４. プロセスの確認

```sh:Terminal
$ docker-compose ps
              Name                            Command               State           Ports         
--------------------------------------------------------------------------------------------------
capistrano_sample_app_v1_app_1     /bin/sh -c rm -f /app/tmp/ ...   Up      0.0.0.0:8080->8080/tcp
capistrano_sample_app_v1_mysql_1   docker-entrypoint.sh mysql ...   Up      3306/tcp, 33060/tcp   
capistrano_sample_app_v1_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp      
```

##### ５.appのコンテナにアクセスできるか確認

```sh:Terminal
$ docker exec -it capistrano_sample_app_v1_app_1 /bin/bash 
root@a72845229f9c:/var/www/capistrano_sample_app_v1# 
root@a72845229f9c:/var/www/capistrano_sample_app_v1# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   2384   744 pts/0    Ss+  18:55   0:00 /bin/sh -c rm -f /app/tmp/pids/server.pid && bin/rails s -p 8080 -b '0.0.0.0'
root         7  0.2  6.3 1125704 130296 pts/0  Sl+  18:55   0:04 puma 3.12.2 (tcp://0.0.0.0:8080) [capistrano_sample_app_v1]
root        27  1.0  0.1   5748  3400 pts/1    Ss   19:28   0:00 /bin/bash
root        32  0.0  0.1   9388  2992 pts/1    R+   19:28   0:00 ps aux
root@a72845229f9c:/var/www/capistrano_sample_app_v1# ls
Capfile  Dockerfile  Gemfile  Gemfile.lock  README.md  Rakefile  app  bin  config  config.ru  db  docker-compose.yml  lib  log	package.json  spec  storage  tmp  vendor
root@a72845229f9c:/var/www/capistrano_sample_app_v1# bundle exec rails c
from /var/www/capistrano_sample_app_v1/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.5/lib/bootsnap.rb:22:in `setup': The 'disable_trace' method is not allowed with this Ruby version. current: 2.6.5, allowed version: < 2.5.0
Loading development environment (Rails 5.2.4.1)
irb(main):001:0> 
irb(main):002:0> Rails.env
=> "development"
irb(main):003:0> 
irb(main):004:0> quit
root@a72845229f9c:/var/www/capistrano_sample_app_v1# 
```

##### 6.mysqlのコンテナにアクセスできるか確認

```sh:Terminal
$ docker exec -it capistrano_sample_app_v1_mysql_1 /bin/bash 
root@38fa1f53821a:/# mysql -udeveloper -p capistrano_sample
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show tables
    -> ;
Empty set (0.00 sec)
```

ここまでで、開発のためのコンテナの設定は完了しています
尚、コンテナを実行しているホストマシンのワーキングコピーで変更があった場合、
dockerを `stop / start` することで変更反映できます


##### 7. Webアプリのコンテナをセットアップする

```sh:Terminal
$ docker exec -it capistrano_sample_app_v1_app_1 /bin/bash

# bundle exec rails db:migrate RAILS_ENV=development
```

##### 8.URLアクセス

[ローカルホスト](http://localhost:8080)のRailsにアクセスする

![スクリーンショット 2020-01-16 22.08.04.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/397c99c9-583e-bdca-e79c-139438b23945.png)


## ワーキングスペースの開発など

##### 1. bundle install

```sh:Terminal
$ bundle _2.1.4_ install --path vendor/bundle
```

##### Your Ruby version is 2.3.7, but your Gemfile specified 2.6.5となる場合

- 上記、bundle installをしないと、systemのruby versionを見るようです

##### [mysql2の原因でエラーが出た方はこちら](https://qiita.com/webmaster-patche/items/cb02e1574102388b649b#%E3%81%95%E3%82%89%E3%81%AB-dont-know-how-to-set-rpath-on-your-system-if-mysql-libraries-are-not-in-path-mysql2-may-not-load%E3%81%8C%E5%87%BA%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%8F%E3%81%A9%E3%81%84)参考に

```sh:Terminal
$ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl@1.1/lib --with-cppflags=-I/usr/local/opt/openssl@1.1/include"


$ bundle _2.1.4_ install --path vendor/bundle
[DEPRECATED] The `--path` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set path 'vendor/bundle'`, and stop using this flag
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
...途中省略...
Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
...途中省略...
Bundle complete! 37 Gemfile dependencies, 148 gems now installed.
Bundled gems are installed into `./vendor/bundle`
```

##### 2. 機能を追加してみる

```sh:Terminal
$ bundle exec rails generate scaffold v1::Event game:string description:string event_date:date join_limit:integer latitude:string longitude:string --skip-assets --skip-helper --skip-stylesheets --skip-view-specs --skip-jbuilder --skip-migration
      invoke  active_record
      create    app/models/v1/high_store.rb
      create    app/models/v1.rb
      invoke    rspec
      create      spec/models/v1/event_spec.rb
      invoke  resource_route
       route    namespace :v1 do
  resources :events
end
      invoke  scaffold_controller
      create    app/controllers/v1/events_controller.rb
      invoke    rspec
      create      spec/controllers/v1/events_controller_spec.rb
      create      spec/routing/v1/events_routing_spec.rb
      invoke      rspec
      create        spec/requests/v1/events_spec.rb

$ rm app/models/v1.rb
```

## リポジトリ

* 本記事のリポジトリは「[webmaster-patche/capistrano_sample_app_v1](https://github.com/webmaster-patche/capistrano_sample_app_v1)」にて公開しております