How to migrate WordPress to a Docker container
This is the tutorial how to migrate WordPress to a Docker container. I have LAMP on my server and I’m going to use LEMP on my Docker container.
If you need more ideas on how to make your Docker environment more complex, in following link you can take a look how you can use a proxy, a logging software, LAMP and LEMP on same the enviroment: docker-server-stack. I prefer you to start with migrating WordPress to Docker container and after that make it more complex.
Mysqldump and tar
First find out what is your WordPress database name (wpdb), user (wpuser) and password (wppassword). You can find it in your wp-config.php file in your server
Now you know your wpuser, wppassword and wpdb. Use this information to create a mysqldump that contains your Wordpress database
Create a tar file from your WordPress folder
A docker LEMP stack
I prefer to use LEMP stack than Dockers own WordPress container. This is because this way it is easier to control every single component and if necessary to debug something. Of course you can use WordPress container too.
Clone git repository and move your mysqldump and Wordpress tar to repository.
Configure docker-compose file
Change your wpdb, wpuser and wppassword. These needs to be same as in the wp-config.php. MYSQL_ROOT_PASSWORD can be whatever you want. Of course I recommend it should be secure password.
On the docker file change your nginx and fpm volumes to wordpress
I recommend to add volume to MySQL. This way it is easy to move your whole stack to other enviroment, backup your data and databases aren’t inside of Docker container.
Now your docker-compose.lemp.yaml should look something like this
If you created volume to mysql. Create data folder.
Change tester folder to wordpress. So it matches your volumes in docker-compose file
Change in dbtest.php your wpuser, wppassword and wpdb. This way you can test your LEMP stack.
In conf.d you need to change your server_name to your domain and root directory to /var/www/wordpress
You need to do this step if you want to test your WordPress on local machine.
Testing your LEMP stack
Start your LEMP stack (this could take a minute)
Now curl should answer from index.html. Nginx is working and answering.
Your index.php should be answering “My first PHP page”. Now you know your fpm is working.
Finally curl dbtest.php. Answer should be just: works. Now you know MySQL is working.
So now your LEMP stack is fully working. If something went wrong don’t go further. You need to have LEMP stack working before continuing. Go back and recheck every step. You can easily start debugging that specific component that didn’t work. Probably you just typed something wrong or forgot something.
Adding mysqldump to docker
First you need mysql-client if some reason you don’t have. Like if you are building fresh environment.
Then you need to know what IP address your Docker MySQL is running. Give this command to find out IP address
So my Docker MySQL running on 172.20.0.2. So this is mysql_ip. Then restore your mysqldump to Docker Mysql.
After that you can stop your LEMP stack
WordPress folder to Docker
Then remove your testing folder
Unpack your Wordpress folder from tar to www folder
Now you need to configure in wp-config.php your DB_HOST correctly. DB_HOST is the name of the mysql container on compose file so it is just mysql.
I prefer to add correct ownership and rights to WordPress folder.
Now you can start your LEMP stack
Now you have migrate WordPress to Docker
If you want to configure more of your Docker environment you can take a look how it is done:
docker-server-stack
Useful commands
Stop docker-compose file containers
Delete docker-compose file containers and volumes
Stop all containers
Delete all containers and volumes
Delete all images
Comment and let me know what you think of this guide. Did you migrate WordPress to Docker correctly?