takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

Docker で古い Laravel の開発環境を作成する

4年前に Laravel4.2 で作った Web アプリケーションの開発環境を Docker で作り直したときの手順メモ

検証環境

Windows10 Home Edition
VirtualBox 5.2.22
Docker version 18.05.0-ce, build f150324
docker-machine version 0.14.0, build 89b8332
docker-compose version 1.20.1, build 5d8c71b

Laravel4.2 の環境について

4年前に作成したときは以下の構成でした
当時は DockerVagrant は使用しておらず、VirtualBox に直接 CentOS をインストールして構築していました

CentOS 6.5
PHP 5.5.16
Laravel 4.2.16
MySQL 5.6.26
memcached 1.4.18

今回以下の内容で Docker 上に再構築します

CentOS 6.10
PHP 5.5.38
Laravel 4.2.22
MySQL 5.6.42
memcached 1.5

各種設定ファイル

GitHub のこちらのリポジトリにアップしてあります github.com

イメージのビルド

$ git clone https://github.com/takaya030/docker-centos6-lamp.git la42
$ cd la42
$ docker-compose build base
$ docker-compose build

コンテナの起動

$ docker-compose up -d

動作確認

web ブラウザで http://192.168.99.100 を開いて以下の画面が表示されれば正常に動作しています

f:id:takaya030:20190101225818p:plain

http://192.168.99.100/phpinfo.php にアクセスすると phpinfo が表示されます

f:id:takaya030:20190101225858p:plain

Laravel 4.2 のインストール

一旦コンテナを停止します

$ docker-compose stop

docker-compose.yml と同じ位置に webapp フォルダを作成します

$ mkdir webapp

docker-compose.yml を以下の通りに変更します

--- a/docker-compose.yml  Tue Jan 01 16:58:55 2019
+++ b/docker-compose.yml  Tue Jan 01 17:28:15 2019
@@ -4,7 +4,7 @@
     build: ./base
     image: takaya030/php:5.5
     volumes:
-      - ./itworks:/webapp
+      - ./webapp:/webapp
   web:
     build: ./web
     image: takaya030/web:5.5

コンテナを再起動します

$ docker-compose up -d

ssh を使って、ユーザー名: docker , パスワード: tcuser , ポート番号: 2022manage コンテナにログインします

$ ssh -p 2022 docker@192.168.99.100

composerLaravel をインストールします

$ composer create-project laravel/laravel /webapp v4.2.11

動作確認

web ブラウザで http://192.168.99.100 を開いて Laravel の Welcome ページが表示されるか確認します

f:id:takaya030:20190101230342p:plain

Laravelの設定

環境設定

/webapp/bootstrap/start.php を下記の通りに変更します

--- a/webapp/bootstrap/start.php       2019-01-02 11:21:29.136767400 +0900
+++ b/webapp/bootstrap/start.php       2019-01-03 12:27:58.733542500 +0900
@@ -26,7 +26,7 @@

 $env = $app->detectEnvironment(array(

-       'local' => array('homestead'),
+       'local' => array('dev-manage','dev-web'),

 ));

データベースの設定

/webapp/app/config/local/database.php を下記の通りに変更します

--- a/webapp/app/config/local/database.php     2018-12-31 16:40:43.421769900 +0900
+++ b/webapp/app/config/local/database.php     2019-01-03 12:40:17.927538800 +0900
@@ -22,24 +22,13 @@

                'mysql' => array(
                        'driver'    => 'mysql',
-                       'host'      => 'localhost',
-                       'database'  => 'homestead',
-                       'username'  => 'homestead',
-                       'password'  => 'secret',
+                       'host'      => 'mysql',
+                       'database'  => 'master',
+                       'username'  => 'dbuser',
+                       'password'  => 'dbuser',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
-               ),
-
-               'pgsql' => array(
-                       'driver'   => 'pgsql',
-                       'host'     => 'localhost',
-                       'database' => 'homestead',
-                       'username' => 'homestead',
-                       'password' => 'secret',
-                       'charset'  => 'utf8',
-                       'prefix'   => '',
-                       'schema'   => 'public',
                ),

        ),

キャッシュの設定

以下の内容で /webapp/app/config/local/cache.php を作成します

<?php

return array(

    /*
   |--------------------------------------------------------------------------
   | Default Cache Driver
   |--------------------------------------------------------------------------
   |
   | This option controls the default cache "driver" that will be used when
   | using the Caching library. Of course, you may use other drivers any
   | time you wish. This is the default when another is not specified.
   |
   | Supported: "file", "database", "apc", "memcached", "redis", "array"
   |
   */

    'driver' => 'memcached',

    /*
   |--------------------------------------------------------------------------
   | Memcached Servers
   |--------------------------------------------------------------------------
   |
   | Now you may specify an array of your Memcached servers that should be
   | used when utilizing the Memcached cache driver. All of the servers
   | should contain a value for "host", "port", and "weight" options.
   |
   */

    'memcached' => array(

        array('host' => 'memcached', 'port' => 11211, 'weight' => 100),

    ),

);

セッションの設定

以下の内容で /webapp/app/config/local/session.php を作成します

<?php

return array(

    /*
   |--------------------------------------------------------------------------
   | Default Session Driver
   |--------------------------------------------------------------------------
   |
   | This option controls the default session "driver" that will be used on
   | requests. By default, we will use the lightweight native driver but
   | you may specify any of the other wonderful drivers provided here.
   |
   | Supported: "file", "cookie", "database", "apc",
   |            "memcached", "redis", "array"
   |
   */

    'driver' => 'memcached',

);

動作確認

manage コンテナにログインして以下の artisan コマンドが動作するか確認します

$ php artisan cache:clear
Application cache cleared!

$ php artisan migrate:install
Migration table created successfully.

phpMyAdmin について

web ブラウザで http://192.168.99.100:8080 にアクセスすると phpMyAdmin のログイン画面が表示されます
サーバ: mysql , ユーザ名: dbuser , パスワード: dbuser でログイン可能です

f:id:takaya030:20190103180856p:plain