Change Domain for WordPress Docker Container with phpMyAdmin

Change Domain for WordPress Docker Container with phpMyAdmin

Are you trying to migrate an existing WordPress Docker container to a new domain URL? One might change domain for a couple of reasons. For example, change in brand name, favorite domain in .com becomes available or change to different TLD, e.g., from .xyz to .com. In this tutorial, I will demonstrate two methods of domain name migration for a WordPress built with Docker Compose, from exampleold.com (old) to examplenew.com (new) domain.

Step 1 – Making Changes to Docker Compose and nginx (web server)

There are two files that require modifications.

docker-compose.yml – Replace the domain name in certbot container because I was running Let’s Encrypt for SSL.

command: certonly --webroot --webroot-path=/var/www/html --email example@gmail.com --agree-tos --no-eff-email --force-renewal -d examplenew.com -d www.examplenew.com

nginx.conf – Replace the domain name in server_name for niginx web server plus ssl_certificate and ssl_certificate_key if you had deployed Let’s Encrypt.

server_name examplenew.com www.examplenew.com;

ssl_certificate /etc/letsencrypt/live/examplenew.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/examplenew.com/privkey.pem;

My WordPress site loaded in browser successfully with the new domain https://examplenew.com after I restarted my docker containers. Next, I wanted to test WordPress Admin at https://examplenew.com/wp-admin. After I click Log In, the web page redirected to the old domain https://exampleold.com/wp-admin which of course resulted in 404: Page Not Found.

There are 2 solutions to resolve this redirection problem.

Step 2a – Update wp-config.php File (preferably a new WordPress installation)

The location of this file is usually in /docker-project/wordpress/wp-config.php. Add these 2 lines to your wp-config.php file. Restart the docker containers and you should be able to log in to WordPress Admin dashboard again.

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

Step 2b – Update MySQL Database using phpMyAdmin (recommended)

This is my preferred method because I don’t like to add redundant lines to config files. Plus, this is a more throughout method of replacing URLs for a seasoned WordPress site that has many old URLs in e.g. posts. To do this, we need to launch a phpMyAdmin container temporarily to access the WordPress database to make the changes. Add these few lines (replace ‘yournetwork’ with the networks similar to database container) in your docker-compose.yml file.

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - "8080:80"
    networks:
      - yournetwork

Execute docker-compose up -d to start the phpMyAdmin container.

Visit http://examplenew:8080 (not https://) and log in to phpMyAdmin. The login is your WordPress database credentials defined either in your docker-compose.yml or a secret .env file.

phpMyAdmin login port 8080

After log in, click on WordPress database and run these seven SQL UPDATE statements (replace exampleold.com with existing domain and examplenew.com with new domain).

UPDATE wp_comments SET comment_content = replace(comment_content , 'exampleold.com','examplenew.com');
UPDATE wp_links SET link_url = replace(link_url, 'exampleold.com','examplenew.com');
UPDATE wp_options SET option_value = replace(option_value, 'exampleold.com', 'examplenew.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_postmeta SET meta_value = replace(meta_value,'exampleold.com','examplenew.com');
UPDATE wp_posts SET post_content = replace(post_content, 'exampleold.com', 'examplenew.com');
UPDATE wp_posts SET guid = replace(guid, 'exampleold.com','examplenew.com');
UPDATE wp_usermeta SET meta_value = replace(meta_value, 'exampleold.com','examplenew.com');

Open your browser and go to WordPress Admin. If everything is in order, you may remove the phpMyAdmin container from your docker-compose.yml or just comment those few lines, just in case you need to access the WordPress database again in future.

Conclusion

There are two objectives when I wrote this tutorial. How to migrate a WordPress Docker container to a new domain name and how easy it is to spin up a phpMyAdmin container with Docker Compose with just few lines of codes.

Domain migration does have its own consequences e.g. loss of ranking in Google. For my case, I bought my preferred domain in .xyz (.com was unavailable) but I changed my mind to get a .com instead and had to modify my domain name a little in order to have it available for purchase. Thus, there was a need for me to migrate my WordPress container.

phpMyAdmin is a useful administration tool for MySQL and MariaDB that is free (open source). In my opinion, one should not need to access the WordPress database and this is true if you check out some of the online examples for WordPress built with Docker Compose. The phpMyAdmin container will be excluded in the docker-compose.yml file. However, do keep phpMyAdmin in your pocket as it can come in handy for others non-WordPress setup.

Show 1 Comment

1 Comment

  1. Lukas Mikelionis

    rubbish, as far as I understand, this is custom setup using nginx? or what are preconditions?
    I’m trying to change URL using official image: wordpress:latest does not work at all…
    So You made tutorial, that works in some cases, that you understand, but some might not as it doesnt hols enough sufficient information… e.g.: in what cases you should and in what you shouldnt…

Leave a Reply

Your email address will not be published. Required fields are marked *