WordpressをDocker-composeで動かそうと思って、色々まとめました。
ついでにMysqlではなくて、Mariadbを使ってます。

Dockerfileの用意

用意する Dockerfile は下記の2種類

  • mariadb
  • wordpress
# docker/mariadb/Dockerfile
FROM mariadb:10.0

RUN usermod -u 1000 mysql
RUN mkdir -p /var/run/mysqld
RUN chmod -R 777 /var/run/mysqld
# docker/wordpress/Dockerfile
FROM wordpress:4.5.2-fpm

ENV WORDPRESS_TAR_FILE wordpress-4.5.2-ja.tar.gz
ENV WORDPRESS_SHA1 bab94003a5d2285f6ae76407e7b1bbb75382c36e

RUN rm -fr /usr/src/wordpress \
&& curl -o ${WORDPRESS_TAR_FILE} -SL https://ja.wordpress.org/${WORDPRESS_TAR_FILE} \
&& echo "0ca6bba086c6a7efc025f12d49c729de ${WORDPRESS_TAR_FILE}" | md5sum -c \
&& tar -xzf ${WORDPRESS_TAR_FILE} -C /usr/src/ \
&& rm ${WORDPRESS_TAR_FILE} \
&& chown -R www-data:www-data /usr/src/wordpress

VOLUME /var/www/html

docker-compose.ymlを用意

# docker-compose.yml
version: '2'
services:
  nginx:
    image: nginx
    hostname: ${DOCKER_NGINX_HOSTNAME}
    ports:
      - "80:80"
    volumes_from:
      - wordpress
    volumes:
      - ./files/conf.d:/etc/nginx/conf.d:ro
      - ./files/wordpress/.htpasswd:/var/www/html/.htpasswd:ro
    depends_on:
      - wordpress
    links:
      - wordpress
  wordpress:
    build: ./docker/wordpress
    depends_on:
      - db
    links:
      - db
    volumes:
      - ./files/wordpress/wp-content/languages:/var/www/html/wp-content/languages
      - ./files/wordpress/wp-content/plugins:/var/www/html/wp-content/plugins
      - ./files/wordpress/wp-content/themes:/var/www/html/wp-content/themes
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_PASSWORD: root
      WORDPRESS_DB_USER: root
  db:
    build: ./docker/mariadb
    volumes:
      - ./.data/db/mysql:/var/lib/mysql
      - ./.data/conf.d:/etc/mysql/conf.d:ro
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: root
    restart: always

Nginxの設定

wordpress 用の nginx.conf ファイルを用意します。

# files/conf.d/wordpress.conf
fastcgi_cache_path      /tmp/cache levels=1:2 keys_zone=wpcache:10m max_size=50M inactive=30m;
server {
  listen 80;
  charset utf-8;
  root /var/www/html;
  index index.php;

  location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|svg)$ {
    root /var/www/html;
    break;
  }

  location / {
    if (!-e $request_filename) {
      rewrite ^ /index.php last;
      break;
    }
  }

  location ~* /wp-config.php {
    deny all;
  }


  location ~* /wp-login\.php|/wp-admin/((?!admin-ajax\.php).)*$ {
    auth_basic "Please enter your name and password";
    auth_basic_user_file  "/var/www/html/.htpasswd";
    fastcgi_pass wordpress:9000;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
  }

  location ~ \.php$ {
    fastcgi_pass wordpress:9000;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
    fastcgi_cache         wpcache;
        fastcgi_cache_key     "$scheme://$host$request_uri";
        fastcgi_cache_valid   200 10m;
        fastcgi_cache_valid   404 1m;
  }
}
$ docker-compose up -d

あとはブラウザで開くだけ

ここまでのファイルはこちらに
github