Backstory:
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.
Disclaimer:
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.
Intro:
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.
##dockerfile start
#taken from https://www.linux.com/learn/how-build-your-own-custom-docker-images
FROM wordpress
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
#enable headers
RUN a2enmod headers
##dockerfile end
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.
##docker-compose.yml start
##run by docker-compose up -d
wpblog1:
image: wpapache
links:
- mysql
environment:
WORDPRESS_DB_NAME: mysqldb1
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: databaseuser
WORDPRESS_DB_PASSWORD: db-password
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
ports:
- "127.0.0.1:8081:80"
#- "127.0.0.1:8082:443"
## You can create more ports here, for instance for HTTPS, but I commented it out for now
working_dir: /var/www/html
volumes:
- /web/myblog1:/var/www/html/
wpblog2:
image: wpapache
links:
- mysql
environment:
WORDPRESS_DB_NAME: mysqldb2
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: databaseuser
WORDPRESS_DB_PASSWORD: db-password
VIRTUAL_HOST: 'www.wordpressblog2.com,wordpressblog2.com'
ports:
- "127.0.0.1:8082:80"
## notice the port is different for local host so that they do not conflict with the wpblog1
working_dir: /var/www/html
volumes:
- /web/myblog2:/var/www/html/
kokensite:
#koken has its own mysql container built in, so there's no need to link to the mysql/mariadb container like the wordpress ones
image: koken/koken-lemp
environment:
VIRTUAL_HOST: 'www.kokensitehere.com,kokensitehere.com,photography,kokensitehere.com'
ports:
- "127.0.0.1:8084:8080"
working_dir: /usr/share/nginx/www
restart: always
volumes:
- /web/kokensitehere:/usr/share/nginx/www
- /web/kokensiteheremysql:/var/lib/mysql
## note the mysql volume is being stored on the local machine, so it should be easier to manage
mysql:
image: mariadb:10.1
ports:
- "127.0.0.1:3306:3306"
#environment:
#- MYSQL_ROOT_PASSWORD=mysqlpasswordhere
#- MYSQL_DATABASE=mysqldatabase
## 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.
volumes:
- /web/mysql:/var/lib/mysql
#phpmyadmin:
## 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
# links:
# - mysql
# ports:
# - 8080:80
# restart: always
# environment:
# MYSQL_USERNAME: root
# MYSQL_ROOT_PASSWORD: mysqlpasswordhere
nginx:
image: jwilder/nginx-proxy
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
##docker-compose.yml end
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:
#!/bin/bash
##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