Skip to content

Commit 8f5bcc1

Browse files
committedJan 7, 2021
Add new "wp-config-docker.php" that uses "getenv" for configuration
This new method of configuration allows us to stop modifying the user-supplied `wp-config.php` file on every container startup, and instead just let PHP itself read the values directly from the source. With this change, the "beta" tags are force-reenabled at the same version as the latest releases to allow users to test this before the next release of WordPress (where it is intended that this change will go live for all image variants).
1 parent f6444fd commit 8f5bcc1

24 files changed

+1113
-1333
lines changed
 

‎.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/*/**/Dockerfile linguist-generated
22
/*/**/docker-entrypoint.sh linguist-generated
3+
/*/**/wp-config-docker.php linguist-generated
34
/Dockerfile.template linguist-language=Dockerfile

‎Dockerfile.template

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ RUN set -ex; \
229229

230230
VOLUME /var/www/html
231231

232+
{{ if env.version == "beta" then ( -}}
233+
COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
234+
{{ ) else "" end -}}
232235
COPY docker-entrypoint.sh /usr/local/bin/
233236

234237
ENTRYPOINT ["docker-entrypoint.sh"]

‎apply-templates.sh

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ for version; do
5252

5353
if [ "$version" = 'cli' ]; then
5454
cp -a cli-entrypoint.sh "$dir/docker-entrypoint.sh"
55+
elif [ "$version" = 'beta' ]; then
56+
cp -a docker-entrypoint-ng.sh "$dir/docker-entrypoint.sh"
57+
cp -a wp-config-docker.php "$dir/"
5558
else
5659
cp -a docker-entrypoint.sh "$dir/"
5760
fi

‎beta/php7.3/apache/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/apache/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/apache/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm-alpine/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm-alpine/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm-alpine/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.3/fpm/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/apache/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/apache/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/apache/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm-alpine/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm-alpine/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm-alpine/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm/Dockerfile

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm/docker-entrypoint.sh

+28-222
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎beta/php7.4/fpm/wp-config-docker.php

+120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docker-entrypoint-ng.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
if [[ "$1" == apache2* ]] || [ "$1" = 'php-fpm' ]; then
5+
uid="$(id -u)"
6+
gid="$(id -g)"
7+
if [ "$uid" = '0' ]; then
8+
case "$1" in
9+
apache2*)
10+
user="${APACHE_RUN_USER:-www-data}"
11+
group="${APACHE_RUN_GROUP:-www-data}"
12+
13+
# strip off any '#' symbol ('#1000' is valid syntax for Apache)
14+
pound='#'
15+
user="${user#$pound}"
16+
group="${group#$pound}"
17+
;;
18+
*) # php-fpm
19+
user='www-data'
20+
group='www-data'
21+
;;
22+
esac
23+
else
24+
user="$uid"
25+
group="$gid"
26+
fi
27+
28+
if [ ! -e index.php ] && [ ! -e wp-includes/version.php ]; then
29+
# if the directory exists and WordPress doesn't appear to be installed AND the permissions of it are root:root, let's chown it (likely a Docker-created directory)
30+
if [ "$uid" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then
31+
chown "$user:$group" .
32+
fi
33+
34+
echo >&2 "WordPress not found in $PWD - copying now..."
35+
if [ -n "$(find -mindepth 1 -maxdepth 1 -not -name wp-content)" ]; then
36+
echo >&2 "WARNING: $PWD is not empty! (copying anyhow)"
37+
fi
38+
sourceTarArgs=(
39+
--create
40+
--file -
41+
--directory /usr/src/wordpress
42+
--owner "$user" --group "$group"
43+
)
44+
targetTarArgs=(
45+
--extract
46+
--file -
47+
)
48+
if [ "$uid" != '0' ]; then
49+
# avoid "tar: .: Cannot utime: Operation not permitted" and "tar: .: Cannot change mode to rwxr-xr-x: Operation not permitted"
50+
targetTarArgs+=( --no-overwrite-dir )
51+
fi
52+
# loop over "pluggable" content in the source, and if it already exists in the destination, skip it
53+
# https://github.com/docker-library/wordpress/issues/506 ("wp-content" persisted, "akismet" updated, WordPress container restarted/recreated, "akismet" downgraded)
54+
for contentDir in /usr/src/wordpress/wp-content/*/*/; do
55+
contentDir="${contentDir%/}"
56+
[ -d "$contentDir" ] || continue
57+
contentPath="${contentDir#/usr/src/wordpress/}" # "wp-content/plugins/akismet", etc.
58+
if [ -d "$PWD/$contentPath" ]; then
59+
echo >&2 "WARNING: '$PWD/$contentPath' exists! (not copying the WordPress version)"
60+
sourceTarArgs+=( --exclude "./$contentPath" )
61+
fi
62+
done
63+
tar "${sourceTarArgs[@]}" . | tar "${targetTarArgs[@]}"
64+
echo >&2 "Complete! WordPress has been successfully copied to $PWD"
65+
fi
66+
67+
wpEnvs=( "${!WORDPRESS_@}" )
68+
if [ ! -s wp-config.php ] && [ "${#wpEnvs[@]}" -gt 0 ]; then
69+
for wpConfigDocker in \
70+
wp-config-docker.php \
71+
/usr/src/wordpress/wp-config-docker.php \
72+
; do
73+
if [ -s "$wpConfigDocker" ]; then
74+
echo >&2 "No 'wp-config.php' found in $PWD, but 'WORDPRESS_...' variables supplied; copying '$wpConfigDocker' (${wpEnvs[*]})"
75+
# using "awk" to replace all instances of "put your unique phrase here" with a properly unique string (for AUTH_KEY and friends to have safe defaults if they aren't specified with environment variables)
76+
awk '
77+
/put your unique phrase here/ {
78+
cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\ -f1"
79+
cmd | getline str
80+
close(cmd)
81+
gsub("put your unique phrase here", str)
82+
}
83+
{ print }
84+
' "$wpConfigDocker" > wp-config.php
85+
break
86+
fi
87+
done
88+
fi
89+
fi
90+
91+
exec "$@"

‎generate-stackbrew-library.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ for version; do
8282

8383
if [ "$version" = 'beta' ] && latestVersion="$(jq -r '.latest.version // ""' versions.json)" && [ "$latestVersion" = "$fullVersion" ]; then
8484
# "beta" channel even with release, skip it
85-
continue
85+
: #continue # TODO once something newer than 5.6 is released, this should be restored (explicit "beta" for allowing pre-release testing of "wp-config-docker.php")
8686
fi
8787

8888
versionAliases=()

‎wp-config-docker.php

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* The base configuration for WordPress
4+
*
5+
* The wp-config.php creation script uses this file during the
6+
* installation. You don't have to use the web site, you can
7+
* copy this file to "wp-config.php" and fill in the values.
8+
*
9+
* This file contains the following configurations:
10+
*
11+
* * MySQL settings
12+
* * Secret keys
13+
* * Database table prefix
14+
* * ABSPATH
15+
*
16+
* This has been slightly modified (to read environment variables) for use in Docker.
17+
*
18+
* @link https://wordpress.org/support/article/editing-wp-config-php/
19+
*
20+
* @package WordPress
21+
*/
22+
23+
// IMPORTANT: this file needs to stay in-sync with https://github.com/WordPress/WordPress/blob/master/wp-config-sample.php
24+
// (it gets parsed by the upstream wizard in https://github.com/WordPress/WordPress/blob/f27cb65e1ef25d11b535695a660e7282b98eb742/wp-admin/setup-config.php#L356-L392)
25+
26+
// a helper function to lookup "env_FILE", "env", then fallback
27+
function getenv_docker($env, $default) {
28+
if ($fileEnv = getenv($env . '_FILE')) {
29+
return file_get_contents($fileEnv);
30+
}
31+
else if ($val = getenv($env)) {
32+
return $val;
33+
}
34+
else {
35+
return $default;
36+
}
37+
}
38+
39+
// ** MySQL settings - You can get this info from your web host ** //
40+
/** The name of the database for WordPress */
41+
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'database_name_here') );
42+
43+
/** MySQL database username */
44+
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'username_here') );
45+
46+
/** MySQL database password */
47+
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'password_here') );
48+
49+
/** MySQL hostname */
50+
define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') );
51+
52+
/** Database Charset to use in creating database tables. */
53+
define( 'DB_CHARSET', getenv_docker('WORDPRESS_DB_CHARSET', 'utf8') );
54+
55+
/** The Database Collate type. Don't change this if in doubt. */
56+
define( 'DB_COLLATE', getenv_docker('WORDPRESS_DB_COLLATE', '') );
57+
58+
/**#@+
59+
* Authentication Unique Keys and Salts.
60+
*
61+
* Change these to different unique phrases!
62+
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
63+
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
64+
*
65+
* @since 2.6.0
66+
*/
67+
define( 'AUTH_KEY', getenv_docker('WORDPRESS_AUTH_KEY', 'put your unique phrase here') );
68+
define( 'SECURE_AUTH_KEY', getenv_docker('WORDPRESS_SECURE_AUTH_KEY', 'put your unique phrase here') );
69+
define( 'LOGGED_IN_KEY', getenv_docker('WORDPRESS_LOGGED_IN_KEY', 'put your unique phrase here') );
70+
define( 'NONCE_KEY', getenv_docker('WORDPRESS_NONCE_KEY', 'put your unique phrase here') );
71+
define( 'AUTH_SALT', getenv_docker('WORDPRESS_AUTH_SALT', 'put your unique phrase here') );
72+
define( 'SECURE_AUTH_SALT', getenv_docker('WORDPRESS_SECURE_AUTH_SALT', 'put your unique phrase here') );
73+
define( 'LOGGED_IN_SALT', getenv_docker('WORDPRESS_LOGGED_IN_SALT', 'put your unique phrase here') );
74+
define( 'NONCE_SALT', getenv_docker('WORDPRESS_NONCE_SALT', 'put your unique phrase here') );
75+
// (See also https://wordpress.stackexchange.com/a/152905/199287)
76+
77+
/**#@-*/
78+
79+
/**
80+
* WordPress Database Table prefix.
81+
*
82+
* You can have multiple installations in one database if you give each
83+
* a unique prefix. Only numbers, letters, and underscores please!
84+
*/
85+
$table_prefix = getenv_docker('WORDPRESS_TABLE_PREFIX', 'wp_');
86+
87+
/**
88+
* For developers: WordPress debugging mode.
89+
*
90+
* Change this to true to enable the display of notices during development.
91+
* It is strongly recommended that plugin and theme developers use WP_DEBUG
92+
* in their development environments.
93+
*
94+
* For information on other constants that can be used for debugging,
95+
* visit the documentation.
96+
*
97+
* @link https://wordpress.org/support/article/debugging-in-wordpress/
98+
*/
99+
define( 'WP_DEBUG', !!getenv_docker('WORDPRESS_DEBUG', '') );
100+
101+
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
102+
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
103+
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
104+
$_SERVER['HTTPS'] = 'on';
105+
}
106+
// (we include this by default because reverse proxying is extremely common in container environments)
107+
108+
if ($configExtra = getenv_docker('WORDPRESS_CONFIG_EXTRA', '')) {
109+
eval($configExtra);
110+
}
111+
112+
/* That's all, stop editing! Happy publishing. */
113+
114+
/** Absolute path to the WordPress directory. */
115+
if ( ! defined( 'ABSPATH' ) ) {
116+
define( 'ABSPATH', __DIR__ . '/' );
117+
}
118+
119+
/** Sets up WordPress vars and included files. */
120+
require_once ABSPATH . 'wp-settings.php';

0 commit comments

Comments
 (0)
Please sign in to comment.