I recently switched web hosts from Digital Ocean to OVH. I decided to go the Docker route and hopefully make migration less of a pain in the future. I run 4 websites off of the host, 3 of which are WordPress and 1 Koken site for photography. I don’t think it was less of a pain to do this, but I had to finish it once I started it.
I’m certain that this is NOT the optimal setup, but I could not find anyone who did this so hopefully it’s a jumping off point for someone with more patience and time than me. This is a waste of resources to have 3 separate WordPress Apache docker containers running simultaneously.
I chose to run a separate docker container for each website, and use the popular nginx Docker reverse proxy to serve all of them out at different “VIRTUAL_HOST” s. They are not setup for HTTPS yet.
I am going to use the following containers:
2x containers of the wordpress image with a small addition added in for the headers module in a dockerfile.
1x container of koken/koken-lemp
1x container mariadb (or you could use mysql). this will be where all the wordpress containers go to CRUD their databases.
1x container for the nginx reverse proxy
Let’s get to business now! So the first step is to get the custom wordpress container configured so that it will have the custom modules. You can skip this step if you don’t use any extra modules.
#taken from https://www.linux.com/learn/how-build-your-own-custom-docker-images
MAINTAINER DockerFan version 1.0
ENV DEBIAN_FRONTEND noninteractive
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
RUN a2enmod headers
Save this as dockerfile in a directory, and run
build -t wpapache .
Now you have an image of an up-to-date wordpress, along with a customization baked into it.
Next step: setup a docker-compose.yml file so you can get everything linked and setup with just one command.
##run by docker-compose up -d
VIRTUAL_HOST: 'www.wordpressblog2.com,wordpressblog2.com'mysqldump -urootuserhere -p wordpressblog1 --single-transaction --routines --triggers > /web/mysql/backupwp1.sql
## note that you can have a continuing list of VIRTUAL_HOSTS if you have multiple domains or subdomains that will redirect to this container
## You can create more ports here, for instance for HTTPS, but I commented it out for now
## notice the port is different for local host so that they do not conflict with the wpblog1
#koken has its own mysql container built in, so there's no need to link to the mysql/mariadb container like the wordpress ones
## note the mysql volume is being stored on the local machine, so it should be easier to manage
## I commented the above env variables out because they are used to create a new database, and we are just migrating an existing one. still good to know though.
## in case you need to also get into mysql and import your SQL files with a GUI, you should uncomment this whole section.
# image: corbinu/docker-phpmyadmin
# - mysql
# - 8080:80
# restart: always
# MYSQL_USERNAME: root
# MYSQL_ROOT_PASSWORD: mysqlpasswordhere
Now that you have docker-compose.yml configured based on your wordpress & koken mysql usernames/passwords and pointed at the location, you just have to run
docker-compose up -d
If all goes well they should be running now on the new server.
If you’re like me and have to edit the docker-compose file and re-run it a lot, a small bash script might help you delete all your Docker containers — WARNING — and recreate from the docker-compose.yml file:
##recreateContainers.sh - delete all docker containers, and run the docker-compose command.
ALL=$(docker ps -a -q)
docker stop $ALL
docker rm $ALL
docker-compose up -d
If you are having issues with the MySQL databases not connecting, you will have to do a mysqldump of the databases from the old mysql instance and import them into the new tables.
mysqldump -urootuserhere -p wordpressblog1 --single-transaction --routines --triggers > /web/mysql/backupwp1.sql
docker exec -i -t mysql_container bash
#now you are in the mysql container
mysql -urootuserhere -p wordpressblog1 < /var/lib/mysql/backup1.sql