Help us understand the problem. What is going on with this article?

PHP7+Laravel5.4+docker環境構築

More than 1 year has passed since last update.

インストール

laravel の開発環境作りやったので、そのメモです
なんで mysql5.5 なのかと言うと、とあるレンタルサーバに合わせているからです

  • install docker
brew install docker
  • install composer
brew install composer
# すでに composer インストール済なら
brew upgrate composer
  • install Laravel
composer global require "laravel/installer"
export PATH="$HOME/.composer/vendor/bin:$PATH"
  • Laravelプロジェクトを生成
laravel new appname
  • diff確認しやすいように、とりあえず初回コミット
cd appname
git init
git add -A
git commit -m "First commit"
  • 起動してみる
php artisan serve
  • localhost:8000 にアクセスして画面表示されたらOK

docker環境構築

  • docker-compose.yml 書く
docker-compose.yml
version: '2'

services:
  datastore:
    image: busybox
    volumes:
     - /var/lib/mysql
  db:
    image: mysql:5.5
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
    ports:
      - "3306:3306"
    volumes_from:
      - datastore
  cache:
    image: redis:3.2.3-alpine
    ports:
      - "6379:6379"
  app:
    image: php:7.1.1-apache
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - db
      - cache
  • docker動かす
docker-compose up
  • しばらく待って  http://localhost へアクセス
  • あれ、見れない?
  • http://localhost/public へアクセス
  • 見れた。どうやら DocumentRoot がずれている
000-default.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/public
    <Directory "/var/www/html/public">
            AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  • docker-compose.yml でapacheの設定ファイルを上書き
docker-compose.yml
     volumes:
       - .:/var/www/html
+      - ./000-default.conf:/etc/apache2/sites-available/000-default.conf
     depends_on:
       - db
       - cache
  • docker-compose up を実行して、再度 http://localhost へアクセス
  • 見れた!!

認証機能入れてみる

php artisan make:auth
  • http://localhost/ へアクセスして右上の register をクリック
  • 見れない?
  • なんか rewrite されてなさげ。。。
  • 仕方ないので Dockerfile で有効にするようにしてみた 
Dockerfile
FROM php:7.1.1-apache
RUN a2enmod rewrite
  • Dockerfileからbuildするように修正
docker-compose.yml
   app:
-    image: php:7.1.1-apache
+    # image: php:7.1.1-apache
+    build: .
     ports:
       - "80:80"
     volumes:
  • 一回止めて docker-compose builddocker-compose up を実行
  • http://localhost/register へアクセス
  • 見れた!
  • フォーム入力してみた
  • 落ちた
  • データベース設定見直し
.env
- DB_HOST=127.0.0.1
+ DB_HOST=db
  DB_PORT=3306
- DB_DATABASE=homestead
- DB_USERNAME=homestead
- DB_PASSWORD=secret
+ DB_DATABASE=dbname
+ DB_USERNAME=root
+ DB_PASSWORD=

  BROADCAST_DRIVER=log
  CACHE_DRIVER=file
  SESSION_DRIVER=file
  QUEUE_DRIVER=sync

- REDIS_HOST=127.0.0.1
+ REDIS_HOST=cache
  REDIS_PASSWORD=null
  REDIS_PORT=6379
  • pdoもなさげだったので
Dockerfile
  FROM php:7.1.1-apache
+ RUN docker-php-ext-install pdo pdo_mysql
  RUN a2enmod rewrite
  • 1回止めて docker-compose builddocker-compose up を実行
  • db作成
mysql -uroot -h127.0.0.1

> CREATE DATABASE dbname DEFAULT CHARSET utf8;
> exit
  • migration
docker-compose exec app php artisan migrate

Migration table created successfully.

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`
  (`email`))

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
  • ここで mysql5.5 で泣く。
  • ググった結果、以下のように回避
config/database.php
             'database' => env('DB_DATABASE', 'forge'),
             'username' => env('DB_USERNAME', 'forge'),
             'password' => env('DB_PASSWORD', ''),
-            'charset' => 'utf8mb4',
-            'collation' => 'utf8mb4_unicode_ci',
+            'charset' => 'utf8',
+            'collation' => 'utf8_unicode_ci',
             'prefix' => '',
             'strict' => true,
             'engine' => null,
  • 一度db作り直し
> database drop dbname
> CREATE DATABASE dbname DEFAULT CHARSET utf8;
  • 再度、migrate!
docker-compose exec app php artisan migrate

Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
  • http://localhost/register へアクセスして入力してみた
  • 無事ユーザ登録成功!
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away