WordPress + MySQL with Isolated Network Deployment in 30 Seconds with Docker Swarm Mode

WordPress is using as a blog CMS for a long time. As a result, it is a huge project and it can be classified as monolithic WEB application. Due to this reason, its deployment could be a little bit different than new fashion deployments with microservices. In this post, we will see how to deploy a WordPress application with a brand new MySQL server(actually, we will use MariaDB) and a proxy in front of them. There are some network configuration which allows us the change of restricting access to database engine outside the system and even inside the system, only WordPress containers can reach the database.

We will employ Docker Swarm Mode features here. Also, we will use Docker stack which allows us to deploy application stacks with YAML files instead of seperate commands or scripts. This method also makes updates easier due to versions being checked on each deploy and only the updated services are updated on the real system.

In network side, we will use a seperate network for communication between database and our blog system. A network will be placed between WordPress service and proxy service and it will be reachable from proxy service. Only proxy services ports will be open to outside world.

The deployment takes approximately 30 seconds to start and after that time, we should be able to see the language selection screen of WordPress installer. We can than remove all these with one command in about one second.

I will user MariaDB instead of MySQL in this post. If you want to use MySQL, just replace image: mariadb line with image: mysql  and it should work without any more change.

1. Storage

Firstly, we should create a directory in order to store MariaDB’s files. In this way, the files will be available even if we will remove the services. So, just create a directory named mysql_data:

mkdir mysql_data

2. Compose File

Create a compose file (in YAML format). The file defines two services; one of them for database while the other will be employed for WordPress (PHP and Apache Web Server). You may want to change the values as needed. Save this file as stack.yml:

version: '3.1'

    image: wordpress
      - 80:80
      - db_network
      WORDPRESS_DB_HOST: wordpress_db
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: db_parola

    image: mariadb
      - $PWD/mysql_data:/var/lib/mysql
      - db_network
      MYSQL_ROOT_PASSWORD: db_root_parola
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: db_parola

    driver: overlay

3. Deploy the Blog

Finally, the main part is deploying our stack. If you do not activate Swarm Mode on Docker and running with one node, just write this to activate(write your external IP address):

docker swarm init --advertise-addr YOUR_IP

In order to deploy the stack, just write this:

docker stack deploy --compose-file stack.yml myBlog

Now, if your computer or server do not have the images, they will be downloaded and afterwards they will be started immediately. The output should looks like the following:

docker stack deploy --compose-file stack.yml myBlog
Creating network myBlog_db_network
Creating service myBlog_wordpress
Creating service myBlog_wordpress_db

4. Check the Results

At the first stage, check the results from command line:

$ docker stack ps myBlog
ID                  NAME                    IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
vru2ex8faege        myBlog_wordpress_db.1   mariadb             moby                Running             Running 6 seconds ago                       
s4z6rkicluci        myBlog_wordpress.1      wordpress           moby                Running             Running 6 seconds ago

If you go to your website, you should see the results:

Bonus: You should consider that WordPress containers does not have any constant data storage for now; therefore, uploaded files, installed themes and extensions will be removed when a container retires. In order to prevent this, you may be able to add a volume to /var/www/html directory for this service.

comments powered by Disqus