Migrate Existing Wordpress Website to a Docker Container

Posted by Ray on August 7, 2023

This document is a guide for migrating an existing wordpress website with live data to a docker container. The container will contain its own mysql, wordpress, and phpmyadmin images.

This guide is a continuation of the Dockerized Wordpress, MySQL, and phpmyadmin with environment variables article.

Pre-requisites

  1. Docker & Docker Compose
  2. Running wordpress docker instance (see: Dockerized Wordpress, MySQL, and phpmyadmin with environment variables article)

Obtain a copy of the wordpress site to migrate

  1. Obtain a copy of the source code of the wordpress website to migrate using any methods you prefer (SSH, SFTP, webmin, etc.)

  2. Obtain a copy of the website database with the .sql file extension using any methods you prefer.

Run the dockerized wordpress container

  1. Run a newly built dockerized wordpress-mysql-phpmyadmin container.
    1
    2
    
    $ cd /mysite/
    $ docker-compose up
    

    docker_compose_up

  2. Verify if the new wordpress container is built successfully using your browser. running_wordpress

  3. Stop the running container
    1
    
    ctrl+c
    

    stopped_container

Migrate the source code

  1. Copy the newly built wordpress container’s wp-config.php file to the root of the directory.
    1
    2
    
    $ cp /path/to/wp-config.php .
    $ ls  # to verify if file has been copied
    

    copy_wpconfig

  2. Delete the contents of the src/ directory.
    1
    2
    
    $ rm -r /path/to/src/*
    $ ls src/  # to verify if the contents of the src/ directory has been deleted
    

    delete_src_contents

  3. Copy the source code of the wordpress website to migrate into the src/ directory.
    1
    2
    
    $ cp -r /path/to/existing/wordpress/* /path/to/new/wordpress/src/
    $ ls src/  # to verify if existing wordpress source codes has been copied
    

    copy_wp_contents

  4. Copy the non-default settings of the wordpress to migrate’s wp-config.php file to the root wp-config.php (the one we copied from the original wordpress container), excluding database settings. These settings are site-specific, and is necessary to ensure your wordpress site runs perfectly. If all of the settings are default, proceed to the next step.

  5. Replace the src/wp-config.php with the ./wp-config.php.
    1
    2
    
    $ cp -f wp-config.php src/wp-config.php
    $ cat src/wp-config.php  # verify if files have been copied. wp-config should now have docker configuration
    

    wp_config_docker

  6. Delete the wp_config.php located in the project root, and run the docker container
    1
    2
    3
    
    $ rm wp-config.php
    $ ls  # to verify if wp_config.php is deleted
    $ docker-compose up
    

    remove_wpconfig_run_docker

Migrate the database

  1. Run a new terminal, and copy the original .sql database into the db_data/ directory of your project.
    1
    2
    
    $ cp /path/to/your/db.sql /path/to/your/project/db_data/
    $ ls /path/to/your/project/db_data/  # to verify if the database has been copied
    

    copy_original_database

  2. Execute the interactive terminal of the running mysql database container, and login into mysql.
    1
    2
    3
    
    $ docker exec -it your_project_name_db bash
    bash# $ mysql -u your_MYSQL_USER -p
    Enter password: your_MYSQL_PASSWORD
    

    enter_mysql

  3. Use wordpress database in mysql bash
    1
    2
    
    mysql > $ use wordpress;
    mysql > $ show tables;  # to verify if database is empty
    

    use_wordpress_database

  4. Migrate database
    1
    2
    
    mysql > $ source /var/lib/mysql/your_database_name.sql
    mysql > $ show tables;  # check if data has been migrated
    

    migrate_db_data check_migration

  5. Open phpmyadmin in your browser localhost:your_phpmyadmin_port, and login using root as username, and your MYSQL_ROOT_PASSWORD root password. Expand the wordpress database, and select the wp_options table. phpmyadmin_options

  6. Click edit on the row with siteurl and home option name, and edit the value from https://your_website_domain into http://localhost:your_wordpress_port. phpmyadmin_options_localhost

  7. Refresh your browser running. You should be able to see your migrated wordpress website. You can login using your live wordpress admin credentials

  8. Navigate to Setttings > Permalinks and remember the selected Permalink Structure.

  9. Select any permalink structure, and save. Then re-select the original permalink structure from last step and save.

Notes

*** Migrating the database steps 5 and 6 can be done through the mysql terminal directly by:

  1. Running the mysql container interactive terminal $ docker exec -it your_project_name_db bash
  2. Logging into mysql $ mysql -u your_MYSQL_USER -p
  3. Using the wordpress database. mysql> $ use wordpress;
  4. Changing the value of siteurl
    1
    
    mysql> $ UPDATE `wp_options` SET `option_value` = 'http://localhost:your_wordpress_port' WHERE  `wp_options`.`option_name` = 'siteurl';
    
  5. Changing the value of home
    1
    
    mysql> $ UPDATE `wp_options` SET `option_value` = 'http://localhost:your_wordpress_port' WHERE  `wp_options`.`option_name` = 'home';
    

Done!