All is in the title. Any ideas ?
Would it be possible to add this option in the Dockerfile ?
Moving Wordpress instances is a problem in itself. Wordpress stores all configuration file in the database and has a complex migration procedure.
In my experience the easiest route is to use an import/export or migration plugin to move from one instance to another.
In fact I am using a script which do the job perfectly and I use it in production to move wordpress installation from local to production. So I propose to add this logic in the docker-entrypoint.sh:
$WORDPRESS_SITE_URL is set then change the SITE_URLsh change-siteurl.sh $WORDPRESS_SITE_URL to change the SITE_URLThat is simple and clean in my opinion. If you think it can be usefull for others, I can push a PR.
The script change-siteurl.sh:
#!/usr/bin/env sh
if [ $# -lt 1 ]; then
echo "Usage: $0 http://www.site-url.com"
exit 1
fi
echo "* Check site url modification"
WORDPRESS_CONFIG="/var/www/html/wp-config.php"
WORDPRESS_DB_NAME=$(cat $WORDPRESS_CONFIG | grep DB_NAME | cut -d \' -f 4)
WORDPRESS_TABLE_PREFIX=$(cat $WORDPRESS_CONFIG | grep "\$table_prefix" | cut -d \' -f 2)
OLD_SITE=$(mysql -h localhost -sN $WORDPRESS_DB_NAME -e "select option_value from ${WORDPRESS_TABLE_PREFIX}options WHERE option_name = 'siteurl';")
NEW_SITE=$1
if [ "$OLD_SITE" = "$NEW_SITE" ]; then
echo "* New site url ($NEW_SITE) already set in the current wordpress installation."
exit 0
fi
echo "* Change site url"
mysql -h localhost $WORDPRESS_DB_NAME -e "UPDATE ${WORDPRESS_TABLE_PREFIX}options
SET option_value = replace(option_value, '$OLD_SITE', '$NEW_SITE')
WHERE option_name = 'home'
OR option_name = 'siteurl';
"
echo "* Change guid url"
mysql -h localhost $WORDPRESS_DB_NAME -e """UPDATE ${WORDPRESS_TABLE_PREFIX}posts
SET guid = REPLACE (guid, '$OLD_SITE', '$NEW_SITE');
"""
echo "* Change media's url in articles and pages"
mysql -h localhost $WORDPRESS_DB_NAME -e """UPDATE ${WORDPRESS_TABLE_PREFIX}posts
SET post_content = REPLACE (post_content, '$OLD_SITE', '$NEW_SITE');
"""
echo "* Change url of meta data"
mysql -h localhost $WORDPRESS_DB_NAME -e """UPDATE ${WORDPRESS_TABLE_PREFIX}postmeta
SET meta_value = REPLACE (meta_value, '$OLD_SITE','$NEW_SITE');
"""
echo "* New site url is now $NEW_SITE (old was $OLD_SITE)"
The mysql binary is not available inside the wordpress image, so this script wouldn't work there.
It seems like putting your script into its own image and giving instructions on how to use --volumes-from and --link to access the /var/www/html volume and the MySQL server would be just as useful (or moreso) than having it in this image.
Ok. I close here.
If you do end up creating an image/repository for your script, be sure to post a link here.
Thanks! :+1:
Well I think I will create an intermediate container which will be in charge to modify site_url and also restore/backup data (/var/lib/mysql and /var/www/html) if needed.
If I do something generic, I will post it here.
Additionally, I think it'd be valuable if we added a warning about making
sure that your access method is what you plan to deploy on during the
Wordpress setup, especially since Docker makes it so much easier to do that
part "wrong" than general deployments usually have.
There seems to be a setting in wp_config.php to change SITE_URL and HOME. Apparently, this does not alter the database, but only overrides those settings (In the admin panel, the settings are greyed out).
From a docker perspective, this seems like a reasonable solution to me. If we want to change the URL, we re-run the image.
Would this be enough? Or are there still any other places that need to be taken care of?
As I understand, setting define('RELOCATE',true); will also alter the database.
@hadim Could you re-open the issue please? Even if your script is not the solution, changing the URL is still a wanted feature, I think.
In my experience, it's still not trivial to move Wordpress from one URL to another -- during normal usage/setup, it hard-codes full URLs in posts, etc, so I think this is still something we're better off not coding for and instead providing documentation and links to help inform our users.
My experience is semi-relevant. I'm developing locally and recently added nginx-proxy to my setup so my domain changed. The easiest workaround for me was to use @kluen 's method (wordpress documentation - Relocate method).
I used docker cp command to copy out wp-config.php from my container, edited with define('RELOCATE', true);, chown the file (docker exec -it container sh; chown www-data:www-data wp-config.php), navigate to new_domain/wp-login.php, change WordPress Address and Site Address under General Settings, edit out define('RELOCATE', true); and reupload to container (again chowning). Done.
Very round about way and hacky but this worked for me.
Closing since this isn't something we're going to update the image to do directly. We now have wordpress:cli variants which include WP-CLI which can help with this transition, but generally it requires the same love and care that a normal move of a WordPress installation does.
For further help/support, I'd recommend the Docker Community Forums, the Docker Community Slack, or Stack Overflow. Thanks!
Most helpful comment
There seems to be a setting in
wp_config.phpto changeSITE_URLandHOME. Apparently, this does not alter the database, but only overrides those settings (In the admin panel, the settings are greyed out).From a docker perspective, this seems like a reasonable solution to me. If we want to change the URL, we re-run the image.
Would this be enough? Or are there still any other places that need to be taken care of?
As I understand, setting
define('RELOCATE',true);will also alter the database.@hadim Could you re-open the issue please? Even if your script is not the solution, changing the URL is still a wanted feature, I think.