DockerComposeをdevelopmentで実行する

これまで、production環境で開発をしてきた。
Dockerを導入し、AWSでのデプロイを試みている。

しかしそれは、他の記事を参考に作っていた為、
意味もわからず、本番環境でやってきただけで、
必要な構成も理解していないので上手く進まなかった。

そこで、development環境に戻し、
まずはDockerの導入にフォーカスして理解度を高めていく。

***

#### 各ファイルの修正

```ruby:docker-compose.yml
services:
  local-redis:
    image: redis
    container_name:
      rails-redis
    container_name: rails-redis

  local-rails:
    build: .
@@ -15,9 +14,31 @@ services:
      - local-redis
    env_file:
      - env_file.env
    container_name:
      rails-rails
    container_name: rails-rails
    command:
      bundle exec rails s -b 0.0.0.0
      # bundle exec rails s -e production

    environment:
      RAILS_DATABASE_PASSWORD: blago18
      RAILS_DATABASE_HOST: db
      RAILS_DATABASE_USER: blago18
      RAILS_ENV: development
    volumes:
      - .:/var/www/footomo

  db:
    image: mysql
    volumes:
      - mysql-data:/var/lib/mysql
    # ports:
    # - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: MySQL8.0
      MYSQL_DATABASE: app_development
      MYSQL_USER: blago18
      MYSQL_PASSWORD: blago18
    command:
      - --default-authentication-plugin=mysql_native_password

volumes:
  mysql-data:
```

***

#### コンテナに入ってmigrationを実行

```
Kotaro18:footomo kotaro18$ docker-compose up --build
Creating network "footomo_default" with the default driver
Building local-rails
[+] Building 0.3s (16/16) FINISHED                                                                                                    
 => [internal] load build definition from Dockerfile                                                                             0.0s
 => => transferring dockerfile: 37B                                                                                              0.0s
 => [internal] load .dockerignore                                                                                                0.0s
 => => transferring context: 2B                                                                                                  0.0s
 => [internal] load metadata for docker.io/library/ruby:2.6.6                                                                    0.0s
 => [ 1/11] FROM docker.io/library/ruby:2.6.6                                                                                    0.0s
 => [internal] load build context                                                                                                0.1s
 => => transferring context: 24.44kB                                                                                             0.1s
 => CACHED [ 2/11] RUN curl -sL https://deb.nodesource.com/setup_11.x | bash -                                                   0.0s
 => CACHED [ 3/11] COPY ./ /var/www/footomo                                                                                      0.0s
 => CACHED [ 4/11] WORKDIR /var/www/footomo                                                                                      0.0s
 => CACHED [ 5/11] COPY Gemfile /var/www/footomo                                                                                 0.0s
 => CACHED [ 6/11] COPY Gemfile.lock /var/www/footomo                                                                            0.0s
 => CACHED [ 7/11] RUN apt-get update                                                                                            0.0s
 => CACHED [ 8/11] RUN apt-get upgrade -y                                                                                        0.0s
 => CACHED [ 9/11] RUN gem update bundler                                                                                        0.0s
 => CACHED [10/11] RUN apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev -y                                  0.0s
 => CACHED [11/11] RUN bundle install                                                                                            0.0s
 => exporting to image                                                                                                           0.0s
 => => exporting layers                                                                                                          0.0s
 => => writing image sha256:80be9ac03901063a087005abd6dda11a3649ae427b8362763ce3dac898f1c523                                     0.0s
 => => naming to docker.io/library/footomo_local-rails                                                                           0.0s
Successfully built 80be9ac03901063a087005abd6dda11a3649ae427b8362763ce3dac898f1c523
Creating rails-redis  ... done
Creating footomo_db_1 ... done
Creating rails-rails  ... done
Attaching to rails-redis, footomo_db_1, rails-rails
db_1           | 2021-04-07 23:35:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1           | 2021-04-07 23:35:44+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1           | 2021-04-07 23:35:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.
db_1           | 2021-04-07T23:35:45.026977Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.23) starting as process 1
db_1           | 2021-04-07T23:35:45.041398Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
rails-redis    | 1:C 07 Apr 2021 23:35:44.073 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
rails-redis    | 1:C 07 Apr 2021 23:35:44.074 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
rails-redis    | 1:C 07 Apr 2021 23:35:44.074 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
rails-redis    | 1:M 07 Apr 2021 23:35:44.105 * Running mode=standalone, port=6379.
rails-redis    | 1:M 07 Apr 2021 23:35:44.106 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
rails-redis    | 1:M 07 Apr 2021 23:35:44.106 # Server initialized
rails-redis    | 1:M 07 Apr 2021 23:35:44.112 * Ready to accept connections
db_1           | 2021-04-07T23:35:45.698289Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
db_1           | 2021-04-07T23:35:46.033480Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
db_1           | 2021-04-07T23:35:46.142287Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db_1           | 2021-04-07T23:35:46.142734Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
db_1           | 2021-04-07T23:35:46.148257Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1           | 2021-04-07T23:35:46.195505Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.23'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
```

ここで、初歩的なミスをしてしまいました。

一旦、Ctl+Cでキャンセルしてから、
同じターミナルでコンテナに入ろうとしたところ、
railsは既に立ち上がってますとのエラーが。

server.pidを削除したり、
他のターミナルで立ち上がってないかチェックしてました。

単純に、1つのターミナルでコンテナを立ち上げ、
もう1つのターミナルでコンテナに入ればいいだけでした。

```
Kotaro18:footomo kotaro18$ docker-compose exec local-rails bash
root@fc0180c62138:/var/www/footomo# 
```

そして、migrationを実行

```
Kotaro18:footomo kotaro18$ docker-compose exec local-rails bash
root@fc0180c62138:/var/www/footomo# rails db:migrate
== 20180217122153 CreateBoards: migrating =====================================
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- create_table(:boards, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0507s
== 20180217122153 CreateBoards: migrated (0.0511s) ============================

== 20180315233935 CreateComments: migrating ===================================
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- create_table(:comments, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.1153s
== 20180315233935 CreateComments: migrated (0.1230s) ==========================

== 20180324120737 CreateTags: migrating =======================================
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0002s
-- create_table(:tags, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0416s
== 20180324120737 CreateTags: migrated (0.0485s) ==============================

== 20180324120941 CreateBoardTagRelations: migrating ==========================
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0001s
-- adapter_name()
   -> 0.0000s
-- create_table(:board_tag_relations, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0912s
== 20180324120941 CreateBoardTagRelations: migrated (0.0963s) =================

== 20180506115954 CreateUsers: migrating ======================================
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- adapter_name()
   -> 0.0000s
-- create_table(:users, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0376s
-- add_index(:users, :name, {:unique=>true})
   -> 0.0656s
== 20180506115954 CreateUsers: migrated (0.1056s) =============================

== 20181128051946 AddBirthdayToUser: migrating ================================
-- add_column(:users, :birthday, :date, {})
   -> 0.0430s
== 20181128051946 AddBirthdayToUser: migrated (0.0452s) =======================
```

Dockerのコンテナに入り、
dbをmigrationできました。

引き続き、開発環境で
コンテナの適切な導入を実施していきます。