2018年7月最新 EC2 + Docker + LaraDock + Laravel-admin構築メモ


### EC2へのDockerインストール
```
$ sudo yum install -y docker git
$ sudo service docker start
```

### Dockerの設定
```
$ sudo usermod -a -G docker ec2-user
$ cat /etc/group | grep docker
docker:x:497:ec2-user
```

### Docker-Composeのインストール
```
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
```

### LaraDocのインストール
```
$ mkdir laravel
$ cd laravel
$ git clone https://github.com/LaraDock/laradock.git
$ cd laradock
$ cp env-example .env
$ vi .env
```

ここで一度リログイン

```
APP_CODE_PATH_HOST=../HogeProject
```
###### プロジェクト用ディレクトリが出来ているか確認
```
$ docker-compose up -d workspace
$ ls ../
```

```
HogeProject  laradock
```

### プロジェクト作成
```
$ docker exec --user=laradock -it laradock_workspace_1 /bin/bash
/var/www $ composer create-project --prefer-dist laravel/laravel HogeProject
/var/www $ exit
$ docker-compose stop
```

### MySQLの設定
```
$ vi mysql/my.cnf
```
###### 以下項目を追加
```
[mysqld]
default_authentication_plugin=mysql_native_password
```
###### .envとdatabase.phpの内容を合わせる
```
$ docker exec --user=laradock -it laradock_workspace_1 /bin/bash
/var/www $ vi config/database.php
```

```
'default' => env('DB_CONNECTION', 'mysql'),

    :
    :

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'mysql'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'dafault'),
    'username' => env('DB_USERNAME', 'dafault'),
    'password' => env('DB_PASSWORD', 'secret'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],
```

```
/var/www $ vi .env
```

```
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret
```

### MySQLの再構築
```
$ sudo rm -rf ~/.laradock/data/mysql
$ docker-compose build mysql
$ docker-compose up -d nginx mysql
$ docker exec -it laradock_mysql_1 mysql mysql -udefault -psecret
```

###### DBの再構築の確認
```
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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 databases;
+--------------------+
| Database           |
+--------------------+
| default            |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)

mysql>
```

### PostgreSQLの場合の設定
今回はGISを使用したいのでPostgres-PostGISコンテナの方を使用する。

```
docker-compose up -d postgres-postgis
```



```
docker exec -it laradock_postgres-postgis_1 psql -h localhost -U default
```



### Laravel-adminのインストール
```
$ docker-compose up -d nginx mysql
$ docker exec --user=laradock -it laradock_workspace_1 /bin/bash
/var/www $ composer require encore/laravel-admin
/var/www $ php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
/var/www $ php artisan admin:install
```

### 設定

このままだとエラーが出るので設定を変更します。

```
docker exec --user=laradock -it laradock_workspace_1 /bin/bash
```

```config/admin.php
    'upload' => [

        // Disk in `config/filesystem.php`.
        // 'disk' => 'admin',
        'disk' => 'local',

        // Image and file upload path under the disk above.
        'directory' => [
            'image' => 'images',
            'file'  => 'files',
        ],
    ],
```

```config/filesystems.php
    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

        'admin' => [
            'driver' => 'local',
            'root' => storage_path('uploads'),
            'visibility' => 'public',
            'url' => env('APP_URL').'/uploads',
        ],
    ],
```


### コピペテンプレート
```
docker exec -ti --env COLUMNS=`tput cols` --env LINES=`tput lines` --user=laradock -it laradock_workspace_1 /bin/bash
docker-compose logs -f caddy
```