It is very easy with Docker to have a working WordPress up in no time with the official WordPress Image on Docker Hub.
If you want to have an image which is backed up properly and that you can migrate w/o any problems on a different machine, you need to take care of the proper volumes mappings. Unfortunately I did not find the proper documentation and I needed to find out the hard way.
WordPress Startup Logic
The WordPress solution consists of two separate pieces which are managing state and therefore will need to be coordinated together:
- the WordPress Application itself and
- the Database (mariadb)
The following startup logic is used by the Official Image:
- When the database is started with an empty instance it automatically creates a new inital database in ./var/lib/mysql
- When WordPress is started from an empty instance it automatically copies the WordPress application from /usr/src/wordpress to /var/www/html and it creates a database with the name wordpress.
In order to prevent this initial setup logic, you need to make sure that these locations are mapped properly and that the complete data is available.
We see the following solution alternatives:
- Do it the WordPress Way and install a Backup or Data Migration Plugin. In the case of data restore on a new environment you need to start from a new empty wordpress instance, install the plugin and the do the restore. From my point of view this is by for the easiest solution.
If you want do to it the Docker way, things are getting more difficult because you need to address each component seperalty.
- Make sure that you map the database volumes so that you can restore them. Please note that if you do only a regular file backup the database might not be in a consistent state.
- Use a separate docker image which performs backups (e.g. zeenlym/mariadb-backup)
- Avoid state in volumes. Do not make any mappings and provide your full solution as custom image by putting your components into /usr/src/wordpress or /var/www/html. This approach only works if your side is stable and rarely changes. Google can help you to get quite some information on the topic of statless wordpress.
- Or make sure that you map the proper file volume:/var/www/html. This is our selected solution which we have implemented below.
The WordPress URL is stored in the database and it is not possible to address the WordPress instance with a different URL. You can find more details in the official documentation.: So you might need to have phpmyadmin available do that you can update the address.
Here is our docker compose file for all components which provides the proper mappings.
version: '3' services: wordpress: image: wordpress:latest container_name: wordpress ports: - 8005:80 environment: - WORDPRESS_DB_PASSWORD=my-password - WORDPRESS_DB_HOST=wordpress-mysql restart: always dns: 188.8.131.52 volumes: - /srv/wordpress:/var/www/html wordpress-mysql: image: mariadb volumes: - /srv/wordpress-mysql:/var/lib/mysql - /backup:/backup environment: - MYSQL_ROOT_PASSWORD=my-password restart: always wordpress-db-backup: image: zeenlym/mariadb-backup environment: - TIMEZONE=Europe/Berlin - SCHEDULE=0 0 * * * - BACKUP_METHOD=mysqldump - MYSQL_HOST=wordpress-mysql - MYSQL_DATABASE=wordpress - MYSQL_USER=root - MYSQL_PASSWORD=my-password volumes: - /backup/wordpress-mysql:/backup phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin environment: - PMA_ARBITRARY=1 restart: always ports: - 8006:80 volumes: - /sessions
In order to run the application on a different machine you just need to make sure that the mapped directories are available. Im my example this is: