4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker+LaravelでMySQLを使えるようになるまでのメモ

Last updated at Posted at 2019-08-29

環境

OS: macOS Mojave 10.14.6
Docker: version 19.03.1

Laradockのダウンロード

ディレクトリを作成後、Laradockをリポジトリから持ってくる

host
$ mkdir laradock_tutorial
$ cd laradock_tutorial
$ git clone https://github.com/Laradock/laradock.git
Cloning into 'laradock'...
remote: Enumerating objects: 9107, done.
remote: Total 9107 (delta 0), reused 0 (delta 0), pack-reused 9107
Receiving objects: 100% (9107/9107), 8.36 MiB | 4.38 MiB/s, done.
Resolving deltas: 100% (4891/4891), done.

MySQLの設定を変更

laradockディレクトリに移動、.env-exampleをコピーしてリネーム
今回はMySQLのバージョンは5.7, DB名はtest, ユーザー名はuser, パスワードはなしとする。

host
$ cd laradock
$ cp env-example .env
$ vi .env

# 240行目~
MYSQL_VERSION=5.7
MYSQL_DATABASE=test
MYSQL_USER=user
MYSQL_PASSWORD=

コンテナを初期化する

(かなり時間かかります)

host
$ docker-compose up -d nginx mysql workspace phpmyadmin

Laravelアプリケーションを作成

Workspaceコンテナに入ります。
Laravel5.7でsampleという名前のアプリケーションを作る(時間かかります)
途中で、Do not run Composer as root/super user! と出てくるが気にしない

host
$ docker-compose exec workspace bash
workspace(docker)
$ cd laradock
$ composer create-project "laravel/laravel" sample --prefer-dist "5.7.*"
Application key set successfully.

データベースの接続を設定する

コンテナ内でsampleアプリケーションが出来たのを確認してから、sampleディレクトリに移動

workspace(docker)
$ ls
laradock  sample
$ cd sample
$ vi .env

# 12行目~ ホストのDB設定と同じにすること。
DB_DATABASE=test
DB_USERNAME=user
DB_PASSWORD=

sampleアプリケーションとLaradockの関連付け

workspace(docker)
$ cd ../laradock
$ vi .env

# 7行目~
APP_CODE_PATH_HOST=../sample

# workspaceコンテナから抜ける
$ exit

DB作成用ファイルを編集

host
$ cd mysql/docker-entrypoint-initdb.d
$ cp createdb.sql.example createdb.sql
$ vi createdb.sql

# 新しく記入
CREATE DATABASE IF NOT EXISTS `test` COLLATE 'utf8_general_ci' ;
GRANT ALL ON `test`.* TO 'user'@'%' ;

DBの作成とユーザーの関連付け

host
$ cd ../..
$ docker-compose exec mysql bash

mysqlコンテナに入る。

mysql(docker)
$ mysql -u root -p < /docker-entrypoint-initdb.d/createdb.sql
Enter password: (rootと入力)

# 作成されたか確認
$ mysql -u user -p
Enter password: (そのままEnter)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               | ← testデータベースが作成されている。
+--------------------+

mysql> exit
Bye
$ exit

動作確認

sampleディレクトリに移動して、マイグレーションできるか確認。
これで動けばMySQLを使うことが出来ます。

host
$ cd ../sample/
$ php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table

最後にサーバーが起動できているかを確認します。

host
$ php artisan serve

http://localhost:8000 にアクセスして以下のページが表示されていれば成功です。スクリーンショット 2019-08-29 21.39.57.png

参考にさせていただいたもの

https://qiita.com/yanagikouta/items/dcd7ce16b7fb7353e086
https://techblog.scouter.co.jp/entry/2019/01/11/133508

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?