Laradock on Docker of Windows

Laradoc による Laravel 開発環境を Docker of Windows で構築する。

Laravel を試すために、Laradoc で開発環境を整えてみる。
Docker も勉強中なので、手探りしながらやってみる。

# PC環境

Docker, git がインストール済みの前提

- CPU i5, Memory 8G
- Windows 10 Pro
- Docker of Windows
- git


# Laradock 概要

[Laravel](https://laravel.com/) の Docker 開発環境
[Laradocドキュメント](http://laradock.io/)

※ Laravel には、Homestead という vagrantの公式開発環境がある。

Laradoc で、サポートされているソフトウェア

- Database Engines: MySQL - MariaDB - Percona - MongoDB - Neo4j - RethinkDB - MSSQL - 
PostgreSQL - Postgres-PostGIS.
- Database Management: PhpMyAdmin - Adminer - PgAdmin
- Cache Engines: Redis - Memcached - Aerospike
- PHP Servers: NGINX - Apache2 - Caddy
- PHP Compilers: PHP FPM - HHVM
- Message Queueing: Beanstalkd - RabbitMQ - PHP Worker
- Queueing Management: Beanstalkd Console - RabbitMQ Console
- Random Tools: HAProxy - Certbot - Blackfire - Selenium - Jenkins - ElasticSearch - Kibana  Mailhog - Minio - Varnish - Swoole - Laravel Echo…

これらから必要なソフトを選択して、環境構築できる。

Workspace は、Laravel の設定などを行う環境

- Workspace Image Tools PHP CLI - Composer - Git - Linuxbrew - Node - V8JS - Gulp - SQLite - xDebug - Envoy - Deployer - Vim - Yarn - SOAP - Drush…




# 手順

## Laradoc の初期導入

- ワークディレクトリ作成
- Laradoc クローンして、laradoc に cd
- env-example から .env 作成
- docker-composeで workspace を起動
- docker-compose ps で、起動状態を確認

```
> git clone https://github.com/laradock/laradock
Cloning into 'laradock'...
remote: Counting objects: 5124, done.
remote: Total 5124 (delta 0), reused 0 (delta 0), pack-reused 5124
Receiving objects: 100% (5124/5124), 5.41 MiB | 335.00 KiB/s, done.
Resolving deltas: 100% (2650/2650), done.

> cd laradock

> cp env-example .env

> docker-compose up -d workspace
Creating network "laradock_default" with the default driver
Creating network "laradock_frontend" with driver "bridge"
Creating network "laradock_backend" with driver "bridge"
Creating laradock_applications_1
Creating laradock_workspace_1

> docker-compose ps
         Name                Command      State           Ports
-----------------------------------------------------------------------
laradock_applications_1   /true           Exit 0
laradock_workspace_1      /sbin/my_init   Up       0.0.0.0:2222->22/tcp

```

## Laravel プロジェクトを作成

- workspace に接続
- プロジェクト larapj1 を作成（プロジェクト名は任意）<br>Do not run Composer に、びっくりしますが、しばらく待つと処理が進みます。
- workspace を抜けて、止めます。


```
> docker exec -it laradock_workspace_1 bash
root@493f7944504f:/var/www#

root@493f7944504f:/var/www# composer create-project laravel/laravel larapj1
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Installing laravel/laravel (v5.4.23)
  - Installing laravel/laravel (v5.4.23): Downloading (100%)
Created project in larapj1
> php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 59 installs, 0 updates, 0 removals
  - Installing symfony/css-selector (v3.3.2): Downloading (100%)
  - Installing tijsverkoyen/css-to-inline-styles (2.2.0): Downloading (100%)
  - Installing symfony/polyfill-mbstring (v1.4.0): Downloading (100%)
  - Installing symfony/var-dumper (v3.3.2): Downloading (100%)
  - Installing jakub-onderka/php-console-color (0.1): Downloading (100%)
  - Installing jakub-onderka/php-console-highlighter (v0.3.2): Downloading (100%)
  - Installing dnoegel/php-xdg-base-dir (0.1): Downloading (100%)
...

Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
> php artisan key:generate
Application key [base64:VwRbtlHayxyJ3b8sIzZfHXRH6XikH/E8bNihDa/CiLk=] set successfully.
root@493f7944504f:/var/www#exit
exit
> docker-compose stop
Stopping laradock_workspace_1 ... done
>
```


- docker-compose.yml に作成したプロジェクトパスを反映

```docker-compose.yml
version: '2'

services:

### Applications Code Container #############################

    applications:
      image: tianon/true
      volumes:
        - ../larapj1:/var/www
```

- nginx, mysql 起動

```
> docker-compose up -d nginx mysql
Creating network "laradock_default" with the default driver
Creating network "laradock_frontend" with driver "bridge"
Creating network "laradock_backend" with driver "bridge"
Creating laradock_mysql_1 ...
Creating laradock_applications_1 ...
Creating laradock_mysql_1
Creating laradock_applications_1 ... done
Creating laradock_workspace_1 ...
Creating laradock_workspace_1 ... done
Creating laradock_php-fpm_1 ...
Creating laradock_php-fpm_1 ... done
Creating laradock_nginx_1 ...
Creating laradock_nginx_1 ... done

> docker-compose ps
         Name                        Command              State                     Ports
-----------------------------------------------------------------------------------------------------------
laradock_applications_1   /true                           Exit 0
laradock_mysql_1          docker-entrypoint.sh mysqld     Up       0.0.0.0:3306->3306/tcp
laradock_nginx_1          nginx                           Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1        docker-php-entrypoint php-fpm   Up       9000/tcp
laradock_workspace_1      /sbin/my_init                   Up       0.0.0.0:2222->22/tcp
```

- Laravel画面の確認 http://localhost

<img width="524" alt="2017-07-01_00h39_13.png" src="https://qiita-image-store.s3.amazonaws.com/0/100572/ebe0d1c1-4188-409b-61fa-b7d40997f67c.png">

- 停止

```
> docker-compose  down
Stopping laradock_nginx_1 ... done
Stopping laradock_php-fpm_1 ... done
Stopping laradock_workspace_1 ... done
Stopping laradock_mysql_1 ... done
Removing laradock_nginx_1 ... done
Removing laradock_php-fpm_1 ... done
Removing laradock_workspace_1 ... done
Removing laradock_mysql_1 ... done
Removing laradock_applications_1 ... done
Removing network laradock_default
Removing network laradock_frontend
Removing network laradock_backend
```

- apache2, maiadb 起動

```
> docker-compose up -d apache2 mariadb
Creating laradock_applications_1 ...
Creating laradock_mariadb_1 ...
Creating laradock_mariadb_1
Creating laradock_applications_1 ... done
Creating laradock_mariadb_1 ... done
Creating laradock_workspace_1 ... done
Creating laradock_php-fpm_1 ...
Creating laradock_php-fpm_1 ... done
Creating laradock_apache2_1 ...
Creating laradock_apache2_1 ... done
```

- Laravel 画面の確認<br>
web サーバが違っても、問題なく Laravel 画面が表示されます。

# phpmyadmin 起動

必要になったときに、phpmyadmin を追加で起動出来ます。

```
> docker-compose ps
         Name                        Command              State                     Ports
-----------------------------------------------------------------------------------------------------------
laradock_applications_1   /true                           Exit 0
laradock_mysql_1          docker-entrypoint.sh mysqld     Up       0.0.0.0:3306->3306/tcp
laradock_nginx_1          nginx                           Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1        docker-php-entrypoint php-fpm   Up       9000/tcp
laradock_workspace_1      /sbin/my_init                   Up       0.0.0.0:2222->22/tcp

> docker-compose up -d phpmyadmin
laradock_mysql_1 is up-to-date
Creating laradock_phpmyadmin_1 ...
Creating laradock_phpmyadmin_1 ... done

> docker-compose ps
         Name                        Command              State                     Ports
-----------------------------------------------------------------------------------------------------------
laradock_applications_1   /true                           Exit 0
laradock_mysql_1          docker-entrypoint.sh mysqld     Up       0.0.0.0:3306->3306/tcp
laradock_nginx_1          nginx                           Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1        docker-php-entrypoint php-fpm   Up       9000/tcp
laradock_phpmyadmin_1     /run.sh phpmyadmin              Up       0.0.0.0:8080->80/tcp
laradock_workspace_1      /sbin/my_init                   Up       0.0.0.0:2222->22/tcp
```

phpmyadmin の URL 
http://localhost:8080/

<img width="426" alt="2017-07-03_11h00_00.png" src="https://qiita-image-store.s3.amazonaws.com/0/100572/49226bf5-94e9-aba5-7e47-89b474e2e0a3.png">