How to migrate WordPress to Docker container

migrate WordPress to Docker

In this post we migrate WordPress to Docker container. I have LAMP on my server and LEMP what I’m going to use on Docker container.

If you need more ideas how to make your Docker environment more complex. Here you can take a look how you can use proxy, logging, LAMP and LEMP on same enviroment: docker-server-stack. I prefer to start with migrate WordPress to Docker and then make it more complex.

Mysqldump and tar

First find out what is your WordPress database name, user and password. You can find it in your wp-config.php

$ cat wordpress/wp-config.php

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wpdb');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'wppassword');

Now you know your wpuser, wppassword and wpdb. Next you need to make mysqldump

$ mysqldump -u [wpuser] -p[wppassword] [wpdb] | gzip -9 > wordpress.sql.gz

Then create tar from your WordPress folder

$ tar -zcvf wordpress.tar.gz wordpress/

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.

$ git clone
$ mv wordpress.tar.gz docker-lemp-stack/
$ mv wordpress.sql.gz docker-lemp-stack/
$ cd docker-lemp-stack

Configure docker-compose file

$ nano docker-compose.lemp.yaml

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.

      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=[wpdb]
      - MYSQL_USER=[wpuser]
      - MYSQL_PASSWORD=[wppassword]

On the docker file change your nginx and fpm volumes to wordpress

      - ./www/wordpress:/var/www/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.

      - ./data/mysql:/var/lib/mysql

Now your docker-compose.lemp.yaml should look something like this

version: '2'
    image: nginx:1.11
      - "80:80"
      - mysql
      - fpm
      - ./www/wordpress:/var/www/wordpress
      - ./configures/conf.d:/etc/nginx/conf.d:ro

    image: mysql:8.0
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=[wpdb]
      - MYSQL_USER=[wpuser]
      - MYSQL_PASSWORD=[wppassword]
      - ./data/mysql:/var/lib/mysql

    build: images/nginxphp
      - ./www/wordpress:/var/www/wordpress
      - mysql

If you created volume to mysql. Create data folder.

$ mkdir data

Change tester folder to wordpress. So it matches your volumes in docker-compose file

$ mv www/tester www/wordpress

Change in dbtest.php your wpuser, wppassword and wpdb. This way you can test your LEMP stack.

$ nano www/wordpress/dbtest.php

        // Create connection

In conf.d you need to change your server_name to your domain and root directory to /var/www/wordpress

$ nano configures/conf.d/default.conf

    server_name [domain];
    root /var/www/wordpress;

You need to do this step if you want to test your WordPress on local machine.

$ sudoedit /etc/hosts [domain]

Testing your LEMP stack

Start your LEMP stack (this could take a minute)

$ sudo docker-compose -f docker-compose.lemp.yaml up -d

Now curl should answer from index.html. Nginx is working and answering.

$ curl [domain]/index.html

         <p>Hello World!</p>

Your index.php should be answering “My first PHP page”. Now you know your fpm is working.

$ curl [domain]/index.php

<!DOCTYPE html>
        <h1>My first PHP page</h1>
        <p>Hello World!</p>

Finally curl dbtest.php. Answer should be just: works. Now you know MySQL is working.

$ curl [domain]/dbtest.php


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.

$ sudo apt install mysql-client

Then you need to know what IP address your Docker MySQL is running. Give this command to find out IP address

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dockerlempstack_mysql_1

So my Docker MySQL running on So this is mysql_ip. Then restore your mysqldump to Docker Mysql.

$ gunzip < wordpress.sql.gz | mysql -u [wpuser] -p -h [mysql_ip] [wpdb]

After that you can stop your LEMP stack

$ sudo docker-compose -f docker-compose.lemp.yaml stop

WordPress folder to Docker

Then remove your testing folder

$ rm -r www/wordpress/

Unpack your WordPress folder from tar to www folder

$ tar -vzxf wordpress.tar.gz -C www/

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.

$ nano www/wordpress/wp-config.php

define('DB_HOST', 'mysql');

I prefer to add correct ownership and rights to WordPress folder.

$ sudo chown -R 33:33 www/wordpress/
$ sudo chmod -R 755 www/wordpress/
$ sudo chmod 444 www/wordpress/wp-config.php

Now you can start your LEMP stack

$ sudo docker-compose -f docker-compose.lemp.yaml up -d

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:

Useful commands

Stop docker-compose file containers

$ sudo docker-compose -f .yaml stop

Delete docker-compose file containers and volumes

$ sudo docker-compose -f .yaml rm -v

Stop all containers

$ sudo docker stop $(sudo docker ps -a -q)

Delete all containers and volumes

$ sudo docker rm -v $(sudo docker ps -a -q)

Delete all images

$ sudo docker rmi $(sudo docker images -q)

Comment and let me know what you think of this guide. Did you migrate WordPress to Docker correctly?