Skip to content

Commit

Permalink
Docker Improvements 2021 Edition (#326)
Browse files Browse the repository at this point in the history
* redis config update to allow hostnames in config

* bitnami container compatibility update

* moved additional plugin installations to compose file

* fixed an issue setting the auto_prepend_file in the php.ini

* fixed an error with already running httpd

* improved mode selection logic

* fixed an issue where the stop function was not accepting all arguments

* added file comment

* improved container ip resolution

* fixed deprecated  variable

* utilizing bitnami functions and env variables

Signed-off-by: naxvog <naxvog@users.noreply.github.com>
  • Loading branch information
naxvog committed Jul 13, 2021
1 parent 7d5357e commit cce5741
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 76 deletions.
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ services:
- mariadb
- redis-master
environment:
MARIADB_HOST: mariadb
MARIADB_PORT_NUMBER: 3306
WORDPRESS_DATABASE_HOST: mariadb
WORDPRESS_DATABASE_PORT_NUMBER: 3306
WORDPRESS_DATABASE_USER: bn_wordpress
WORDPRESS_DATABASE_NAME: bitnami_wordpress
ALLOW_EMPTY_PASSWORD: "yes"
WORDPRESS_USERNAME: wordpress
WORDPRESS_PASSWORD: wordpress
WORDPRESS_BLOG_NAME: Redis Cache Dev
WORDPRESS_PLUGINS: query-monitor
redis-master:
build: docker/redis
ports:
Expand Down
1 change: 1 addition & 0 deletions docker/redis-sentinel/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ print_master () {
}

echo "port $SENTINEL_PORT" > $SENTINEL_CONFIGURATION_FILE
echo "sentinel resolve-hostnames yes" > $SENTINEL_CONFIGURATION_FILE

if [ "$ANNOUNCE_IP" ]; then
echo "sentinel announce-ip $ANNOUNCE_IP" >> $SENTINEL_CONFIGURATION_FILE
Expand Down
95 changes: 63 additions & 32 deletions docker/start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/usr/bin/env bash
#
# Utility script to manage the docker dev environments
#
# Documentation:
# https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development
#

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Expand All @@ -21,7 +27,7 @@ function start {

# Stops all containers
function stop {
compose down "${@:2}"
compose down "${@}"
}

# Modifies the auto-prepend-file
Expand Down Expand Up @@ -70,7 +76,25 @@ function apf {

# Retrieves the IP of a docker container using its name and optionally its index
function dcip {
echo $(compose exec --index="${2:-1}" "$1" hostname -i) | tr -d '\r'
declare -i counter=1
declare -i max_counter=25
container_name="$1_${2:-1}"
while [ $counter -le $max_counter ]; do
container_info=$(docker ps -a --no-trunc --format '{{ .ID }}\t{{ .Names }}\t{{ .State }}\tp:{{ .Label "com.docker.compose.project" }}' \
| grep 'p:redis-cache' \
| grep "$container_name")
container_state=$(echo $container_info | awk '{print $3}')
if [ "running" = $container_state ]; then
break
fi
sleep 0.1s
((counter++))
done
if [ $counter -lt $max_counter ]; then
echo $(compose exec --index="${2:-1}" "$1" hostname -i) | tr -d '\r'
else
echo "$container_name"
fi
}

# Restarts apache in the wordpress container
Expand All @@ -86,46 +110,53 @@ declare -A variables=();
declare options=();
declare -i index=1;

variables["-m"]="mode";
variables["--mode"]="mode";
variables["-c"]="client";
variables["--client"]="client";

for i in "$@"; do
arguments[$index]=$i;
prev_index="$(expr $index - 1)";

# this if block does something akin to "where $i contains ="
# "%=*" here strips out everything from the = to the end of the argument leaving only the label
if [[ $i == *"="* ]]
then argument_label=${i%=*}
else argument_label=${arguments[$prev_index]}
fi

if [[ -n $argument_label ]] ; then
# this if block only evaluates to true if the argument label exists in the variables array
if [[ -n ${variables[$argument_label]} ]] ; then
# dynamically creating variables names using declare
# "#$argument_label=" here strips out the label leaving only the value
if [[ $i == *"="* ]]
then declare ${variables[$argument_label]}=${i#$argument_label=}
else declare ${variables[$argument_label]}=${arguments[$index]}
fi
for i in ${@}; do
arguments[$index]=$i;
prev_index="$(expr $index - 1)";

# this if block does something akin to "where $i contains ="
# "%=*" here strips out everything from the = to the end of the argument leaving only the label
if [[ $i == *"="* ]]; then
argument_label=${i%=*}
else
argument_label=${arguments[$prev_index]}
fi

# first argument and no label detected: must be mode then
if [[ 1 == $index && -z $argument_label ]]; then
argument_label="-m"
fi

if [[ -n $argument_label ]]; then
# this if block only evaluates to true if the argument label exists in the variables array
if [[ -n ${variables[$argument_label]} ]]; then
# dynamically creating variables names using declare
# "#$argument_label=" here strips out the label leaving only the value
if [[ $i == *"="* ]]; then
declare ${variables[$argument_label]}=${i#$argument_label=}
else
declare ${variables[$argument_label]}=${arguments[$index]}
fi
else
# if the argument was not found store it in order
options+=(${arguments[$index]})
fi
else
# if the argument was not found store it in order
options+=(${arguments[$index]})
echo "unrecognized $1"
fi
fi

index=index+1;
index=index+1;
done;

mode="default"
if [[ -n "$1" && "$1" != --* ]] ; then
mode="$1"
fi

case "$mode" in
case ${mode-default} in

"default"|""|"-"|"simple"|"up"|"start")
"default"|"-"|"simple"|"up"|"start")
start 1 0 0 "${options[@]}"
apf --reset
apf WP_REDIS_CLIENT "${client-phpredis}"
Expand Down
8 changes: 2 additions & 6 deletions docker/wordpress/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
FROM bitnami/wordpress

ENV WORDPRESS_USERNAME="wordpress" \
WORDPRESS_PASSWORD="wordpress" \
WORDPRESS_BLOG_NAME="WP dev installation"
FROM bitnami/wordpress:5

# Required to perform privileged actions
USER 0
Expand All @@ -20,4 +16,4 @@ USER 1001
RUN rm -r /opt/bitnami/wordpress/wp-content/plugins/*

ENTRYPOINT [ "/app-entrypoint-custom.sh" ]
CMD [ "httpd", "-f", "/opt/bitnami/apache/conf/httpd.conf", "-DFOREGROUND" ]
CMD [ "/opt/bitnami/scripts/apache/run.sh" ]
97 changes: 61 additions & 36 deletions docker/wordpress/app-entrypoint-custom.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
#!/bin/bash -e
#!/usr/bin/env bash
set -e

. /opt/bitnami/base/functions
. /opt/bitnami/base/helpers
#
# Initial part copied from
# https://github.com/bitnami/bitnami-docker-wordpress/blob/a0affeb00b7087bcfb81e85e1982a9419ad401c9/5/debian-10/rootfs/opt/bitnami/scripts/wordpress/entrypoint.sh
#

# shellcheck disable=SC1091

set -o errexit
set -o nounset
set -o pipefail
# set -o xtrace # Uncomment this line for debugging purpose

# Load WordPress environment
. /opt/bitnami/scripts/wordpress-env.sh

# Load libraries
. /opt/bitnami/scripts/libbitnami.sh
. /opt/bitnami/scripts/liblog.sh
. /opt/bitnami/scripts/libwebserver.sh

print_welcome_page

if [[ "$1" == "nami" && "$2" == "start" ]] || [[ "$1" == "httpd" ]]; then
. /apache-init.sh
. /wordpress-init.sh
nami_initialize apache mysql-client wordpress
info "Starting gosu... "
. /post-init.sh
if [[ "$1" = "/opt/bitnami/scripts/$(web_server_type)/run.sh" || "$1" = "/opt/bitnami/scripts/nginx-php-fpm/run.sh" ]]; then
info "** Starting WordPress setup **"
/opt/bitnami/scripts/"$(web_server_type)"/setup.sh
/opt/bitnami/scripts/php/setup.sh
/opt/bitnami/scripts/mysql-client/setup.sh
/opt/bitnami/scripts/wordpress/setup.sh
/post-init.sh
info "** WordPress setup finished! **"
fi

# Additional custom actions
###
# Custom actions
###

# Load libraries
. /opt/bitnami/scripts/libfs.sh
. /opt/bitnami/scripts/libphp.sh
. /opt/bitnami/scripts/libapache.sh
. /opt/bitnami/scripts/libwordpress.sh
# Load environments
. /opt/bitnami/scripts/php-env.sh
. /opt/bitnami/scripts/apache-env.sh

PLUGIN_SOURCE_DIR="/redis-cache"
PLUGIN_TARGET_DIR="/opt/bitnami/wordpress/wp-content/plugins/redis-cache"
PHP_INI_PATH="/opt/bitnami/php/etc/php.ini"
WP_DIR="/bitnami/wordpress"
APF_FILE="/redis-cache/docker/apf.php"
APF_FILE_PATH="/redis-cache/docker/apf.php"

## Symlink generation
info "Creating plugin symlink"
Expand All @@ -27,44 +57,39 @@ if [ ! -L "$PLUGIN_TARGET_DIR" ]; then
fi

## Set APF file
info "Setting PHP auto prened file to inject our plugin constants"
if [ -f "$APF_FILE" ]; then
cp "$PHP_INI_PATH" "$PHP_INI_PATH-original"
TF=$(mktemp)
#awk '{gsub(/^(auto_prepend_file\s*=\s*).*/,"& \"/redis-cache/docker/apf.php\"",$1)}1' "$PHP_INI_PATH" \
awk '{gsub(/^auto_prepend_file =.*/,"auto_prepend_file = \"'"$APF_FILE"'\"")}1' "$PHP_INI_PATH" \
> "$TF" \
&& mv "$TF" "$PHP_INI_PATH"
if [ -f "$APF_FILE_PATH" ]; then
php_conf_set auto_prepend_file "$APF_FILE_PATH"
info "Set PHP auto prepend file"
else
error "Unable to set PHP auto prepend file"
ls -lah /redis-cache/docker | grep 'apf.php'
ls -lah $(dirname "$APF_FILE_PATH") | grep $(basename "$APF_FILE_PATH")
fi

## Create phpinfo file
info "Creating info.php file displaying phpinfo"
echo "<?php phpinfo();" > "/$WP_DIR/info.php"
echo "<?php phpinfo();" > "$WORDPRESS_BASE_DIR/info.php"

## Set development constants
info "Setting wp-config.php development constants"
chmod +w "/$WP_DIR/wp-config.php"
is_file_writable "$WORDPRESS_CONF_FILE" || chmod +w "$WORDPRESS_CONF_FILE"

wp config set WP_DEBUG true --raw
wp config set SCRIPT_DEBUG true --raw
wp config set WP_ENVIRONMENT_TYPE "local"
wordpress_conf_set WP_DEBUG true yes
wordpress_conf_set SCRIPT_DEBUG true yes
wordpress_conf_set WP_ENVIRONMENT_TYPE "local"

wp config set DISALLOW_FILE_EDIT true --raw
wp config set CONCATENATE_SCRIPTS false --raw
wordpress_conf_set DISALLOW_FILE_EDIT true yes
wordpress_conf_set CONCATENATE_SCRIPTS false yes

chmod -w "/$WP_DIR/wp-config.php"
is_file_writable "$WORDPRESS_CONF_FILE" && chmod -w "$WORDPRESS_CONF_FILE"

## Activates the newly copied plugin
info "Activating plugin and enabling dropin"
wp plugin install --activate \
query-monitor
wp plugin activate redis-cache
wp redis update-dropin
wp redis enable
wp_execute plugin activate redis-cache
wp_execute redis update-dropin
wp_execute redis enable

# fixes httpd already running error
apache_stop
# Needed for bitnami image - needs to be the last command!
exec tini -- "$@"
echo ""
exec "$@"

0 comments on commit cce5741

Please sign in to comment.