Skip to content

Commit 537363a

Browse files
authored
Merge pull request #193 from guice/wp/add-php71
Adding in php7.1 docker options.
2 parents 92af5d8 + dddde60 commit 537363a

File tree

7 files changed

+731
-0
lines changed

7 files changed

+731
-0
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ env:
88
- VARIANT=php7.0/apache
99
- VARIANT=php7.0/fpm
1010
- VARIANT=php7.0/fpm-alpine
11+
- VARIANT=php7.1/apache
12+
- VARIANT=php7.1/fpm
13+
- VARIANT=php7.1/fpm-alpine
1114

1215
install:
1316
- git clone https://github.com/docker-library/official-images.git ~/official-images

php7.1/apache/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
FROM php:7.1-apache
2+
3+
# install the PHP extensions we need
4+
RUN set -ex; \
5+
\
6+
apt-get update; \
7+
apt-get install -y \
8+
libjpeg-dev \
9+
libpng12-dev \
10+
; \
11+
rm -rf /var/lib/apt/lists/*; \
12+
\
13+
docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
14+
docker-php-ext-install gd mysqli opcache
15+
# TODO consider removing the *-dev deps and only keeping the necessary lib* packages
16+
17+
# set recommended PHP.ini settings
18+
# see https://secure.php.net/manual/en/opcache.installation.php
19+
RUN { \
20+
echo 'opcache.memory_consumption=128'; \
21+
echo 'opcache.interned_strings_buffer=8'; \
22+
echo 'opcache.max_accelerated_files=4000'; \
23+
echo 'opcache.revalidate_freq=2'; \
24+
echo 'opcache.fast_shutdown=1'; \
25+
echo 'opcache.enable_cli=1'; \
26+
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
27+
28+
RUN a2enmod rewrite expires
29+
30+
VOLUME /var/www/html
31+
32+
ENV WORDPRESS_VERSION 4.7
33+
ENV WORDPRESS_SHA1 1e14144c4db71421dc4ed22f94c3914dfc3b7020
34+
35+
RUN set -ex; \
36+
curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
37+
echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
38+
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
39+
tar -xzf wordpress.tar.gz -C /usr/src/; \
40+
rm wordpress.tar.gz; \
41+
chown -R www-data:www-data /usr/src/wordpress
42+
43+
COPY docker-entrypoint.sh /usr/local/bin/
44+
45+
ENTRYPOINT ["docker-entrypoint.sh"]
46+
CMD ["apache2-foreground"]

php7.1/apache/docker-entrypoint.sh

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# usage: file_env VAR [DEFAULT]
5+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
6+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
7+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
8+
file_env() {
9+
local var="$1"
10+
local fileVar="${var}_FILE"
11+
local def="${2:-}"
12+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
13+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
14+
exit 1
15+
fi
16+
local val="$def"
17+
if [ "${!var:-}" ]; then
18+
val="${!var}"
19+
elif [ "${!fileVar:-}" ]; then
20+
val="$(< "${!fileVar}")"
21+
fi
22+
export "$var"="$val"
23+
unset "$fileVar"
24+
}
25+
26+
if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
27+
file_env 'WORDPRESS_DB_HOST' 'mysql'
28+
# if we're linked to MySQL and thus have credentials already, let's use them
29+
file_env 'WORDPRESS_DB_USER' "${MYSQL_ENV_MYSQL_USER:-root}"
30+
if [ "$WORDPRESS_DB_USER" = 'root' ]; then
31+
file_env 'WORDPRESS_DB_PASSWORD' "${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}"
32+
else
33+
file_env 'WORDPRESS_DB_PASSWORD' "${MYSQL_ENV_MYSQL_PASSWORD:-}"
34+
fi
35+
file_env 'WORDPRESS_DB_NAME' "${MYSQL_ENV_MYSQL_DATABASE:-wordpress}"
36+
if [ -z "$WORDPRESS_DB_PASSWORD" ]; then
37+
echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable'
38+
echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?'
39+
echo >&2
40+
echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)'
41+
exit 1
42+
fi
43+
44+
if ! [ -e index.php -a -e wp-includes/version.php ]; then
45+
echo >&2 "WordPress not found in $(pwd) - copying now..."
46+
if [ "$(ls -A)" ]; then
47+
echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
48+
( set -x; ls -A; sleep 10 )
49+
fi
50+
tar cf - --one-file-system -C /usr/src/wordpress . | tar xf -
51+
echo >&2 "Complete! WordPress has been successfully copied to $(pwd)"
52+
if [ ! -e .htaccess ]; then
53+
# NOTE: The "Indexes" option is disabled in the php:apache base image
54+
cat > .htaccess <<-'EOF'
55+
# BEGIN WordPress
56+
<IfModule mod_rewrite.c>
57+
RewriteEngine On
58+
RewriteBase /
59+
RewriteRule ^index\.php$ - [L]
60+
RewriteCond %{REQUEST_FILENAME} !-f
61+
RewriteCond %{REQUEST_FILENAME} !-d
62+
RewriteRule . /index.php [L]
63+
</IfModule>
64+
# END WordPress
65+
EOF
66+
chown www-data:www-data .htaccess
67+
fi
68+
fi
69+
70+
# TODO handle WordPress upgrades magically in the same way, but only if wp-includes/version.php's $wp_version is less than /usr/src/wordpress/wp-includes/version.php's $wp_version
71+
72+
# version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
73+
# https://github.com/docker-library/wordpress/issues/116
74+
# https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
75+
sed -ri -e 's/\r\n|\r/\n/g' wp-config*
76+
77+
if [ ! -e wp-config.php ]; then
78+
awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
79+
// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
80+
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
81+
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
82+
$_SERVER['HTTPS'] = 'on';
83+
}
84+
85+
EOPHP
86+
chown www-data:www-data wp-config.php
87+
fi
88+
89+
# see http://stackoverflow.com/a/2705678/433558
90+
sed_escape_lhs() {
91+
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
92+
}
93+
sed_escape_rhs() {
94+
echo "$@" | sed -e 's/[\/&]/\\&/g'
95+
}
96+
php_escape() {
97+
php -r 'var_export(('$2') $argv[1]);' -- "$1"
98+
}
99+
set_config() {
100+
key="$1"
101+
value="$2"
102+
var_type="${3:-string}"
103+
start="(['\"])$(sed_escape_lhs "$key")\2\s*,"
104+
end="\);"
105+
if [ "${key:0:1}" = '$' ]; then
106+
start="^(\s*)$(sed_escape_lhs "$key")\s*="
107+
end=";"
108+
fi
109+
sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
110+
}
111+
112+
set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
113+
set_config 'DB_USER' "$WORDPRESS_DB_USER"
114+
set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
115+
set_config 'DB_NAME' "$WORDPRESS_DB_NAME"
116+
117+
# allow any of these "Authentication Unique Keys and Salts." to be specified via
118+
# environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
119+
UNIQUES=(
120+
AUTH_KEY
121+
SECURE_AUTH_KEY
122+
LOGGED_IN_KEY
123+
NONCE_KEY
124+
AUTH_SALT
125+
SECURE_AUTH_SALT
126+
LOGGED_IN_SALT
127+
NONCE_SALT
128+
)
129+
for unique in "${UNIQUES[@]}"; do
130+
uniqVar="WORDPRESS_$unique"
131+
file_env "$uniqVar"
132+
if [ "${!uniqVar}" ]; then
133+
set_config "$unique" "${!uniqVar}"
134+
else
135+
# if not specified, let's generate a random value
136+
current_set="$(sed -rn -e "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
137+
if [ "$current_set" = 'put your unique phrase here' ]; then
138+
set_config "$unique" "$(head -c1m /dev/urandom | sha1sum | cut -d' ' -f1)"
139+
fi
140+
fi
141+
done
142+
143+
file_env 'WORDPRESS_TABLE_PREFIX'
144+
if [ "$WORDPRESS_TABLE_PREFIX" ]; then
145+
set_config '$table_prefix' "$WORDPRESS_TABLE_PREFIX"
146+
fi
147+
148+
file_env 'WORDPRESS_DEBUG'
149+
if [ "$WORDPRESS_DEBUG" ]; then
150+
set_config 'WP_DEBUG' 1 boolean
151+
fi
152+
153+
TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
154+
<?php
155+
// database might not exist, so let's try creating it (just to be safe)
156+
157+
$stderr = fopen('php://stderr', 'w');
158+
159+
// https://codex.wordpress.org/Editing_wp-config.php#MySQL_Alternate_Port
160+
// "hostname:port"
161+
// https://codex.wordpress.org/Editing_wp-config.php#MySQL_Sockets_or_Pipes
162+
// "hostname:unix-socket-path"
163+
list($host, $socket) = explode(':', $argv[1], 2);
164+
$port = 0;
165+
if (is_numeric($socket)) {
166+
$port = (int) $socket;
167+
$socket = null;
168+
}
169+
170+
$maxTries = 10;
171+
do {
172+
$mysql = new mysqli($host, $argv[2], $argv[3], '', $port, $socket);
173+
if ($mysql->connect_error) {
174+
fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
175+
--$maxTries;
176+
if ($maxTries <= 0) {
177+
exit(1);
178+
}
179+
sleep(3);
180+
}
181+
} while ($mysql->connect_error);
182+
183+
if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) {
184+
fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");
185+
$mysql->close();
186+
exit(1);
187+
}
188+
189+
$mysql->close();
190+
EOPHP
191+
fi
192+
193+
exec "$@"

php7.1/fpm-alpine/Dockerfile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
FROM php:7.1-fpm-alpine
2+
3+
# docker-entrypoint.sh dependencies
4+
RUN apk add --no-cache \
5+
# in theory, docker-entrypoint.sh is POSIX-compliant, but priority is a working, consistent image
6+
bash \
7+
# BusyBox sed is not sufficient for some of our sed expressions
8+
sed
9+
10+
# install the PHP extensions we need
11+
RUN set -ex; \
12+
\
13+
apk add --no-cache --virtual .build-deps \
14+
libjpeg-turbo-dev \
15+
libpng-dev \
16+
; \
17+
\
18+
docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
19+
docker-php-ext-install gd mysqli opcache; \
20+
\
21+
runDeps="$( \
22+
scanelf --needed --nobanner --recursive \
23+
/usr/local/lib/php/extensions \
24+
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
25+
| sort -u \
26+
| xargs -r apk info --installed \
27+
| sort -u \
28+
)"; \
29+
apk add --virtual .wordpress-phpexts-rundeps $runDeps; \
30+
apk del .build-deps
31+
32+
# set recommended PHP.ini settings
33+
# see https://secure.php.net/manual/en/opcache.installation.php
34+
RUN { \
35+
echo 'opcache.memory_consumption=128'; \
36+
echo 'opcache.interned_strings_buffer=8'; \
37+
echo 'opcache.max_accelerated_files=4000'; \
38+
echo 'opcache.revalidate_freq=2'; \
39+
echo 'opcache.fast_shutdown=1'; \
40+
echo 'opcache.enable_cli=1'; \
41+
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
42+
43+
VOLUME /var/www/html
44+
45+
ENV WORDPRESS_VERSION 4.7
46+
ENV WORDPRESS_SHA1 1e14144c4db71421dc4ed22f94c3914dfc3b7020
47+
48+
RUN set -ex; \
49+
curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
50+
echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
51+
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
52+
tar -xzf wordpress.tar.gz -C /usr/src/; \
53+
rm wordpress.tar.gz; \
54+
chown -R www-data:www-data /usr/src/wordpress
55+
56+
COPY docker-entrypoint.sh /usr/local/bin/
57+
58+
ENTRYPOINT ["docker-entrypoint.sh"]
59+
CMD ["php-fpm"]

0 commit comments

Comments
 (0)