Vagrant + centos + docker + laravel (nginx + php-fpm + mysql + redis)

Vagrant CentOS に docker をインストールし開発環境の laravel を動かすところまで。

Nginx
PHP-FPM
MySQL
Redis

[OS]

hostsファイルに追記(例)
----
192.168.33.10 local.laravel-sample.jp
192.168.33.20 local-api.laravel-sample.jp
----

[Git Bash]

$ mkdir docker-test
$ cd docker-test
$ vagrant init centos/7
$ vim Vagrantfile
----
# config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "private_network", ip: "192.168.33.10"
----
$ vagrant up
$ vagrant ssh

■dockerのインストール

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install -y docker-ce docker-ce-cli containerd.io
$ sudo docker -v
$ sudo systemctl start docker
$ sudo systemctl enable docker

■dockerグループにvagrantユーザを追加

$ id $USER
$ sudo usermod -aG docker $USER
$ id $USER

■docker-composeのインストール

最新バージョン確認

https://github.com/docker/compose/releases

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose -v
$ exit
再度ログイン
$ vagrant ssh

VM内に作るdockerの最終的なディレクトリ構成
----
laravel-sample/
      ├─ docker-compose.yml
      ├─ nginx/
      │  └─ default.conf
      ├─ php-fpm/
      │  └─ Dockerfile
      ├─ mysql/
      │  └─ data/
      └─ source/
         └─ app/
         └─ bootstrap/
         └─ ...etc
----

$ mkdir laravel-sample
$ cd laravel-sample
$ sudo yum install vim
$ vim docker-compose.yml
----
version: '3'
services:

nginx:
image: nginx
ports:
- 80:80
----

$ docker-compose up -d
$ docker ps -a

ブラウザでアクセス
⇒ http://192.168.33.10

■nginxの設定ファイルを修正

$ mkdir nginx
$ vim nginx/default.conf
----
server {
listen 80;
server_name local.laravel-sample.jp; # 適宜

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
----

$ vim docker-compose.yml
----
version: '3'
services:

nginx:
image: nginx:
ports:
- 80:80
volumes:
- ./source:/usr/share/nginx/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
----

$ mkdir source
$ vim source/index.html
----
123
----

コンテナの更新
$ docker-compose up -d

これで http://192.168.33.10 にアクセスします。

■php-fpm コンテナ作成

$ mkdir php-fpm
$ vim php-fpm/Dockerfile
----
FROM php:8.0-fpm
----

$ vim docker-compose.yml
----
version: '3'
services:

nginx:
image: nginx
ports:
- 80:80
volumes:
- ./source:/usr/share/nginx/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf

php-fpm:
build: ./php-fpm
volumes:
- ./source:/var/www/html
----

$ docker-compose up -d --build
$ docker ps -a
$ docker exec -it laravel-sample_php-fpm_1 bash

[コンテナ]
sourceファイルがマウントされていることを確認
# ls -la
# exit

$ rm source/index.html
$ vim source/index.php
----
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| laravel_sample_db |
+--------------------+
2 rows in set (0.01 sec)

$ vim source/.env
----
DB_HOST=mysql
-DB_HOST=127.0.0.1
+DB_HOST=mysql
DB_DATABASE=laravel_sample_db # 追記
DB_USERNAME=laravel_sample_user # 追記
DB_PASSWORD=password # 追記
----

$ docker exec -it laravel-sample_php-fpm_1 bash

[コンテナ]

# php artisan migrate
# exit

■MySQLのデータ永続化(対応しないとテーブル自体が消えます)

$ vim docker-compose.yml
----
version: '3'
services:

nginx:
image: nginx
ports:
- 80:80
volumes:
- ./source:/usr/share/nginx/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf

php-fpm:
build: ./php-fpm
volumes:
- ./source:/var/www/html

mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel_sample_db
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: laravel_sample_user
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
ports:
- 3306:3306
volumes:
- ./mysql/data:/var/lib/mysql
----

$ docker-compose down
$ docker-compose up -d
$ ls mysql/data
⇒mysqlのデータが保管されている

■Redisの設定

$ vim docker-compose.yml
----
version: '3'
services:

nginx:
image: nginx
ports:
- 80:80
volumes:
- ./source:/usr/share/nginx/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf

php-fpm:
build: ./php-fpm
volumes:
- ./source:/var/www/html

mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel_sample_db
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: laravel_sample_user
MYSQL_PASSWORD: password
TZ: Asia/Tokyo
ports:
- 3306:3306
volumes:
- ./mysql/data:/var/lib/mysql

redis:
image: redis
ports:
- 6379:6379
----

$ docker-compose stop
$ docker-compose up -d
$ docker exec -it laravel-sample_php-fpm_1 bash

[コンテナ]

# composer require predis/predis
# exit

$ vim source/.env
----
-CACHE_DRIVER=file
+CACHE_DRIVER=redis

-SESSION_DRIVER=file
+SESSION_DRIVER=redis

-REDIS_HOST=127.0.0.1
+REDIS_HOST=redis
----

以上で、Vagrant 上の docker に、Nginx + PHP-FPM + MySQL + Redis の laravel の開発環境を用意できました。


投稿日

カテゴリー:

, ,

投稿者:

タグ: