Wordpress Sites with Docker-Scripts
Note: This tutorial can be tried interactively on: https://katacoda.com/dashohoxha/courses/docker-scripts/wordpress
1. Introduction
With docker-scripts we can have one or more Wordpress containers, and each container can serve one or more sites, as shown in the diagram:
Since all these sites use the ports 80
and 443
, we need a reverse
HTTP proxy in order to forward each request to the corresponding
container that serves that site/domain. The docker-script container
wsproxy plays this role, using Apache virtual domains. Not only
that, but wsproxy also gets automatically a free LetsEncrypt SSL
certificate for each domain/site that it manages, and also redirects
automatically all HTTP requests to HTTPS.
All the containers of docker-scripts are placed on the same local
network, and wsproxy knows how to access the container of each
domain that it manages. So, only the wsproxy container needs to get
the ports 80/443
forwarded from the hosts. The Wordpress containers
get their requests from wsproxy through the docker-scripts local
network, so they don't need to expose the ports 80/443
to the host
or to the outside world.
Each Wordpress container in turn uses Apache virtual domains to be able to serve several (more than one) sites/domains.
Each Wordpress site needs also a database, and all of them use the docker-scripts container mariadb, which they access through the local network of docker-scripts.
2. Install dependencies
Install docker:
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
Install docker-scripts:
apt install m4 make git clone https://gitlab.com/docker-scripts/ds /opt/docker-scripts/ds cd /opt/docker-scripts/ds/ make install
Install Webserver Proxy:
ds pull wsproxy ds init wsproxy @wsproxy cd /var/ds/wsproxy/ vim settings.sh ds make
Install MariaDB:
ds pull mariadb ds init mariadb @mariadb cd /var/ds/mariadb/ vim settings.sh ds make
3. Install Wordpress sites
Install a wordpress container:
ds pull wordpress ds init wordpress @wordpress1 cd /var/ds/wordpress1/ vim settings.sh ds make
initialize and Install a Wordpress site:
cd /var/ds/wordpress1/ ds site ds site init site1.example.org ls ls site1.example.org ls apache2/sites-enabled/ cat apache2/sites-enabled/site1.example.org.conf ls ../wsproxy/sites-enabled/ cat ../wsproxy/sites-enabled/site1.example.org.conf
Initializing a site with
ds site init <domain>
not only creates a directory for this site, but also creates the necessary VirtualHost configurations for it on the containers wsproxy and wordpress1. It also gets a free SSL certificate from LetsEncrypt for this site/domain.After initializing the site, we can customize its
settings.sh
and then install wordpress in it:vim site1.example.org/settings.sh ds site install site1.example.org ls site1.example.org/
Now you can open in browser https://site1.example.org and login as admin.
Install another site on the same container:
cd /var/ds/wordpress1/ ds site init site2.example.org ls ls site2.example.org ls apache2/sites-enabled/ ls ../wsproxy/sites-enabled/
vim site2.example.org/settings.sh ds site install site2.example.org ls site2.example.org/
Install a second container:
ds init wordpress @wordpress2 cd /var/ds/wordpress2/ vim settings.sh ds make
Install a site on the second container:
ds site init site3.example.org vim site3.example.org/settings.sh ds site install site3.example.org ls site3.example.org/
4. Manage a Wordpress site
Backup
site3
:ds backup ds backup site3.example.org ls -lh backup/
Let's delete
site3
and then try to restore it from the backup:ds site del site3.example.org ls ls apache2/sites-enabled/
Before restoring, we have to initialize it:
ds site init site3.example.org ls site3.example.org/ ds restore ds restore site3.example.org \ backup/wordpress-site3.example.org-*.tgz ls site3.example.org/
Clone a site with
ds clone
:ds site clone ds site clone site3.example.org site4.example.org
Cloning a site to another one actually initializes the new site, then makes a backup on the source site and restores it on the target site, and finally fixes the options
siteurl
andhome
of the new site.Cloning a site might be useful when you want to try something new on the site, for example a new plugin or some configuration. Instead of applying it directly to the site, you may want to test it first on a clone.
Using wp-cli:
ds wp ds wp site3.example.org ds wp site3.example.org option ds wp site3.example.org option list ds wp site3.example.org option get siteurl ds wp site3.example.org option get home
Working inside the container:
ds shell ls cd site3.example.org wp wp option get siteurl wp option get home
5. Multiple sites vs. multiple containers
As we have seen, we can install multiple sites in a single Wordpress container, but we can also install a single site for each Wordpress container. Which one is better?
It depends.
When we consolidate multiple sites in a single Wordpress container we use less resources (RAM and disk space) then installing each site on its own container.
However if one of these sites is compromised (for example you install a plugin with a security hole) it can potentially compromise the other sites that are on the same container.
If each site is on its own container, they are better encapsulated from each-other.