dockerでwordpress ワンパンで構築

2018年11月26日

docker swamを利用してすぐにサービスを開始できるようにしました。

docker swarmが有効になっていない場合は有効にするために以下のコマンドを実行します。

docker swarm init

yamlを作成するに当たって、以下のサイトを参考しました

https://qiita.com/takyam/items/e92e5a6ca1548cbd58db

https://hub.docker.com/_/wordpress/

https://hub.docker.com/_/mariadb/

永続化はされていないので注意してください。

apache版

version: '3.1'
services:  
  wordpress:
    image: wordpress
    restart: always
    ports: 
          - 5556:80
    depends_on:
            - mysql
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
  mysql:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: wordpress 
      MYSQL_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress

php-fpm版

version: '3.1'
services:
  web:
    image: nginx
    depends_on: 
          - wordpress
    restart: always
    ports: 
          - 5555:80
    environment:
      - PHP_APP_HOST=wordpress
    
    volumes:
      - link_data:/var/www/html
      - ./mysite.template:/etc/nginx/conf.d/mysite.template
    command: /bin/bash -c "envsubst '$$PHP_APP_HOST' < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
  
  wordpress:
    image: wordpress:fpm
    restart: always
    depends_on:
            - mysql
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
            - link_data:/var/www/html
  mysql:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: wordpress 
      MYSQL_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
volumes:
  link_data:

nginx設定ファイル

cat ./mysite.template
server {
	listen 80;
	server_name ;
	index index.php;
	root /var/www/html;
	location ~* /wp-config.php {
		deny all;
	}
	location ~ \.php$ {
		fastcgi_pass   ${PHP_APP_HOST}:9000;
		fastcgi_index  index.php;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}
	location @wordpress {
		fastcgi_pass ${PHP_APP_HOST}:9000;
		fastcgi_index index.php;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
		fastcgi_param SCRIPT_FILENAME $document_root/index.php;
		include fastcgi_params;
	}
	location / {
		try_files $uri $uri/ @wordpress;
	}
}

サービスを作成するには次のコマンドで実行できます。

docker stack deploy -c ./eginx.yml wordpress_enginx
Ignoring unsupported options: restart
Creating network wordpress_enginx_default
Creating service wordpress_enginx_web
Creating service wordpress_enginx_wordpress
Creating service wordpress_enginx_mysql

webブラウザでアクセスすると初期状態で起動しています。

abコマンドで見てみます

ab -n 10 -c 10 http://127.0.0.1:5555/
This is ApacheBench, Version 2.3 &amp;lt;$Revision: 1757674 $&amp;gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient).....done
Server Software:        nginx/1.15.6
Server Hostname:        127.0.0.1
Server Port:            5555
Document Path:          /
Document Length:        0 bytes
Concurrency Level:      10
Time taken for tests:   0.230 seconds
Complete requests:      10
Failed requests:        0
Non-2xx responses:      10
Total transferred:      3130 bytes
HTML transferred:       0 bytes
Requests per second:    43.51 [#/sec] (mean)
Time per request:       229.855 [ms] (mean)
Time per request:       22.985 [ms] (mean, across all concurrent requests)
Transfer rate:          13.30 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    4   1.2      4       6
Processing:   135  161  26.5    160     223
Waiting:      134  161  26.5    159     223
Total:        140  165  26.0    163     227
Percentage of the requests served within a certain time (ms)
  50%    163
  66%    169
  75%    175
  80%    181
  90%    227
  95%    227
  98%    227
  99%    227
 100%    227 (longest request)

nginxで動いてます。逆も然りで、apacheもymlを変えれば動きます。