forget for get

覚えるために忘れる

docker-composeでLaravel環境構築(PHP/nginx/MySQL)

これを参考に

【超入門】20分でLaravel開発環境を爆速構築するDockerハンズオン

https://qiita.com/ucan-lab/items/56c9dc3cf2e6762672f4

ディレクトリ・ファイルを作成

ディレクトリ構成

laravel_docker/
  docker-compose.yml
  docker/
    php/
      Dockerfile
      php.ini
    nginx/
      default.conf
    mysql/
      Dockerfile
      my.cnf
  laravel/

ディレクトリ作成

mkdir docker
mkdir docker/php
mkdir docker/nginx
mkdir docker/mysql
mkdir laravel

vi docker-compose.yml

version: "3.9"
services:
  app:
    build: ./docker/php
    volumes:
      - ./laravel:/work
  web:
    image: nginx:1.20-alpine
    ports:
      - 8054:80
    volumes:
      - ./laravel:/work
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
    working_dir: /work
  db:
    build: ./docker/mysql
    volumes:
      - db-store:/var/lib/mysql

volumes:
  db-store:

vi docker/php/Dockerfile

FROM php:8.0-fpm-buster
SHELL ["/bin/bash", "-oeux", "pipefail", "-c"]

#プロキシ設定が必要な場合
#ENV http_proxy http://xxx:3128/
#ENV https_proxy http://xxx:3128/

ENV COMPOSER_ALLOW_SUPERUSER=1 \
  COMPOSER_HOME=/composer

COPY --from=composer:2.0 /usr/bin/composer /usr/bin/composer

RUN apt-get update && \
  apt-get -y install git unzip libzip-dev libicu-dev libonig-dev && \
  apt-get clean && \
  rm -rf /var/lib/apt/lists/* && \
  docker-php-ext-install intl pdo_mysql zip bcmath

COPY ./php.ini /usr/local/etc/php/php.ini

WORKDIR /work

vi docker/php/php.ini

zend.exception_ignore_args = off
expose_php = on
max_execution_time = 30
max_input_vars = 1000
upload_max_filesize = 64M
post_max_size = 128M
memory_limit = 256M
error_reporting = E_ALL
display_errors = on
display_startup_errors = on
log_errors = on
error_log = /dev/stderr
default_charset = UTF-8

[Date]
date.timezone = Asia/Tokyo

[mysqlnd]
mysqlnd.collect_memory_statistics = on

[Assertion]
zend.assertions = 1

[mbstring]
mbstring.language = Japanese

vi docker/nginx/default.conf

server {
    listen 80;
    server_name example.com;
    root /work/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

vi docker/mysql/Dockerfile

FROM mysql/mysql-server:8.0

ENV MYSQL_DATABASE=laravel_local \
  MYSQL_USER=phper \
  MYSQL_PASSWORD=secret \
  MYSQL_ROOT_PASSWORD=secret \
  TZ=Asia/Tokyo

COPY ./my.cnf /etc/my.cnf
RUN chmod 644 /etc/my.cnf

vi docker/mysql/my.cnf

[mysqld]
# default
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

# character set / collation
character_set_server = utf8mb4
collation_server = utf8mb4_ja_0900_as_cs_ks

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# Error Log
log-error = mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file = mysql-slow.log
long_query_time = 1.0
log_queries_not_using_indexes = 0

# General Log
general_log = 1
general_log_file = mysql-general.log

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

環境構築

docker-compose up -d --build
docker exec -it laravel_docker-app-1 composer create-project laravel/laravel .

ブラウザからアクセスしてトップページが表示できる。

 

.envのDB設定を書き換え

vi laravel/.env

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=phper
DB_PASSWORD=secret

マイグレーション

docker exec -it laravel_docker-app-1 php artisan migrate

環境を破棄するには

docker-compose down --rmi all --volumes --remove-orphans