1
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 3 years have passed since last update.

Laravel開発環境をDockerで構築 ~備忘録~

Last updated at Posted at 2021-02-14

参考にした記事

最強のLaravel開発環境をDockerを使って構築する

上記の記事を参考にして環境構築を行いました。

nginx, php-fpm, MySQLの実行環境(LEMP)を構築して、Laravel開発環境を構築しました。と言ってもテンプレートをダウンロードしただけですが、、、、(笑)

##前提
・Docker
・Docker-compose
・Git
これらはダウンロード済み
それではレッツゴー!!

使い方

テンプレートのダウンロード

まず https://github.com/ucan-lab/docker-laravel ここからテンプレートを自分のリポジトリにダウンロードする
image.png
「Use this template」の部分(緑)をクリック!!

image.png
Codeをダウンロード!(自分はzipでダウンロード)

Laravelのバージョンを指定して新規作成

Makefileの一部を書き換える

docker-compose exec app composer create-project --prefer-dist laravel/laravel .

docker-compose exec app composer create-project --prefer-dist "laravel/laravel=6.*" .

次にコマンドを実行

$ make create project

>>> make[1]: ディレクトリ `C:/Users/xxxx/port' に入ります
    docker-compose build --no-cache --force-rm
    Building app
    Step 1/9 : FROM php:8.0-fpm-buster
    ---> 93375cae27f3
    Step 2/9 : LABEL maintainer="ucan-lab <yes@u-can.pro>"
    ---> Running in f69b8198df72
                   ~~省略~~
    Discovered Package: nunomaduro/collision
    Package manifest generated successfully.
    67 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!
    > @php artisan key:generate --ansi
    Application key set successfully.

laravelまで全部ダウンロードしてくれます!

既存のLaravelプロジェクトの環境を構築する

$ git clone git@github.com:[github名]/[プロジェクト名].git
$ git clone git@github.com:yossi0315-a/port.git →自分だったらこんな感じ

>>> Cloning into 'port'...
    git@github.com: Permission denied (publickey).
    fatal: Could not read from remote repository.

最初やると失敗。調べてみるとGitHubにssh接続ができなかったらしい。
ってことで調べると色々出てきた!
https://qiita.com/tnatsume00/items/e147662368d02e6416d2
自分はこの記事を参考にしました。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\xxxx/.ssh/id_rsa):
C:\Users\xxxx/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\xxxxx/.ssh/id_rsa.
Your public key has been saved in C:\Users\xxxxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx@DESKTOP-xxxxxxx
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|           .     |
|      .   . o    |
|       + + = .   |
|      o S * = .  |
|       = * B *   |
|      o = +.% +  |
|      .+ +oB O ..|
|       o++*=E +o |
+----[SHA256]-----+

とりあえずEnterキーを連打(3回くらい)(笑)
Overwrite (y/n)?の部分では返信を返すように!!
※因みにコマンドは全部コマンドプロンプトで実行しています。
キーをコピーしないといけないのだが,コマンドプロントだとコピーのコマンドが分からなかった為,git-bashを使って取得する

xxxxx@DESKTOP-xxxxx MINGW64 / $ clip < ~/.ssh/id_rsa.pub

>>> ※何も応答はありません

その後git.hubのsettingsでキーを入力して登録!(https://github.com/settings/keys)

$ ssh -T git@github.com

>>> Hi xxxxxxx! You've successfully authenticated, but GitHub does not provide shell access.

Hi!ってきたら成功!!

$ git clone git@github.com:xxxxxx/port.git

>>> Cloning into 'port'...
    remote: Enumerating objects: 28, done.
    remote: Counting objects: 100% (28/28), done.
    remote: Compressing objects: 100% (25/25), done.
    remote: Total 28 (delta 3), reused 10 (delta 0), pack-reused 0
    Receiving objects: 100% (28/28), 7.45 KiB | 305.00 KiB/s, done.
    Resolving deltas: 100% (3/3), done.
$ make init

>>>    Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'phper'@'172.22.0.5' (using password: YES) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

  at /work/backend/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'phper'@'172.22.0.5' (using password: YES)")
      /work/backend/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=db;port=3306;dbname=laravel_local", "phper", "secret", [])
      /work/backend/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details

エラーが発生。????って感じ(笑)
調べてみると.envの書き方やdocker-compose.ymlの書き方が違うっていうのが散見されたけど,
どれをやっても上手くいかず、、、。
でも諦めずに調べていたら,https://github.com/docker-library/mysql/issues/51 この記事を発見しどんどん読み進めていくと
「コンテナを削除して,再起動すると直ると」って記載されていたのでやってみると

$ docker-compose rm -v

>>>   Going to remove port_db_1, port_web_1, port_app_1
    Are you sure? [yN] y
    Removing port_db_1  ... done                                                                                            
    Removing port_web_1 ... done                                                                                            
    Removing port_app_1 ... done 

まずは削除

$ docker-compose up -d
>>>  WARNING: Found orphan containers (wordpress1) for this project. If you removed or renamed this service in your 
    compose file, you can run this command with the --remove-orphans flag to clean it up.
    Creating port_web_1 ... done                                                                                            
    Creating port_app_1 ... done                                                                                            
    Creating port_db_1  ... done

コンテナを再度立ち上げる
そしてしっかり立ち上がったのを確認して
エラーで止まってた部分から再度流すと、、、、

$ docker-compose exec app php artisan migrate:fresh --seed
>>>  Dropped all tables successfully.
    Migration table created successfully.
    Migrating: 2014_10_12_000000_create_users_table
    Migrated:  2014_10_12_000000_create_users_table (0.23 seconds)
    Migrating: 2014_10_12_100000_create_password_resets_table
    Migrated:  2014_10_12_100000_create_password_resets_table (0.13 seconds)
    Migrating: 2019_08_19_000000_create_failed_jobs_table
    Migrated:  2019_08_19_000000_create_failed_jobs_table (0.16 seconds)
    Database seeding completed successfully.

できた―!!!!
こんな単純なのに,だいぶハマりました。
これで構築完了!!

image.png

無事立ち上がりました!!

1
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
1
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?