[Laravel学習]Dockerでかんたんに、Laravel開発環境を作る(れなかった話)(Part8)
以前から気にはなっていたDockerを使ってLaravelの開発環境を作って行きたいと思います。これまではVagrantを使って仮想サーバを作ったりしてたけど、容量が大きくなりすぎて、使うのが億劫になっていく。また限りあるノートPCの容量を食い尽くすということで、こまってました。
PCが修理から返ってきてちょうどいいと思うので、DockerでLaravel環境を作りたいと思います。
幸いにもLaradockというのを使うとかんたんにインストールできるようなので挑戦したいとおもいます。
[目次]
Dockerのインストール
laradoc
Dockerのインストール
Dockerのインストール自体はかんたんです。ここからダウンロードしてdmgを展開してアプリケーションフォルダに掘り込むだけ(いつもと同じ)です。
laradocインストール
$ mkdir laravel
$ cd laravel
$ git clone https://github.com/LaraDock/laradock.git
Agreeing to the Xcode/iOS license requires admin privileges, please run “sudo xcodebuild -license” and then retry this command.
Xcodeのadmin権限が必要とのこと
$ sudo xcodebuild -license
[Enter]を押すとライセンスが出てくる
[q]で閉じる
agree[Enter]で同意
$ git clone https://github.com/LaraDock/laradock.git
Cloning into 'laradock'...
remote: Counting objects: 6491, done.
remote: Total 6491 (delta 0), reused 0 (delta 0), pack-reused 6491
Receiving objects: 100% (6491/6491), 6.08 MiB | 462.00 KiB/s, done.
Resolving deltas: 100% (3423/3423), done.
次はうまくいきました。
$ cp env-example .env
$ vi docker-compose.yml
$ docker-compose up -d nginx mysql redis beanstalkd
本当はapacheを入れたかったけどnginx -> apacheで置き換えられなかったので、そのまま実行。
結構早く終わりました。(5分ぐらいとおもう)
[参考ページ]
ステータスを確認すると
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
laradock_applications_ /true Exit 0
1
laradock_beanstalkd_1 /usr/bin/beanstalkd Up 0.0.0.0:11300->11300/
tcp
laradock_mysql_1 docker-entrypoint.sh Up 0.0.0.0:3306->3306/tc
mysqld p
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 Up 9000/tcp
php-fpm
laradock_redis_1 docker-entrypoint.sh Up 0.0.0.0:6379->6379/tc
redis ... p
laradock_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp
.envを修正します。
## tacky2k add 2018.01.15(月) 11:35
DB_HOST=mysql
REDIS_HOST=redis
QUEUE_HOST=beanstalkd
はい404 NotFoundでました。
参考サイトにあるようにLaravelのプロジェクトを作成しないといけないっぽいので、作ります。
ワークスペース(コンテナ)へログインですが、あんまりコンテナの意味がわかってません。
$ docker exec -it laradock_workspace_1 /bin/bash
root@d30caee63cc7:/var/www# composer create-project laravel/laravel testapp
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Installing laravel/laravel (v5.5.28)
- Installing laravel/laravel (v5.5.28): Downloading (100%)
Created project in testapp
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 70 installs, 0 updates, 0 removals
- Installing symfony/thanks (v1.0.1): Downloading (100%)
- Installing hamcrest/hamcrest-php (v2.0.0): Downloading (100%)
- Installing mockery/mockery (1.0): Downloading (100%)
- Installing vlucas/phpdotenv (v2.4.0): Downloading (100%)
- Installing symfony/css-selector (v4.0.3): Downloading (100%)
- Installing tijsverkoyen/css-to-inline-styles (2.2.1): Downloading (100%)
- Installing symfony/polyfill-mbstring (v1.6.0): Downloading (100%)
- Installing symfony/var-dumper (v3.4.3): Downloading (100%)
- Installing symfony/routing (v3.4.3): Downloading (100%)
- Installing symfony/process (v3.4.3): Downloading (100%)
- Installing paragonie/random_compat (v2.0.11): Downloading (100%)
- Installing symfony/polyfill-php70 (v1.6.0): Downloading (100%)
- Installing symfony/http-foundation (v3.4.3): Downloading (100%)
- Installing symfony/event-dispatcher (v4.0.3): Downloading (100%)
- Installing psr/log (1.0.2): Downloading (100%)
- Installing symfony/debug (v3.4.3): Downloading (100%)
- Installing symfony/http-kernel (v3.4.3): Downloading (100%)
- Installing symfony/finder (v3.4.3): Downloading (100%)
- Installing symfony/console (v3.4.3): Downloading (100%)
- Installing doctrine/lexer (v1.0.1): Downloading (100%)
- Installing egulias/email-validator (2.1.3): Downloading (100%)
- Installing swiftmailer/swiftmailer (v6.0.2): Downloading (100%)
- Installing ramsey/uuid (3.7.2): Downloading (100%)
- Installing psr/simple-cache (1.0.0): Downloading (100%)
- Installing psr/container (1.0.0): Downloading (100%)
- Installing symfony/translation (v3.4.3): Downloading (100%)
- Installing nesbot/carbon (1.22.1): Downloading (100%)
- Installing mtdowling/cron-expression (v1.2.1): Downloading (100%)
- Installing monolog/monolog (1.23.0): Downloading (100%)
- Installing league/flysystem (1.0.41): Downloading (100%)
- Installing erusev/parsedown (1.6.4): Downloading (100%)
- Installing doctrine/inflector (v1.3.0): Downloading (100%)
- Installing laravel/framework (v5.5.28): Downloading (100%)
- Installing fideloper/proxy (3.3.4): 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%)
- Installing nikic/php-parser (v3.1.3): Downloading (100%)
- Installing psy/psysh (v0.8.17): Downloading (100%)
- Installing laravel/tinker (v1.0.3): Downloading (100%)
- Installing filp/whoops (2.1.14): Downloading (100%)
- Installing fzaninotto/faker (v1.7.1): Downloading (100%)
- Installing sebastian/version (2.0.1): Downloading (100%)
- Installing sebastian/resource-operations (1.0.0): Downloading (100%)
- Installing sebastian/recursion-context (3.0.0): Downloading (100%)
- Installing sebastian/object-reflector (1.1.1): Downloading (100%)
- Installing sebastian/object-enumerator (3.0.3): Downloading (100%)
- Installing sebastian/global-state (2.0.0): Downloading (100%)
- Installing sebastian/exporter (3.1.0): Downloading (100%)
- Installing sebastian/environment (3.1.0): Downloading (100%)
- Installing sebastian/diff (2.0.1): Downloading (100%)
- Installing sebastian/comparator (2.1.2): Downloading (100%)
- Installing doctrine/instantiator (1.1.0): Downloading (100%)
- Installing phpunit/php-text-template (1.2.1): Downloading (100%)
- Installing phpunit/phpunit-mock-objects (5.0.6): Downloading (100%)
- Installing phpunit/php-timer (1.0.9): Downloading (100%)
- Installing phpunit/php-file-iterator (1.4.5): Downloading (100%)
- Installing theseer/tokenizer (1.1.0): Downloading (100%)
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Downloading (100%)
- Installing phpunit/php-token-stream (2.0.2): Downloading (100%)
- Installing phpunit/php-code-coverage (5.3.0): Downloading (100%)
- Installing webmozart/assert (1.2.0): Downloading (100%)
- Installing phpdocumentor/reflection-common (1.0.1): Downloading (100%)
- Installing phpdocumentor/type-resolver (0.4.0): Downloading (100%)
- Installing phpdocumentor/reflection-docblock (4.2.0): Downloading (100%)
- Installing phpspec/prophecy (1.7.3): Downloading (100%)
- Installing phar-io/version (1.0.1): Downloading (100%)
- Installing phar-io/manifest (1.0.1): Downloading (100%)
- Installing myclabs/deep-copy (1.7.0): Downloading (100%)
- Installing phpunit/phpunit (6.5.5): Downloading (100%)
symfony/var-dumper suggests installing ext-symfony_debug ()
symfony/routing suggests installing doctrine/annotations (For using the annotation loader)
symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)
symfony/routing suggests installing symfony/dependency-injection (For loading routes from a service)
symfony/routing suggests installing symfony/expression-language (For using expression matching)
symfony/routing suggests installing symfony/yaml (For using the YAML loader)
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/http-kernel suggests installing symfony/browser-kit ()
symfony/http-kernel suggests installing symfony/config ()
symfony/http-kernel suggests installing symfony/dependency-injection ()
symfony/console suggests installing symfony/lock ()
ramsey/uuid suggests installing ircmaxell/random-lib (Provides RandomLib for use with the RandomLibAdapter)
ramsey/uuid suggests installing ext-libsodium (Provides the PECL libsodium extension for use with the SodiumRandomGenerator)
ramsey/uuid suggests installing ext-uuid (Provides the PECL UUID extension for use with the PeclUuidTimeGenerator and PeclUuidRandomGenerator)
ramsey/uuid suggests installing moontoast/math (Provides support for converting UUID to 128-bit integer (in string form).)
ramsey/uuid suggests installing ramsey/uuid-doctrine (Allows the use of Ramsey\Uuid\Uuid as Doctrine field type.)
ramsey/uuid suggests installing ramsey/uuid-console (A console application for generating UUIDs with ramsey/uuid)
symfony/translation suggests installing symfony/config ()
symfony/translation suggests installing symfony/yaml ()
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
league/flysystem suggests installing league/flysystem-aws-s3-v2 (Allows you to use S3 storage with AWS SDK v2)
league/flysystem suggests installing league/flysystem-aws-s3-v3 (Allows you to use S3 storage with AWS SDK v3)
league/flysystem suggests installing league/flysystem-azure (Allows you to use Windows Azure Blob storage)
league/flysystem suggests installing league/flysystem-cached-adapter (Flysystem adapter decorator for metadata caching)
league/flysystem suggests installing league/flysystem-eventable-filesystem (Allows you to use EventableFilesystem)
league/flysystem suggests installing league/flysystem-rackspace (Allows you to use Rackspace Cloud Files)
league/flysystem suggests installing league/flysystem-sftp (Allows you to use SFTP server storage via phpseclib)
league/flysystem suggests installing league/flysystem-webdav (Allows you to use WebDAV storage)
league/flysystem suggests installing league/flysystem-ziparchive (Allows you to use ZipArchive adapter)
league/flysystem suggests installing spatie/flysystem-dropbox (Allows you to use Dropbox storage)
league/flysystem suggests installing srmklive/flysystem-dropbox-v2 (Allows you to use Dropbox storage for PHP 5 applications)
laravel/framework suggests installing aws/aws-sdk-php (Required to use the SQS queue driver and SES mail driver (~3.0).)
laravel/framework suggests installing doctrine/dbal (Required to rename columns and drop SQLite columns (~2.5).)
laravel/framework suggests installing guzzlehttp/guzzle (Required to use the Mailgun and Mandrill mail drivers and the ping methods on schedules (~6.0).)
laravel/framework suggests installing league/flysystem-aws-s3-v3 (Required to use the Flysystem S3 driver (~1.0).)
laravel/framework suggests installing league/flysystem-cached-adapter (Required to use Flysystem caching (~1.0).)
laravel/framework suggests installing league/flysystem-rackspace (Required to use the Flysystem Rackspace driver (~1.0).)
laravel/framework suggests installing nexmo/client (Required to use the Nexmo transport (~1.0).)
laravel/framework suggests installing pda/pheanstalk (Required to use the beanstalk queue driver (~3.0).)
laravel/framework suggests installing predis/predis (Required to use the redis cache and queue drivers (~1.0).)
laravel/framework suggests installing pusher/pusher-php-server (Required to use the Pusher broadcast driver (~3.0).)
laravel/framework suggests installing symfony/dom-crawler (Required to use most of the crawler integration testing tools (~3.3).)
laravel/framework suggests installing symfony/psr-http-message-bridge (Required to psr7 bridging features (~1.0).)
psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)
psy/psysh suggests installing hoa/console (A pure PHP readline implementation. You'll want this if your PHP install doesn't already support readline or libedit.)
filp/whoops suggests installing whoops/soap (Formats errors as SOAP responses)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.5.5)
phpunit/phpunit suggests installing phpunit/php-invoker (^1.1)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Package manifest generated successfully.
> @php artisan key:generate
Application key [base64:nk41Z5EqidHp5qIBO8G926W3+jPmuVvHRZvst75QEKM=] set successfully.
root@d30caee63cc7:/var/www#
rootでやんなよ!みたいな警告は出ているけど、うまくいきました。
exitしてdocker-composeを停めます。
root@d30caee63cc7:/var/www# exit
exit
tacky2k@MacBook:~/homepage/laravel/laradock$ docker-compose stop
Stopping laradock_nginx_1 ... done
Stopping laradock_beanstalkd_1 ... done
Stopping laradock_php-fpm_1 ... done
Stopping laradock_workspace_1 ... done
Stopping laradock_mysql_1 ... done
Stopping laradock_redis_1 ... done
ここまで来て、色々問題が発生。
この状態でmysqlも動いてるしnginxも動いているんだけど、どうもlaravelとmysql8.0というバージョン都合が悪いっぽい(laravelとの相性と言うのは嘘かもしれないけど)。加えてnginxも普段使ってないので使い勝手が悪いからapache2に乗り換えを行いました。
#すでにしてあれば不要
$docker-compose stop
$docker-compose rm
#このコマンドでapache2が使えるようになった。
$docker-compose up -d apache2 mysql redis beanstalkde
docker-compose.ymlというlaradoc直下のファイルを以下のように書き換え.
volumes:とその次の行追加
### Applications Code Container #############################
applications:
image: tianon/true
volumes:
- ${APPLICATION}/tanba-events:/var/www
### Workspace Utilities Container ###########################
mysqlを8.0->5.7にバージョンダウン
これもルート(laradoc直下)の.envにかかれているものを変更(webでググると.envで管理されてない時代の情報が出てきて困った)
MYSQLでググって以下のように書き換えた
#MYSQL_VERSION=8.0
MYSQL_VERSION=5.7
MYSQL_DATABASE=tanba-events
MYSQL_USER=user
MYSQL_PASSWORD=secret
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=root
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d
MYSQL_VERSIONを5.7に。
MYSQL_DATABASE=tanba-events これはデータベース名(自分で設定。本番に合わせておけば良いと思います。)
MYSQL_USER=user (これは後々面倒なので、[user]がいいじゃないでしょうか?もちろん変えてもいい)
MYSQL_PASSWORD=secret (これもMYSQL_USERの説明と一緒)
次にapache2の設定
laradoc/apache2/Dockerfileを修正
#ubuntu16.04->centos-7へ変更
#FROM webdevops/apache:ubuntu-16.04
FROM webdevops/apache:centos-7
MAINTAINER Eric Pfeiffer <computerfr33k@users.noreply.github.com>
ARG PHP_UPSTREAM_CONTAINER=php-fpm
ARG PHP_UPSTREAM_PORT=9000
ARG PHP_UPSTREAM_TIMEOUT=60
ENV WEB_PHP_SOCKET=${PHP_UPSTREAM_CONTAINER}:${PHP_UPSTREAM_PORT}
#Documentrootを変更
ENV WEB_DOCUMENT_ROOT=/var/www/tanba-events
ENV WEB_PHP_TIMEOUT=${PHP_UPSTREAM_TIMEOUT}
EXPOSE 80 443
#ここは変更不要かも
WORKDIR /var/www/tanba-events
COPY vhost.conf /etc/apache2/sites-enabled/vhost.conf
ENTRYPOINT ["/opt/docker/bin/entrypoint.sh"]
CMD ["supervisord"]
これでDockerコンテナを再構築
$ docker-compose up -d apache2 mysql redis beanstalkd
しばらく時間がかかるけどうまくいくのではないだろうか。
apache2にログイン
$ docker exec -it laradock_apache2_1 /bin/bash
[root@3519f0a0178b tanba-events]# pwd
/var/www/tanba-events
[root@3519f0a0178b tanba-events]# ls
app composer.json database phpunit.xml routes vendor
artisan composer.lock index.php public storage webpack.mix.js
bootstrap config package.json resources tests yarn.lock
[root@3519f0a0178b tanba-events]# vi /etc/httpd/conf/httpd.conf
# DocumentRoot "/var/www/html"
DocumentRoot "/var/www/tanba-events"
# <Directory "/var/www/tanba-events">
<Directory "/var/www/html">
[root@3519f0a0178b tanba-events]# service httpd restart
[root@3519f0a0178b tanba-events]# exit
これで動くようになるはず。
dockerインストール https://qiita.com/kurkuru/items/fa7401a01c4d5dd98e4a
laradocインストール https://qiita.com/kurkuru/items/6ce0b9c4632b944347b8
https://qiita.com/meidaimae/items/1b5902e2e520ece34b9a
https://day-journal.com/memo/laravel-001/
http://laradock.io/documentation/#change-the-php-fpm-version
https://qiita.com/k-waragai/items/ad46d0875eb745705512
https://qiita.com/tifa2chan/items/e9aa408244687a63a0ae
ディスカッション
コメント一覧
まだ、コメントがありません