


Dockerコンテナ間の連携を勉強するのにWordPressのサイトを構築してみました。
WordPressイメージも検索すると多数ありますが、今回はDocker StoreからOFFICIAL REPOSITORYのwordpressを使用しました。
https://store.docker.com/images/c14a56d6-07e4-464b-b71c-4b24dc7f1836?tab=description

使用したイメージはwordpress:4.7.3-php7.1-apacheです。
まずデータベースのMariaDBのコンテナを準備します。MariaDBについては下記の記事を参考にして下さい。
MariaDBコンテナの作成、起動です。取り敢えずDBはコンテナ内に保存します。
| 1 2 3 4 5 | $ docker run -d --name mariadb_database -e MYSQL_USER=wpadmin -e MYSQL_PASSWORD=wppass -e MYSQL_DATABASE=wp centos/mariadb-101-centos7 b96a470823f57166a238f4698b036a5503a63c7ccc3d4976d574c5de30c24451 $ docker ps CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                    NAMES b96a470823f5        centos/mariadb-101-centos7   "container-entrypo..."   10 seconds ago      Up 10 seconds       3306/tcp                 mariadb_database | 
wordpress:4.7.3-php7.1-apacheイメージのダウンロードです。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $ docker pull wordpress:4.7.3-php7.1-apache 4.7.3-php7.1-apache: Pulling from library/wordpress 6d827a3ef358: Pull complete 87fe8fbc743a: Pull complete f6d1a8d304ab: Pull complete caf3547d9b73: Pull complete 1004db2760ff: Pull complete 66e2d66a547e: Pull complete bbfaa62c234a: Pull complete 19ce8807f4d1: Pull complete ccf3ec7b3529: Pull complete 7b0fb921474a: Pull complete 8cde875cb7f5: Pull complete 2e8b672a5081: Pull complete c1b547802173: Pull complete 8c6fe321b3eb: Pull complete e29361d07211: Pull complete 6e07012daf59: Pull complete b8aa821fcb53: Pull complete 2b65b0891c99: Pull complete Digest: sha256:87bc68d96d82777ceb405b0e20c312c9a94c94281a1414de661362418a0facc6 Status: Downloaded newer image for wordpress:4.7.3-php7.1-apache $ docker images REPOSITORY                   TAG                   IMAGE ID            CREATED             SIZE wordpress                    4.7.3-php7.1-apache   dff0f9e0c014        5 days ago          416 MB centos/mariadb-101-centos7   latest                f4cf7d5ba30d        2 weeks ago         407 MB | 
wordpressコンテナの作成、起動です。 --linkオプションで必要とするコンテナを指定します。
オプションについてはDocker wordpressの説明から引用します(手抜き)。
・-e WORDPRESS_DB_HOST=… (defaults to the IP and port of the linked mysql container)
・-e WORDPRESS_DB_USER=… (defaults to “root”)
・-e WORDPRESS_DB_PASSWORD=… (defaults to the value of the MYSQL_ROOT_PASSWORD environment variable from the linked mysql container)
・-e WORDPRESS_DB_NAME=… (defaults to “wordpress”)
| 1 2 3 4 5 6 7 | $ docker run --name wordpress --link mariadb_database:db -e WORDPRESS_DB_HOST=db -e WORDPRESS_DB_USER=wpadmin -e WORDPRESS_DB_PASSWORD=wppass -e WORDPRESS_DB_NAME=wp -p 80:80 -d wordpress:4.7.3-php7.1-apache 1b05ddbb00f3a458bdaeac6c24b17706ff026502818c682c53cb6658d1961b5f $ docker ps -a CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES 1b05ddbb00f3        wordpress:4.7.3-php7.1-apache   "docker-entrypoint..."   9 seconds ago       Up 8 seconds        0.0.0.0:80->80/tcp       wordpress b96a470823f5        centos/mariadb-101-centos7      "container-entrypo..."   20 minutes ago      Up 20 minutes       3306/tcp                 mariadb_database | 
http://host_IPに接続すればWordPressセットアップ画面が表示されました。
セットアップも完了して、さらにWordPress 4.7.4にアップデートできました。
wordpressコンテナが数分で落ちるのはDBコンテナとの接続が出来ていないと思います。
ログを確認するとMySQL Connection Errorが表示されていました。
| 1 2 3 4 5 6 | $ docker logs wordpress WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html Warning: mysqli::__construct(): (HY000/2002): No route to host in - on line 22 MySQL Connection Error: (2002) No route to host | 
正常に起動できると下記のログが表示されました。
| 1 2 3 4 5 6 7 | $ docker logs wordpress WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message [Mon Apr 24 12:03:50.564177 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.1.4 configured -- resuming normal operations [Mon Apr 24 12:03:50.564213 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' | 
--linkの指定方法がよく分からなくて試行錯誤の上にやっと連携できました。
下記サイトを参考に参考にさせて頂きました。
Docker: コンテナの連結
http://qiita.com/t-yotsu/items/e0277d545b4ddeeb44d6
--linkフラグのフォームは”–link <コンテナ名 or コンテナID>:alias(link名のエイリアス)”である

