How To Dockerise And Deploy WordPress Applications

0
82
How To Dockerise And Deploy WordPress Applications

Docker-Compose is a command line tool for defining and managing multi-container docker containers as if they were a single service. Compose is written in python and can be installed with the Python pip command. With compose, we can run multiple docker containers just with a single command. It allows you to create a container as a service, great for your development, testing and staging environment.

Install Docker

Before we start will install a docker and docker-compose first.

# curl -fsSL https://get.docker.com | sh

When the installation is done, start docker and add it to start automatically at boot time:

# systemctl start docker
 # systemctl enable docker

Now test your docker installation with the command below:

# docker run hello-world

You will see hello-world from docker.

Install Docker-Compose

Docker-compose is a script written in python, it’s available in the PyPI python repository and can be installed with python pip. So, we need to install python and python pip on our system first.

Install python and python-pip:

# apt-get install -y python python-pip

Next, install docker-compose with the pip command:

# pip install docker-compose

wait for the installation process to finish. Then check the installation with the docker-compose command:

# docker-compose -v

You will get the docker-compose version.

Setup WordPress

For Now, We installed the docker and docker-compose, now will start create and setup the docker-compose environment for our WordPress project.

Will deploy the ‘WordPress’ PHP application with Nginx as the web server, and MariaDB for the MySQL database as docker containers managed by docker-compose. Each application (WordPress, Nginx, and MySQL) will run in its own container.

We need 3 docker images from the docker hub registry for each application.

# mkdir -p wordpress
# cd wordpress/

Next, create a new necessary directory for the project. Just type the commands below:

# touch docker-compose.yml
 # mkdir -p nginx/
 # mkdir -p db/
 # mkdir -p logs/nginx/
 # mkdir -p wordpress/

File and Directory List of the project:

docker-compose.yml: This is the docker-compose configuration file, you must create it when starting new docker-compose project.

nginx/: This directory is used for our additional nginx configuration like the virtual host etc.

db/: The volume/directory for the mysql data. The sql from data ‘/var/lib/mysql’ is mounted to db directory.

logs/: Directory for application log, nginx, mariadb and php-fpm.

wordpress/: All wordpress files will be available in that directory.

In the ‘nginx’ directory, create a new configuration file for our wordpress virtual host.

Create a new file wordpress.conf with following content.

# vim nginx/wordpress.conf

server {
     listen 80;
     server_name example.com;
  
     root /var/www/html;
     index index.php;
  
     access_log /var/log/nginx/access.log;
     error_log /var/log/nginx/error.log;
  
     location / {
         try_files $uri $uri/ /index.php?$args;
     }
  
     location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass wordpress:9000;
         fastcgi_index index.php;
         include fastcgi_params;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param PATH_INFO $fastcgi_path_info;
     }
 }

Configure Docker-Compose

For now we setup the environment to run a docker-compose file. Now let start write a docker-compose file.

# vim docker-compose.yml

Will start with nginx service container. Here We are using Nginx official docker image, the latest version, and configure port mapping for port 80 on the container to port 80 on the host. Next, configure the docker volumes, the volume for our Nginx virtual host configuration, volume for Nginx log files and the web root directory volume ‘/var/www/html’. The Nginx container is linked to WordPress container.

nginx:
     image: nginx:latest
     ports:
         - '80:80'
     volumes:
         - ./nginx:/etc/nginx/conf.d
         - ./logs/nginx:/var/log/nginx
         - ./wordpress:/var/www/html
     links:
         - wordpress
     restart: always

Next, define the MySQL server. We are using the MariaDB image, latest version. Configure port mapping for the container on port 3306, and configure the MySQL root password with the environment variable ‘MYSQL_ROOT_PASSWORD’. Finally, configure the container volume for the MySQL data directory.

mysql:
     image: mariadb
     ports:
         - '3306:3306'
     volumes:
         - ./db-data:/var/lib/mysql
     environment:
         - MYSQL_ROOT_PASSWORD=*********
     restart: always

Then we will configure the WordPress service by using the WordPress 4.7 docker image with PHP-FPM 7.0 installed. Configure the port for PHP-fpm on port 9000, enable the docker volume for the web directory ‘/var/www/html’ to the host directory ‘wordpress’, setup the database by defining WordPress environment variable, and link the WordPress service to mysql.

wordpress:
     image: wordpress:4.7.1-php7.0-fpm
     ports:
         - '9000:9000'
     volumes:
         - ./wordpress:/var/www/html
     environment:
         - WORDPRESS_DB_NAME=wpdb
         - WORDPRESS_TABLE_PREFIX=wp_
         - WORDPRESS_DB_HOST=mysql
         - WORDPRESS_DB_PASSWORD=aqwe123
     links:
         - mysql
     restart: always

Our docker-compose configuration is ready. Find complete compose file on GitHub

Run Docker-compose

Before we do this step, let’s check the available ports/open ports on the system. Make sure you have 3 ports opened, port 80, 3306 and port 9000.

# netstat -nutlp [Make sure all ports are listening]

Start to create the new containers with docker compose. Go to the compose file location and start run the file which we prepared.

# cd wordpress/
 # docker-compose up -d

To check the running containers, use “docker-compose ps”

Install WordPress

Now open your web browser and type in the server URL or IP address.

http://serverIP/

You can see the WordPress installation page. Choose your language and click ‘Continue‘.

install wordpressFill in your website details like site title, admin user and password, your email address and then click ‘Install WordPress‘.

success installationYou will be redirected to the ‘WordPress Admin Dashboard‘.

wordpress dashboardAnd this is my WordPress sample post hello world.

wordpress siteWordPress has been installed with docker-compose.

NO COMMENTS