Greetings to Docker specialists and regular visitors of our blog!
- What should you do if your project has grown and a single server can't handle the load?
- What should be considered during the scaling process?
- How to correctly scale your high-volume website?
- And which docker commands you should be familiar with to perform this?

I'll try to disclose this topic in details and show you the best ways to solve such problems.
So, let's imagine that your project is developed on PHP and it was dockerized before. You're using a single node for your whole stack and a remote MySQL server as a database server. You've launched a marketing campaign today and a huge wave of visitors has flown to your website, but something is wrong. Surprisingly everything began slowing down and users are experiencing a bad experience. Or it can be even worse: your website started to throw errors and just stopped opening at all. Am I talking about something familiar? So let's go!

Ideally, you should use applications in Docker when they're serverless, in other words when they're not dependent on permanent files or data writing to the disk, so we're able to easily launch them on any available server. But in most cases the situation is different, so let's review the example of usage of Wordpress inside of Docker (not the best option), but it will work to provide you with clear examples of how to correctly use scaling for PHP. Also, we will show you in live how to use corresponding docker commands to resolve such situations.
Let's begin from the review of the scaling schema of PHP Docker high available cluster:

scale-php-application-across-multiple-nginx-docker-nodes

NFS storage - we'll use it as shared storage for our WordPress. We'll carry out wp-content directory there, where plugins and static content (i.e images) are stored. Of course, the best way would be to store images in some kind of CDN, but that's totally another story.
HAproxy Load Balancer - will be used for redirecting users to the correspondent container and distributing the load accordingly.
Code container - we'll keep Wordpress core here and run Nginx with PHP-FPM.
Cron container - this is a container with your code, but it will be used only for cron tasks execution. I'd like to draw your attention that Wordpress has several options of cron executions.
The first one is the usage of the standard CRON function when the task is executed when the client appeals to the website, and the second option is the usage of system cron. If we're talking about high load usage of system crons is the better option, as it will reduce the load on the servers.

High Available Cluster Info

Here's what was used in order to get started:

Two servers with Ubuntu 22.04 in Digital Ocean thedockerexperts-wp-01 & thedockerexperts-wp-02
Test domain wp.thedockerexperts.com
Installed and configured Rancher rancher.example.com
Private registry
Remote MySQL Server

Docker-ce Installation

Before the start, we need to check the compatibility of Docker versions and your Rancher. When the article was written I used Rancher 2.6 and versions were checked via official rancher site
I installed docker-ce 20.10.x-ce on all hosts:

curl https://releases.rancher.com/install-docker/20.10.sh | sh

NFS server setup

I've chosen thedockerexperts-wp-01 to be the NFS server:

apt-get install nfs-kernel-server

Now we're creating a directory www-data with permissions that will be shared between hosts:

mkdir -p /srv/nfs-server/wp.thedockerexperts.com/shared/wp-content

chown -R www-data:www-data /srv/nfs-server/wp.thedockerexperts.com/shared/wp-content

To limit the accesses from trusted hosts let's edit file /etc/exports and add this string:

/srv/nfs-server/wp.thedockerexperts.com/shared 167.99.251.41(rw,sync,no_root_squash,fsid=0) 46.101.159.236(rw,sync,no_root_squash,fsid=0) 127.0.0.1(rw,sync,no_root_squash,fsid=0)

You need to change the data according to your IP addresses and dirs.


IMPORTANT! I suggest using an internal network with private IPs for NFS and also covering all of that with a firewall for security reasons.

service nfs-kernel-server restart

Now we're setting up NFS server on all servers:

apt-get install nfs-common

To check let's mount the dir /srv/nfs-server/wp.thedockerexperts.com/shared with previously added content of wp-content:

mount -t nfs -o proto=tcp,port=2049 :/ /mnt
ls -la /mnt/
total 12
drwxr-xr-x 3 root root 4096 Apr 22 19:45 .
drwxr-xr-x 23 root root 4096 Apr 22 19:41 ..
drwxr-xr-x 4 www-data www-data 4096 Apr 22 19:48 wp-content

Docker build Build and publish Docker images

To ease the process our company has to prepare a ready-to-go Docker image with NginX and PHP-FPM, which is available on Docker Hub. We'll use it for building images of cronjob container and container with your code.
Directories structure:

├── cron
│   ├── conf
│   │   ├── cron
│   │   │   └── crontab
│   │   └── supervisor
│   │   └── supervisord.conf
│   └── Dockerfile
└── lemp7
├── conf
│   └── nginx
│   └── wp.thedockerexperts.com
├── Dockerfile
└── public_html
├── index.php
├── license.txt
├── readme.html
├── wp-activate.php
├── wp-admin
│   ├── about.php
│   ├── admin-ajax.php
│   ├── admin-footer.php
│   ├── admin-functions.php
│   ├── admin-header.php
│   ├── admin.php
│   ├── admin-post.php
│   ├── async-upload.php
│   ├── comment.php
│   ├── credits.php
│   ├── css
│   │   ├── about.css
│   │   ├── about.min.css
│   │   ├── about-rtl.css
│   │   ├── about-rtl.min.css
│   │   ├── admin-menu.css
│   │   ├── admin-menu.min.css
│   │   ├── admin-menu-rtl.css
│   │   ├── admin-menu-rtl.min.css
│   │   ├── code-editor.css
│   │   ├── code-editor.min.css
│   │   ├── code-editor-rtl.css
│   │   ├── code-editor-rtl.min.css
│   │   ├── color-picker.css
│   │   ├── color-picker.min.css
│   │   ├── color-picker-rtl.css
│   │   ├── color-picker-rtl.min.css
│   │   ├── colors
--More--

Disabling of standard wp-cron:
To disable standard wp-cron and use system one we need to add the next string to wp-config.php file:

define('DISABLE_WP_CRON', true);

NginX Docker Image

We put code of your app to NginX Docker container with PHP-FPM & excluding wp-content and called it CODEBASE:
Content of lemp7/Dockerfile:

FROM dockerexperts/nginx-php7
COPY conf/nginx/wp.thedockerexperts.com /etc/nginx/sites-enabled/
RUN mkdir -p /var/www/wp.thedockerexperts.com
COPY public_html/ /var/www/wp.thedockerexperts.com/public_html/
RUN chown -R www-data:www-data /var/www/wp.thedockerexperts.com/public_html

To configure NginX we use simple config lemp7/conf/nginx/wp.thedockerexperts.com:

upstream php {
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name wp.thedockerexperts.com;
## Your only path reference.
root /var/www/wp.thedockerexperts.com/public_html;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}

Don't forget that you need to change paths in Dockerfile as well as NginX configuration file according to your project's specs.

Docker commands for image building ( you can also read our overview about top docker commands which every expert should know )

cd lemp7

ls -la
total 20
drwxr-xr-x 4 root root 4096 Apr 22 19:58 .
drwxr-xr-x 4 root root 4096 Apr 22 19:51 ..
drwxr-xr-x 3 root root 4096 Apr 22 19:51 conf
-rw-r--r-- 1 root root 286 Apr 22 19:58 Dockerfile
drwxr-xr-x 4 root root 4096 Apr 22 19:57 public_html

docker build .
Sending build context to Docker daemon 24.33MB
Successfully built 16c0c7fef405

Docker commands for image publishing

docker tag 16c0c7fef405 /lemp7:wptest

docker push /lemp7:wptest

CRON image

CRON container will use files of CODEBASE image, so all PHP modules dependencies are followed and your code is in place.
Content of cron/Dockerfile:

FROM /lemp7:wptest
COPY conf/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY conf/cron/crontab /tmp/
RUN crontab /tmp/crontab

Content of cron/conf/supervisor/supervisord.conf:

[supervisord]
nodaemon = true
logfile = /var/log/supervisord.log
logfile_maxbytes = 10MB
pidfile = /var/run/supervisord.pid
[program:rsyslog]
command=rsyslogd -n
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:cron]
command = /usr/sbin/cron -f
autostart = true
autorestart = true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:cron_log]
command = tail -F /var/log/syslog
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile_maxbytes = 10MB
stdout_logfile_backups = 0
[eventlistener:stdout]
command = supervisor_stdout
buffer_size = 100
events = PROCESS_LOG
result_handler = supervisor_stdout:event_handler

Content of cron/conf/cron/crontab:

*/10 * * * * cd /var/www/wp.thedockerexperts.com/public_html; php wp-cron.php &ampampgt; /dev/null 2&ampampgt;&ampampamp;1

Docker commands for image building

cd cron/

docker build .
Successfully built 828412c9b982

Docker commands for image publishing

docker tag 828412c9b982 /lemp7:wpcron

docker push /lemp7:wpcron

Rancher

Create Base Environment

Click on the button Add environment in envs. management, which can be found by following the link in your Rancher: https://rancher.example.com/settings/env

add-rancher-environment

Let's enter the new environment and start adding hosts.

rancher-environment-menu

In your environment, we'll see a notification that before container launch we need to add hosts with compatible Docker versions. You're able to check them by following the link.

Add Hosts

Infrastructure 〉Add Host

We copy the content of the 5th point and add it to the console of our servers. When the installation process of the rancher-agent is over, you'll have to see similar data, as it's shown on the below screenshot:

successfully-added-hosts-to-rancher

Connect Private Registry

Infrastructure 〉Registry 〉Add registry

add-private-registry-rancher

Stacks and services

Now when everything is ready, we can start adding services to Rancher.

NFS Rancher

Go to environment 〉Catalog - let's use the search to find NFS.
Click on `View Details` and fill in the fields:

NFS Server: &LTnfs-server-IP>
Export Base Directory: /
On Remove: Retain

We'll see the next picture after the launch:

successfully-added-nfs-service

Now we're adding volume. Let's proceed in Infrastructure 〉Storage 〉Add Volume:

Name: wp-content

MyScalableWP stack

We need to add stack, where we'll launch our services. Keep in mind that wp-content needs to be connected from NFS.

add-new-stack-to-rancher

Service configuration

add-core-service-to-rancher

Volumes configuration

mount-nfs-volume-to-service-container

MySQL External Service

Let's add MySQL as an external service for convenience:

mysql-external-service

Load balancing

We need to schedule Load Balancer to 2 hosts, where we add balancer=true labels. This menu is available while editing hosts.

Load Balancer service

load-balancer-in-rancher

DNS records

nslookup wp.thedockerexperts.com
Server: 67.207.67.3
Address: 67.207.67.3#53
Non-authoritative answer:
Name: wp.thedockerexperts.com
Address: 167.99.251.41
Name: wp.thedockerexperts.com
Address: 46.101.159.236

CRON service

To deploy cron container we need to add new service:

cron-service-in-rancher

Volumes

cron-service-volumes

Testing

Let's try to install W3 Total Cache WP plugin:

test-installation-of-wp-plugin

We see that plugin was installed in shared dir:

root@thedockerexperts-wp-01:~/mywp/lemp7# ls -la /srv/nfs-server/wp.thedockerexperts.com/shared/wp-content/plugins/
total 52
drwxr-xr-x 4 www-data www-data 4096 Apr 22 20:40 .
drwxr-xr-x 5 www-data www-data 4096 Apr 22 20:40 ..
drwxr-xr-x 4 www-data www-data 4096 Apr 3 20:19 akismet
-rw-r--r-- 1 www-data www-data 2230 Mar 17 20:27 hello.php
-rw-r--r-- 1 www-data www-data 28 Jun 5 2014 index.php
drwxr-xr-x 9 www-data www-data 28672 Apr 22 20:40 w3-total-cache

Let's try to add one more host.
Don't forget that we need to update /etc/exports file on the NFS server and add new host.

cluster-scaling-with-test-host

Conclusion

In this blog post, I tried to show you how you can use Docker for scaling of your PHP project in high available cluster. WordPress was used just for example's clear visibility. However something is not considered in this article and you should always remember that when you're ready to scale your project.
1. NFS is a bottleneck in this case. And for HA infrastructure it doesn't fit really well, because if NFS server goes down your whole project will be down. Also, the speed of NFS doesn't always fit for production mode - consider CDN to serve static content. Alternative for NFS is GLusterFS, it's more appropriate for HA but the data transfer speed won't be really high if we're comparing it with regular local disks.
2. Take into consideration where your sessions are stored. In WordPress's case, it's not actual, but with other PHP projects, you'll need to put them into shared storage. In my opinion, the best option here would be Redis or a database.
3. Don't forget about the size of your docker images. Always try to cut your image, so the time spent for its build and deploy is reduced.
I'll be really glad to see any kind of comments, questions or suggestions!

Request more information today

Discover how our services can benefit your business. Leave your contact information and our team will reach out to provide you with detailed information tailored to your specific needs. Take the next step towards achieving your business goals.

Contact form:
Our latest news