原因 1
docker-compose run
ではなく、 docker-compose exec
を使います:
console
1docker-compose up -d
2docker-compose exec wordpress bash
docker-compose run
は wordpress のサービスを作り直してしまう上、
実行コマンドを bash にしてしまいます
[WordPress イメージ](ENTRYPOINT ["docker-entrypoint.sh"])の ENTRYPOINT
に指定されている docker-entrypoint.sh では
コマンドが apache2
から始まっているか、php-fpm
のときしか
wp-config.php
を作成しません:
bash
1if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
2 ...
3 awk '
4 /^/*.*stop editing.**/$/ && c == 0 {
5 c = 1
6 system("cat")
7 if (ENVIRON["WORDPRESS_CONFIG_EXTRA"]) {
8 print "// WORDPRESS_CONFIG_EXTRA"
9 print ENVIRON["WORDPRESS_CONFIG_EXTRA"] "\n"
10 }
11 }
12 { print }
13 ' wp-config-sample.php > wp-config.php <<'EOPHP'
wp-config.php
が存在しないことが原因で wp-load.php で die() していました
原因 2
docker-compose.yml
を次のように修正します:
yaml
1services:
2 wordpress:
3 ...
4 environment:
5 WORDPRESS_DB_HOST: "db:3306"
↓
yaml
1services:
2 wordpress:
3 ...
4 environment:
5 WORDPRESS_DB_HOST: db
設定が正しくないため、データベースへの接続に失敗していました
原因 3
docker-compose.yml
を次のように修正します:
yaml
1version: "2"
2services:
3 # ↓ CLI のサービスを追加します
4 cli:
5 command: core install --url=http://localhost:9000/ --title=WordPress --admin_user=admin --admin_password=p@ssW0rd --admin_email=example@google.com
6 image: wordpress:cli
7 depends_on:
8 - wordpress
9 volumes:
10 - web:/var/www/html
11 wordpress:
12 ports:
13 - "9000:80"
14 depends_on:
15 - db
16 environment:
17 WORDPRESS_DB_HOST: db
18 env_file: .env
19 image: wordpress:latest
20 volumes:
21 # ↓ ディレクトリーを CLI サービスと共有します
22 - web:/var/www/html
23 - ./:/var/batch/
24 db:
25 env_file: .env
26 image: mysql:5.7.27
27 volumes:
28 - db-data:/var/lib/mysql
29volumes:
30 # ↓ volume を追加します
31 web:
32 db-data:
33 driver: local
WordPress のインストールが完了していなかったため、
wp-settings.php
の wp_not_installed() で die() していました
実行結果
console
1$ docker-compose up -d
2Creating network "test-php_default" with the default driver
3Creating volume "test-php_web" with default driver
4Creating test-php_db_1 ... done
5Creating test-php_wordpress_1 ... done
6Creating test-php_cli_1 ... done
7$ docker logs -f test-php_cli_1
8sendmail: can't connect to remote host (127.0.0.1): Connection refused
9Success: WordPress installed successfully.
10Warning: Unable to create directory wp-content/uploads/2020/07. Is its parent directory writable by the server?
11$ docker-compose exec wordpress bash
12root@2c7b6fca6c89:/var/www/html# php /var/batch/wordpress_batch.php
13batch testobject(wpdb)#2 (61) {
14 ["show_errors"]=>
15 bool(false)
16 ["suppress_errors"]=>
17 bool(false)
18 ["last_error"]=>
19 string(0) ""
20 ["num_queries"]=>
21 int(36)
22 ["num_rows"]=>
23 int(0)
24 ["rows_affected"]=>
25 int(0)
26 ["insert_id"]=>
27 int(113)
28 ["last_query"]=>
29 string(80) "SELECT option_value FROM wp_options WHERE option_name = 'theme_switched' LIMIT 1"
30 ["last_result"]=>
31 array(0) {
32 }
33 ["result":protected]=>
34 object(mysqli_result)#638 (5) {
35 ["current_field"]=>
36 int(0)
37 ["field_count"]=>
38 int(1)
39 ["lengths"]=>
40 NULL
41 ["num_rows"]=>
42 int(0)
43 ["type"]=>
44 int(0)
45 }
46
47---略---
48
49 ["charset"]=>
50 string(7) "utf8mb4"
51 ["collate"]=>
52 string(22) "utf8mb4_unicode_520_ci"
53 ["dbuser":protected]=>
54 string(7) "wp_user"
55 ["dbpassword":protected]=>
56 string(8) "hogehoge"
57 ["dbname":protected]=>
58 string(9) "wordpress"
59 ["dbhost":protected]=>
60 string(2) "db"
61 ["dbh":protected]=>
62 object(mysqli)#3 (19) {
63 ["affected_rows"]=>
64 int(0)
65 ["client_info"]=>
66 string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $"
67 ["client_version"]=>
68 int(50012)
69 ["connect_errno"]=>
70 int(0)
71 ["connect_error"]=>
72 NULL
73 ["errno"]=>
74 int(0)
75 ["error"]=>
76 string(0) ""
77 ["error_list"]=>
78 array(0) {
79 }
80 ["field_count"]=>
81 int(1)
82 ["host_info"]=>
83 string(13) "db via TCP/IP"
84 ["info"]=>
85 NULL
86 ["insert_id"]=>
87 int(0)
88 ["server_info"]=>
89 string(6) "5.7.27"
90 ["server_version"]=>
91 int(50727)
92 ["stat"]=>
93 string(134) "Uptime: 218 Threads: 1 Questions: 678 Slow queries: 0 Opens: 116 Flush tables: 1 Open tables: 109 Queries per second avg: 3.110"
94 ["sqlstate"]=>
95 string(5) "00000"
96 ["protocol_version"]=>
97 int(10)
98 ["thread_id"]=>
99 int(4)
100 ["warning_count"]=>
101 int(0)
102 }
103 ["func_call"]=>
104 string(94) "$db->query("SELECT option_value FROM wp_options WHERE option_name = 'theme_switched' LIMIT 1")"
105 ["is_mysql"]=>
106 bool(true)
107 ["incompatible_modes":protected]=>
108 array(6) {
109 [0]=>
110 string(12) "NO_ZERO_DATE"
111 [1]=>
112 string(18) "ONLY_FULL_GROUP_BY"
113 [2]=>
114 string(19) "STRICT_TRANS_TABLES"
115 [3]=>
116 string(17) "STRICT_ALL_TABLES"
117 [4]=>
118 string(11) "TRADITIONAL"
119 [5]=>
120 string(4) "ANSI"
121 }
122 ["use_mysqli":"wpdb":private]=>
123 bool(true)
124 ["has_connected":"wpdb":private]=>
125 bool(true)
126 ["categories"]=>
127 string(13) "wp_categories"
128 ["post2cat"]=>
129 string(11) "wp_post2cat"
130 ["link2cat"]=>
131 string(11) "wp_link2cat"
132}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。