読者です 読者をやめる 読者になる 読者になる

takaya030の備忘録

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

Docker で nginx + php-fpm + Larvel5.4 の環境構築

こちらの記事を参考に Docker で Laravel5.4 の開発環境を構築したときの手順メモ
(フロントエンド関連ツールは省いています)
qiita.com

ディレクトリ構成

+---la54
|   |   docker-compose.yml
|   |   
|   +---composer
|   |       Dockerfile
|   |       
|   +---data
|   |       Dockerfile
|   |       
|   +---fpm
|   |       Dockerfile
|   |       
|   +---nginx
|   |       Dockerfile
|   |       server.conf
|   |       
|   +---phpcli
|           Dockerfile
|            
+---logs
|    
+---www

各種設定ファイル

la54/docker-compose.yml

version: "2"
services:
  data:
    build: ./data
    volumes:
      - ../:/data
  fpm:
    build: ./fpm
    volumes_from:
      - data
  nginx:
    build: ./nginx
    volumes_from:
      - data
    links:
      - fpm:fpm
    ports:
      - "80:80"
  composer:
    build: ./composer
    volumes_from:
      - data
  phpcli:
    build: ./phpcli
    volumes_from:
      - data

la54/composer/Dockerfile

FROM php:7.1.4-cli
LABEL maintainer "takaya030"

WORKDIR /tmp

RUN apt-get update -y && \
	apt-get install -y git unzip && \
	apt-get clean

RUN curl -sS https://getcomposer.org/installer | php && \
	mv composer.phar /usr/local/bin/composer && \
	composer self-update && \
	composer global require hirak/prestissimo

RUN mkdir -p /data/www
VOLUME ["/data"]
WORKDIR /data/www

ENTRYPOINT ["composer"]
CMD ["--help"]

la54/data/Dockerfile

FROM busybox
LABEL maintainer "takaya030"

RUN mkdir -p /data
VOLUME ["/data"]
CMD ["true"]

la54/fpm/Dockerfile

FROM php:7.1.4-fpm
LABEL maintainer "takaya030"

RUN apt-get update && \
  apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev libmcrypt-dev && \
  docker-php-ext-install pdo_mysql mysqli mbstring gd iconv mcrypt && \
  apt-get clean

RUN mkdir -p /data
VOLUME ["/data"]

la54/nginx/Dockerfile

FROM nginx:latest
LABEL maintainer "takaya030"

ADD server.conf /etc/nginx/conf.d/server.conf

RUN mkdir -p /data
VOLUME ["/data"]

la54/nginx/server.conf

server {
    listen 80 default;
    server_name _;
    root /data/www/public;
    index index.php index.html index.htm;
    charset utf-8;

    access_log /data/logs/access_log;
    error_log /data/logs/error.log warn;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ ^/index.php$ {
        fastcgi_pass fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

la54/phpcli/Dockerfile

FROM php:7.1.4-cli
LABEL maintainer "takaya030"

RUN apt-get update && \
  apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev libmcrypt-dev && \
  docker-php-ext-install pdo_mysql mysqli mbstring gd iconv mcrypt && \
  apt-get clean

RUN mkdir -p /data/www
VOLUME ["/data"]
WORKDIR /data/www

ENTRYPOINT ["php"]
CMD ["--version"]

使用方法

la54 ディレクトリに移動後、docker-compose を実行。

# nginx, php-fpm コンテナの起動
$ docker-compose up -d

# Laravel のインストール (最後のピリオドを忘れずに)
$ docker-compose run --rm composer create-project laravel/laravel .

# composer パッケージのアップデート
$ docker-compose run --rm composer update

# artisan コマンド
$ docker-compose run --rm phpcli artisan list