takaya030の備忘録

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

Docker で nginx + php-fpm + Laravel5.3 の開発環境を作る

DockerLaravel 5.3 の開発環境を構築した際の手順メモ

検証環境

以下の環境で検証しました
VirtualBoxDocker はインストール済みの前提で話を進めます

Windows10 Home Edition
VirtualBox 5.0.16
docker 1.10.3
docker-machine 0.6.0
docker-compose 1.6.2

準備作業

Windowsのエディタで php のコードを編集した結果を直ちに反映させるため、Windows のフォルダを VirtualBox にマウントし、そこに Laravel をインストールします
マウント手順は下記の記事を参照してください
takaya030.hatenablog.com

C:\workspace をマウントした後、C:\workspace\laravel53 フォルダを作成します

Docker イメージの作成のための各種ファイル

ファイル構成

l53
│  docker-compose.yml
│
├─nginx
│      Dockerfile
│      server.conf
│
└─php7
        bashrc
        Dockerfile
        index.php
        mysupervisord.conf

各ファイルの内容

l53/docker-compose.yml
nginx: 
  build: ./nginx
  ports: 
    - "80:80"
    - "9000:9000"
  links: 
    - php7

php7: 
  build: ./php7
  hostname: laraweb
  ports: 
    - "2022:22"
  volumes: 
    - /workspace/laravel53:/webapp
l53/nginx/Dockerfile
FROM nginx:latest
MAINTAINER takaya030

ADD server.conf /etc/nginx/conf.d/server.conf
l53/nginx/server.conf
server {
    listen 80 default;
    server_name _;
    root /webapp/public;
    index index.php index.html index.htm;
    charset utf-8;

    access_log off;
    error_log off;

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

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

server {
    listen 9000 default;
    server_name _;
    root /var/www/html;
    index index.php index.html index.htm;
    charset utf-8;

    access_log off;
    error_log off;

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

    location ~ ^/index.php$ {
        fastcgi_pass l53_php7_1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}
l53/php7/bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# git prompt
if [ -f $HOME/.git-completion.bash ]; then
	source $HOME/.git-completion.bash
fi
if [ -f $HOME/.git-prompt.sh ]; then
	source $HOME/.git-prompt.sh
	export PS1='\[\033]0;$TITLEPREFIX:${PWD//[^[:ascii:]]/?}\007\]\n\[\033[32m\]\u@\h\[\033[35m\]: \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ '
fi

# User specific aliases and functions
alias ls='ls -F --color=auto'
alias ll='ls -la --color=auto'
alias la='ls -a --color=auto'
alias sl='ls -F --color=auto'
alias glog='git log --oneline --decorate --graph --branches --tags --remotes'
l53/php7/Dockerfile
#
# php7-fpm
#
# 2016-09-18
#   PHP 7.0.11

FROM php:7-fpm
MAINTAINER takaya030

# php
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

# sshd, scp, sudo, unzip, tar
RUN apt-get install -y openssh-server openssh-client sudo tar unzip supervisor && \
    apt-get clean

# gcc (for building git)
RUN apt-get install -y build-essential libssl-dev gettext curl expat openssl zlibc libcurl4-openssl-dev \
	&& apt-get clean

# git
RUN curl -O https://www.kernel.org/pub/software/scm/git/git-2.9.3.tar.gz && \
	tar xvzf git-2.9.3.tar.gz && \
	cd git-2.9.3 && \
	make configure && \
	./configure --prefix=/usr/local --with-curl --with-expat && \
	make all && \
	make install && \
	cd .. && \
	rm -r git-2.9.3 git-2.9.3.tar.gz

# for git prompt
RUN curl -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash > /etc/skel/.git-completion.bash && \
	curl -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh > /etc/skel/.git-prompt.sh 
ADD ./bashrc /etc/skel/.bashrc

# composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# phpunit
RUN curl -L https://phar.phpunit.de/phpunit.phar > /usr/local/bin/phpunit && \
	chmod +x /usr/local/bin/phpunit

# node, npm, gulp
RUN apt-get install -y nodejs npm && \
	npm cache clean && \
	npm install n -g && \
	n stable && \
	apt-get purge -y nodejs npm && \
	apt-get clean && \
	/usr/local/bin/npm install --global gulp-cli

# initialize for ssh
RUN sed -i '/pam_loginuid\.so/s/required/optional/' /etc/pam.d/sshd && \
	/usr/sbin/service ssh start && \
	/usr/sbin/service ssh stop

# create login user
RUN useradd -d /home/laravel -m -s /bin/bash laravel && \
	echo laravel:****laravel | chpasswd && \
	echo 'laravel ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# timezone
RUN cp -p /usr/share/zoneinfo/Japan /etc/localtime && \
	echo "date.timezone = Asia/Tokyo" > /usr/local/etc/php/conf.d/myphp.ini

# supervisor
COPY ./mysupervisord.conf /etc/supervisor/conf.d/

COPY index.php /var/www/html/

EXPOSE 22 9000

CMD ["/usr/bin/supervisord"]
l53/php7/index.php
<?php
	phpinfo();
l53/php7/mysupervisord.conf
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
nodaemon=true               ; (start in foreground if true;default false)

[program:sshd]
command=/usr/sbin/sshd -D

[program:php-fpm]
command=/usr/local/sbin/php-fpm

Docker イメージの作成

Docker ホストに上記のファイルを配置した後、以下のコマンドでイメージを作成します

$ cd l53
$ docker-compose build

コンテナの起動

$ cd l53
$ docker-compose up -d

ssh でログイン

$ ssh -p 2022 laravel@192.168.99.100

※パスワードは "****laravel"

Laravel 5.3 のインストール

ssh でログインしたコンテナ上で以下のコマンドを実行する

$ cd /webapp
$ composer create-project "laravel/laravel" --prefer-dist .

動作確認

WEBブラウザで http://192.168.99.100 にアクセスすると Laravel の welcome ページが表示されます
f:id:takaya030:20160920230537p:plain

また http://192.168.99.100:9000 にアクセスすると phpinfo が表示されます
f:id:takaya030:20160920230600p:plain

更新履歴

  • (2016-09-27) l53/php7/Dockerfilenode, npm, gulp のインストールを追加