Skip to content

Commit

Permalink
Adjust for wp-config changes in the WordPress image.
Browse files Browse the repository at this point in the history
None of the overrides worked anymore in the latest docker image because overriding requires a special wp-config.php, which we now use.
See docker-library/wordpress#576

The previous values that need to be preserved are now extracted to a environment file for Docker.

A future commit might also include username, password and the database values, which for now stay hard-coded.
  • Loading branch information
sdellenb committed May 14, 2021
1 parent 212fb61 commit e51eb62
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
backupwordpress/
docker/*.env
15 changes: 13 additions & 2 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ services:
restart: always
environment:
# Override the configured DB settings with hardcoded values.
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
# WORDPRESS_DEBUG: 1 # uncomment in case of problems.
env_file:
# Values extracted from the original wp-config.php.
wordpress.env

# In case you need to look into the database:
# adminer:
# image: adminer
# restart: always
# ports:
# - 8080:8080

volumes:
db_data: {}
13 changes: 13 additions & 0 deletions scripts/extract_latest_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,21 @@ rm -f extracted/*.sql
# Source: https://www.wpbeginner.com/wp-tutorials/how-to-move-live-wordpress-site-to-local-server/
SITEURL=$(awk -F\' '/siteurl/ {print $4}' db_dumps/00-latest.sql)
TABLE_PREFIX=$(awk -F\' '/table_prefix/ {print $2}' extracted/wp-config.php)
DB_CHARSET=$(awk -F\' '/DB_CHARSET/ {print $4}' extracted/wp-config.php)
DB_COLLATE=$(awk -F\' '/DB_COLLATE/ {print $4}' extracted/wp-config.php)
cat <<EOT >> db_dumps/01-update_siteurl.sql
UPDATE ${TABLE_PREFIX}options SET option_value = replace(option_value, '${SITEURL}', 'http://localhost:8000') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE ${TABLE_PREFIX}posts SET post_content = replace(post_content, '${SITEURL}', 'http://localhost:8000');
UPDATE ${TABLE_PREFIX}postmeta SET meta_value = replace(meta_value,'${SITEURL}','http://localhost:8000');
EOT

# Generate the environment override file from the wp-config values, as that file will be replaced the auto-generated one.
{
printf 'WORDPRESS_TABLE_PREFIX=%s\n' "${TABLE_PREFIX}"
printf 'WORDPRESS_DB_CHARSET=%s\n' "${DB_CHARSET}"
printf 'WORDPRESS_DB_COLLATE=%s\n' "${DB_COLLATE}"

} > "${ROOT_DIR}"/docker/wordpress.env

# Delete the old config file to get the auto-generated one provided in the Docker image, which allows for environment variable overrides.
rm -f extracted/wp-config.php

0 comments on commit e51eb62

Please sign in to comment.