JavaScript Abstract or loose Equality Comparisons

As per ECMA the comparison x == y, where x and y are values which produce true or false. And, such comparison is performed as follow.

Type(x) is same as Type(y), Then    
If Type(x) is Undefined Return True  
If Type(x) is Null Return True  
If Type(x) is Number If x is NaN Return False
  If y is NaN Return False
  If x is same number as y Return True
  If x is +0 & y is -0 Return True
  If x is -0 & y is +0 Return True
  Else Return False  
If Type(x) is String If x & y are exactly same,
– same sequence of character
– same length
– same character in
corresponding positions
Return True
  Else Return False  
If Type(x) is Boolean If x & y are both True or both False Return True
  Else Return False  
If x & y refers to the same object
( for more detail check description below )
Return True  
  Else Return False  


In the case of Type of x & y is different    
If x is Null y is Undefined Return True
If x is Undefined y is Null Return True
If Type(x) is Number Type(y) is String Return the Result of comparison x == ToNumber(y)
If Type(x) is String Type(y) is Number Return the Result of comparison ToNumber(x) == y
If Type(x) is Boolean Type(y) is any Return the Result of comparison ToNumber(x) == y
If Type(x) is any Type(y) is Boolean Return the Result of comparison x == ToNumber(y)
If Type(x) is either String or Number Type(y) is Object Return the Result of comparison x == ToPrimitive(y)
Type(x) is Object If Type(y) is either String or Number Return the Result of comparison ToPrimitive(x) == y
  • For all other cases, it will Return False

Here some things to keep in note that ToNumber stands for coercing/typecasting value to a number. So in the case of Type(x) is boolean and y is any value, the JS will perform implicit coercion on value of y to make it comparable with x.

ToPrimitive stands for typecasting non-primitive value ( eg. Object including function and array ) to a primitive value ( eg. string, number, etc ).

In the case of x & y refers to the same object only returns true else return false means lets suppose,

var a = [1,2,3];
var b = [1,2,3];
var c = 1,2,3;

a == c // true.
b == c // true.
a == b // false.

In the case of a == c, a is coerced to string as c is a string and both values are same (because while coercing array to string it return a comma separated values) so returns true. and same for b == c.

While comparing a == b, coercing won’t happen as the type on both side is same. The reason it will return false is, JS now don’t check the value of the object instead it will check its reference. Even though the value of a and b is the same, but because of different reference, it will return false.

It can be true in the case of,

var a = [1,2,3];
var b = a;

a == b // true.

As per ECMA the comparison x == y, where x and y are values which produce true or false. And, such comparison is performed as follow.

Type(x) is same as Type(y), Then    
If Type(x) is Undefined Return True  
If Type(x) is Null Return True  
If Type(x) is Number If x is NaN Return False
  If y is NaN Return False
  If x is same number as y Return True
  If x is +0 & y is -0 Return True
  If x is -0 & y is +0 Return True
  Else Return False  
If Type(x) is String If x & y are exactly same,
– same sequence of character
– same length
– same character in
corresponding positions
Return True
  Else Return False  
If Type(x) is Boolean If x & y are both True or both False Return True
  Else Return False  
If x & y refers to the same object
( for more detail check description below )
Return True  
  Else Return False  


In the case of Type of x & y is different    
If x is Null y is Undefined Return True
If x is Undefined y is Null Return True
If Type(x) is Number Type(y) is String Return the Result of comparison x == ToNumber(y)
If Type(x) is String Type(y) is Number Return the Result of comparison ToNumber(x) == y
If Type(x) is Boolean Type(y) is any Return the Result of comparison ToNumber(x) == y
If Type(x) is any Type(y) is Boolean Return the Result of comparison x == ToNumber(y)
If Type(x) is either String or Number Type(y) is Object Return the Result of comparison x == ToPrimitive(y)
Type(x) is Object If Type(y) is either String or Number Return the Result of comparison ToPrimitive(x) == y
  • For all other cases, it will Return False

Here some things to keep in note that ToNumber stands for coercing/typecasting value to a number. So in the case of Type(x) is boolean and y is any value, the JS will perform implicit coercion on value of y to make it comparable with x.

ToPrimitive stands for typecasting non-primitive value ( eg. Object including function and array ) to a primitive value ( eg. string, number, etc ).

In the case of x & y refers to the same object only returns true else return false means lets suppose,

var a = [1,2,3];
var b = [1,2,3];
var c = 1,2,3;

a == c // true.
b == c // true.
a == b // false.

In the case of a == c, a is coerced to string as c is a string and both values are same (because while coercing array to string it return a comma separated values) so returns true. and same for b == c.

While comparing a == b, coercing won’t happen as the type on both side is same. The reason it will return false is, JS now don’t check the value of the object instead it will check its reference. Even though the value of a and b is the same, but because of different reference, it will return false.

It can be true in the case of,

var a = [1,2,3];
var b = a;

a == b // true.

Create custom menu page with settings section and field

Creating custom settings with multiple modules might be confusing because WordPress documents can’t add all examples for our requirements.

Here is the snippet of creating custom menu page.

Creating custom settings with multiple modules might be confusing because WordPress documents can’t add all examples for our requirements.

Here is the snippet of creating custom menu page.

Php Singleton Trait and using it in a Class

Singleton is an OOP concept which is used to support instantiating class only once. So basically it just uses already created instance of the class which is stored in a static variable.

Traits are one of the ways of inheritance in PHP for more detail check Traits.

Code Snippet:

This is how you can create a Trait and use it in a class.

Some points to keep in a note. In Trait, the constructor is protected so one can’t create an instance of the class by just calling new SingletonClass(). It will give a fatal error.

PHP Fatal error:  Uncaught Error: Call to protected SingletonClass::__construct() from invalid context

Singleton is an OOP concept which is used to support instantiating class only once. So basically it just uses already created instance of the class which is stored in a static variable.

Traits are one of the ways of inheritance in PHP for more detail check Traits.

Code Snippet:

This is how you can create a Trait and use it in a class.

Some points to keep in a note. In Trait, the constructor is protected so one can’t create an instance of the class by just calling new SingletonClass(). It will give a fatal error.

PHP Fatal error:  Uncaught Error: Call to protected SingletonClass::__construct() from invalid context

WordPress Protected Embed Plugin

WordPress plugin to add protected embed, especially for VIP Go environment.

The plugin source code is available here protected_embed.

To install the plugin clone the plugin repo in wp_content -> plugin folder.

git clone [email protected]:vishaldodiya/protected-embeds.git

Enable plugin from WordPress wp_admin plugins page.

To add protected embed on post follow this steps.

Step 1: Create a new post. Click on Add Media button.

Step 2: Click on Protected Embed Menu.

Step 3: Now we have one textarea in which we need to add embed code and click Add to Post button.

Step 4: After adding embed in the post, one shortcode will be generated and added to the editor. Now protected embed is ready.

WordPress plugin to add protected embed, especially for VIP Go environment.

The plugin source code is available here protected_embed.

To install the plugin clone the plugin repo in wp_content -> plugin folder.

git clone [email protected]:vishaldodiya/protected-embeds.git

Enable plugin from WordPress wp_admin plugins page.

To add protected embed on post follow this steps.

Step 1: Create a new post. Click on Add Media button.

Step 2: Click on Protected Embed Menu.

Step 3: Now we have one textarea in which we need to add embed code and click Add to Post button.

Step 4: After adding embed in the post, one shortcode will be generated and added to the editor. Now protected embed is ready.

GCP instance DNS configuration steps

In the last post, we configured our WP_Docker setup on GCP. Now In this section, we will cover the DNS configuration on GCP.

</figure>

First, go to GCP side menu bar > Network Services > Cloud DNS. After visiting that we can see the screen shown below.

</figure>

Now click on Create Zone and another form will appear like shown below.

</figure>

In this form, we need to fill zone name which can be anything, then DNS name, which will be your domain name and then we are good to go. Click on create.

</figure>

As we can see It will generate Nameserver records. These nameserver records are used in your domain registrars nameserver records.

</figure>

Now, we need to create two more records to point our DNS to the server. As you can see in the image after clicking add new record button we can see this page. Fill up the required details DNS name will be your domain name, First, we are generating an entry for A type record so set Resource record type to A and add your Instance IP ( As we have generated in the last post ) in server IP box and click Create. The second record will be CNAME record, so set Resource record type as CNAME and add your domain name with canonical URL www.domain.com in the canonical name section and click create. Now these two A and CNAME records are needs to be added on your domain registrars DNS records as well. After doing that we have completed our DNS configuration and now you can check that your domain will point to your server.

In the last post, we configured our WP_Docker setup on GCP. Now In this section, we will cover the DNS configuration on GCP.

</figure>

First, go to GCP side menu bar > Network Services > Cloud DNS. After visiting that we can see the screen shown below.

</figure>

Now click on Create Zone and another form will appear like shown below.

</figure>

In this form, we need to fill zone name which can be anything, then DNS name, which will be your domain name and then we are good to go. Click on create.

</figure>

As we can see It will generate Nameserver records. These nameserver records are used in your domain registrars nameserver records.

</figure>

Now, we need to create two more records to point our DNS to the server. As you can see in the image after clicking add new record button we can see this page. Fill up the required details DNS name will be your domain name, First, we are generating an entry for A type record so set Resource record type to A and add your Instance IP ( As we have generated in the last post ) in server IP box and click Create. The second record will be CNAME record, so set Resource record type as CNAME and add your domain name with canonical URL www.domain.com in the canonical name section and click create. Now these two A and CNAME records are needs to be added on your domain registrars DNS records as well. After doing that we have completed our DNS configuration and now you can check that your domain will point to your server.

Host WordPress site on GCP with Docker

Hey There. Now we are moving ahead with our set up of the WordPress site. In this Post, we will cover the part of server configurations. We will be configuring our server on GCP (Google Cloud Platform) Using Docker Compose configuration we have done in previous posts. The post will mostly focus on GCP overview and configuration of the instance and install dependencies for installing WP_Docker.

To configure an instance on GCP we need GCP access. One can use GCP’s 12 Month trial version for beginning and experiments or can check GCP plans as per the services. After getting GCP console we are ready to set up one VM instance.

To create VM instance open GCP > Compute Engine > vm instance.

To create new instance click Create Instance and you need to fill up configuration details.

Fill up the details like instance name, region etc. One can change VM boot disk by clicking Change in book disk box. There will be list of Boot disks like shown below. We will use Ubuntu 16.04 LTS.

Select Ubuntu 16.04 LTS and click Select.

Click Create to initialize the instance. The instance will be started in few minutes and one can check the instance status like below.

Now our instance is ready to configure WP_Docker and install dependencies. To access VM instance click ssh to login to ssh into the instance. It will open a popup menu and give access to the instance.

Now we are inside out instance so we need to install few dependencies because instance does not have docker and docker-compose pre-installed. To install docker check follow this steps. After installing docker install Docker-compose by following this steps.

Confirm Docker and docker-compose by running below commands.

$ docker -v
$ docker-compose -v

If everything is Ok then we can move to next step to install over WP_Docker.

Run this series of commands.

$ git clone [email protected]:vishaldodiya/WP_Docker.git

( this will need ssh setup on GitHub or you can use Http link )

$ cd WP_Docker
$ cp .env.example .env

in .env file one can change a MySQL password, database name and virtual hostname that will be required for configuration. Now we are good to run the setup file.

$ bash setup.sh

After successfully running all this commands our all docker containers will be started. Now to access our site and access from the domain name and DNS configurations will be covered in next post.

Hey There. Now we are moving ahead with our set up of the WordPress site. In this Post, we will cover the part of server configurations. We will be configuring our server on GCP (Google Cloud Platform) Using Docker Compose configuration we have done in previous posts. The post will mostly focus on GCP overview and configuration of the instance and install dependencies for installing WP_Docker.

To configure an instance on GCP we need GCP access. One can use GCP’s 12 Month trial version for beginning and experiments or can check GCP plans as per the services. After getting GCP console we are ready to set up one VM instance.

To create VM instance open GCP > Compute Engine > vm instance.

To create new instance click Create Instance and you need to fill up configuration details.

Fill up the details like instance name, region etc. One can change VM boot disk by clicking Change in book disk box. There will be list of Boot disks like shown below. We will use Ubuntu 16.04 LTS.

Select Ubuntu 16.04 LTS and click Select.

Click Create to initialize the instance. The instance will be started in few minutes and one can check the instance status like below.

Now our instance is ready to configure WP_Docker and install dependencies. To access VM instance click ssh to login to ssh into the instance. It will open a popup menu and give access to the instance.

Now we are inside out instance so we need to install few dependencies because instance does not have docker and docker-compose pre-installed. To install docker check follow this steps. After installing docker install Docker-compose by following this steps.

Confirm Docker and docker-compose by running below commands.

$ docker -v
$ docker-compose -v

If everything is Ok then we can move to next step to install over WP_Docker.

Run this series of commands.

$ git clone [email protected]:vishaldodiya/WP_Docker.git

( this will need ssh setup on GitHub or you can use Http link )

$ cd WP_Docker
$ cp .env.example .env

in .env file one can change a MySQL password, database name and virtual hostname that will be required for configuration. Now we are good to run the setup file.

$ bash setup.sh

After successfully running all this commands our all docker containers will be started. Now to access our site and access from the domain name and DNS configurations will be covered in next post.

Setup WordPress site using docker compose – Part 3

We have come to last part of setting up WordPress using docker compose. In this part, we will cover how to run all containers to get a fully working site. If you missed the last two parts ( Part 1, Part 2 ) then you can take a look to get more detail. For GitHub Repo check WP_Docker. I have created one setup file in very GitHub repo which includes series of commands to start WordPress site using docker compose.

export $(xargs <.env)

docker network create $VIRTUAL_HOST

docker run --name nginx-proxy --restart always -d -p 80:80 -p 443:443 \
    -v ~/.ee4/etc/nginx/certs:/etc/nginx/certs \
    -v ~/.ee4/etc/nginx/conf.d:/etc/nginx/conf.d \
    -v ~/.ee4/etc/nginx/htpasswd:/etc/nginx/htpasswd \
    -v ~/.ee4/etc/nginx/vhost.d:/etc/nginx/vhost.d \
    -v ~/.ee4/usr/share/nginx/html:/usr/share/nginx/html \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \
    -v $(pwd)/config/nginx/custom_proxy_settings.conf://etc/nginx/conf.d/custom_proxy_settings.conf \
    jwilder/nginx-proxy

docker run -d --name letsencrypt \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    --volumes-from nginx-proxy \
    jrcs/letsencrypt-nginx-proxy-companion

docker network connect $VIRTUAL_HOST nginx-proxy
docker network connect $VIRTUAL_HOST letsencrypt

docker-compose up -d

docker logs nginx-proxy -f

Let’s take a look at this file ( setup.sh ) and all the series of commands.

export $(xargs <.env)

The first command is to export all the .env file variables into a current shell script so we can use those same variables inside script also.

docker network create $VIRTUAL_HOST

The second command is to create a network inside docker which is needed to attach one service to another docker service means it will be needed to connect all the containers/services to a single network.

docker run --name nginx-proxy --restart always -d -p 80:80 -p 443:443 \
    -v ~/.ee4/etc/nginx/certs:/etc/nginx/certs \
    -v ~/.ee4/etc/nginx/conf.d:/etc/nginx/conf.d \
    -v ~/.ee4/etc/nginx/htpasswd:/etc/nginx/htpasswd \
    -v ~/.ee4/etc/nginx/vhost.d:/etc/nginx/vhost.d \
    -v ~/.ee4/usr/share/nginx/html:/usr/share/nginx/html \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \
    -v $(pwd)/config/nginx/custom_proxy_settings.conf://etc/nginx/conf.d/custom_proxy_settings.conf \
    jwilder/nginx-proxy

This command will create Nginx Reverse Proxy container. We have not included this container inside docker-compose because we are not going to stop this container and it can be used with other Nginx instances (for more detail check nginx-proxy ). Here one configuration file is included custom_proxy_settings.conf which is needed for some custom configuration on the server. So I came across with one error while uploading images in wp-admin having a size of more than 2MB. The error is about the file upload limit but in Nginx configuration, we have a parameter for upload file size ( client_max_body_size 100m; ) but that configuration was not affecting because nginx-proxy was running and nginx-proxy creates its own configuration file for the current server. So to bypass this error we can include configuration like this parameter ( client_max_body_size 100m; ) in conf file to override default configurations.

docker run -d --name letsencrypt \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    --volumes-from nginx-proxy \
    jrcs/letsencrypt-nginx-proxy-companion

This command is an optional command. Its used to create SSL certificate for our site, it takes volumes from nginx-proxy and creates certificates as per the configurations in nginx-proxy. For more detail check Letsencrypt.

docker network connect $VIRTUAL_HOST nginx-proxy
docker network connect $VIRTUAL_HOST letsencrypt

docker-compose up -d

docker logs nginx-proxy -f

These are the last commands. First, 2 commands will connect nginx-proxy and letsencrypt container with docker network. The third command will run docker compose configuration means run all containers which are configured in docker-compose.yml file and the last command is to display logs.

So now we can run all this command by typing this command in terminal.

bash setup.sh

It will create all the services and containers and connect everything and we are ready to access our WordPress site.

We have come to last part of setting up WordPress using docker compose. In this part, we will cover how to run all containers to get a fully working site. If you missed the last two parts ( Part 1, Part 2 ) then you can take a look to get more detail. For GitHub Repo check WP_Docker. I have created one setup file in very GitHub repo which includes series of commands to start WordPress site using docker compose.

export $(xargs <.env)

docker network create $VIRTUAL_HOST

docker run --name nginx-proxy --restart always -d -p 80:80 -p 443:443 \
    -v ~/.ee4/etc/nginx/certs:/etc/nginx/certs \
    -v ~/.ee4/etc/nginx/conf.d:/etc/nginx/conf.d \
    -v ~/.ee4/etc/nginx/htpasswd:/etc/nginx/htpasswd \
    -v ~/.ee4/etc/nginx/vhost.d:/etc/nginx/vhost.d \
    -v ~/.ee4/usr/share/nginx/html:/usr/share/nginx/html \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \
    -v $(pwd)/config/nginx/custom_proxy_settings.conf://etc/nginx/conf.d/custom_proxy_settings.conf \
    jwilder/nginx-proxy

docker run -d --name letsencrypt \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    --volumes-from nginx-proxy \
    jrcs/letsencrypt-nginx-proxy-companion

docker network connect $VIRTUAL_HOST nginx-proxy
docker network connect $VIRTUAL_HOST letsencrypt

docker-compose up -d

docker logs nginx-proxy -f

Let’s take a look at this file ( setup.sh ) and all the series of commands.

export $(xargs <.env)

The first command is to export all the .env file variables into a current shell script so we can use those same variables inside script also.

docker network create $VIRTUAL_HOST

The second command is to create a network inside docker which is needed to attach one service to another docker service means it will be needed to connect all the containers/services to a single network.

docker run --name nginx-proxy --restart always -d -p 80:80 -p 443:443 \
    -v ~/.ee4/etc/nginx/certs:/etc/nginx/certs \
    -v ~/.ee4/etc/nginx/conf.d:/etc/nginx/conf.d \
    -v ~/.ee4/etc/nginx/htpasswd:/etc/nginx/htpasswd \
    -v ~/.ee4/etc/nginx/vhost.d:/etc/nginx/vhost.d \
    -v ~/.ee4/usr/share/nginx/html:/usr/share/nginx/html \
    -v /var/run/docker.sock:/tmp/docker.sock:ro \
    -v $(pwd)/config/nginx/custom_proxy_settings.conf://etc/nginx/conf.d/custom_proxy_settings.conf \
    jwilder/nginx-proxy

This command will create Nginx Reverse Proxy container. We have not included this container inside docker-compose because we are not going to stop this container and it can be used with other Nginx instances (for more detail check nginx-proxy ). Here one configuration file is included custom_proxy_settings.conf which is needed for some custom configuration on the server. So I came across with one error while uploading images in wp-admin having a size of more than 2MB. The error is about the file upload limit but in Nginx configuration, we have a parameter for upload file size ( client_max_body_size 100m; ) but that configuration was not affecting because nginx-proxy was running and nginx-proxy creates its own configuration file for the current server. So to bypass this error we can include configuration like this parameter ( client_max_body_size 100m; ) in conf file to override default configurations.

docker run -d --name letsencrypt \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    --volumes-from nginx-proxy \
    jrcs/letsencrypt-nginx-proxy-companion

This command is an optional command. Its used to create SSL certificate for our site, it takes volumes from nginx-proxy and creates certificates as per the configurations in nginx-proxy. For more detail check Letsencrypt.

docker network connect $VIRTUAL_HOST nginx-proxy
docker network connect $VIRTUAL_HOST letsencrypt

docker-compose up -d

docker logs nginx-proxy -f

These are the last commands. First, 2 commands will connect nginx-proxy and letsencrypt container with docker network. The third command will run docker compose configuration means run all containers which are configured in docker-compose.yml file and the last command is to display logs.

So now we can run all this command by typing this command in terminal.

bash setup.sh

It will create all the services and containers and connect everything and we are ready to access our WordPress site.

Setup WordPress site using docker compose – Part 2

This is Part 2 of setting up a WordPress site using docker compose. Part 1 covers some details regarding GitHub repository ( WP_Docker ) directory structure. In this part, we will deep dive into Docker Compose file included in Github Repository.

We will check Docker Compose file service by service. There are three main services (Database, PHP, Nginx ) needed to start the WordPress site and Phpmyadmin is optional.

1. Database: 

db:
    image: mysql:5.7
    restart: always
    volumes:
      - "./app/db:/var/lib/mysql"
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD
      - MYSQL_DATABASE
      - MYSQL_USER
      - MYSQL_PASSWORD
    networks:
     - site-network

Here we are using Mysql 5.7 image which the stable one. We have mounted MySQL data to out host directory to make the volume persistent otherwise whenever you restart the container our data will be gone. The port is mapped to use the functionality from the host so we have mapped container 3306 port with Host’s 3306 port. We can customize MySQL configuration using environment variables like root password and database name etc. This environment variables are fetched by docker-compose file from the .env file in the same directory as docker-compose.yml. In WP_Docker I have included one .env.example file.

MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=password

WORDPRESS_DB_HOST=db
VIRTUAL_HOST=example.com

We can specify password or hostname etc for using environment variables in containers. So this is an example file, to use those environment variables we need to move the file to .env named file.

2. PHP:

php:
    build:
      context: ./config/php-fpm/
    links:
      - db:mysql
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - "./app/src:/var/www/html"
      - "./config/php-fpm/php.ini:/usr/local/etc/php/php.ini"
      - "./config/php-fpm/composer.json:/var/www/html/composer.json"
      - "./config/php-fpm/www.conf:/usr/local/etc/php-fpm.d/www.conf"
    environment:
      - WORDPRESS_DB_HOST
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
    networks:
     - site-network

Same as database service It has been mapped to host 9000 port so server (Nginx) can listen to PHP service. We are mapping ./app/src from container’s /var/www/html. Here app/src folder will be created when we start the container means the image we are using rtcamp/wordpress include all WordPress core files in /var/www/html folder and that folder is mapped in host ./app/src folder. The New thing in this container is we are customizing image and building it again, So the docker-compose will look into context directory and look for default file named as Dockerfile and build the file before running the container. Other than that we mounting some configuration files like php.ini file and www.conf files.

3. Nginx:

nginx:
    image: nginx:latest
    depends_on:
      - php
    restart: always
    environment:
      - VIRTUAL_HOST
      - LETSENCRYPT_HOST=${VIRTUAL_HOST}
    volumes:
      - "./app/src:/var/www/html"
      - "./config/nginx/default.conf:/etc/nginx/conf.d/default.conf"
      - "./logs/nginx:/var/log/nginx"
    networks:
     - site-network

Most of the part is same as above two services. In Nginx container we provide VIRTUAL_HOST and LETSENCRYPTY_HOST env variable. These variables are used by other two services like nginx-proxy and letsencrypt. Here we have not mounted any ports because we will be using ports of nginx-proxy. nginx-proxy is a Nginx Reverse Proxy image which can take data from Nginx container and use the services. We will take a look at its configuration while running all the containers.

One parameter is common in all services is the network. so we need to connect all containers to one single network so everyone can access to ports and data.

The next part will cover details to set up and run site configurations.

This is Part 2 of setting up a WordPress site using docker compose. Part 1 covers some details regarding GitHub repository ( WP_Docker ) directory structure. In this part, we will deep dive into Docker Compose file included in Github Repository.

We will check Docker Compose file service by service. There are three main services (Database, PHP, Nginx ) needed to start the WordPress site and Phpmyadmin is optional.

1. Database: 

db:
    image: mysql:5.7
    restart: always
    volumes:
      - "./app/db:/var/lib/mysql"
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD
      - MYSQL_DATABASE
      - MYSQL_USER
      - MYSQL_PASSWORD
    networks:
     - site-network

Here we are using Mysql 5.7 image which the stable one. We have mounted MySQL data to out host directory to make the volume persistent otherwise whenever you restart the container our data will be gone. The port is mapped to use the functionality from the host so we have mapped container 3306 port with Host’s 3306 port. We can customize MySQL configuration using environment variables like root password and database name etc. This environment variables are fetched by docker-compose file from the .env file in the same directory as docker-compose.yml. In WP_Docker I have included one .env.example file.

MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=password

WORDPRESS_DB_HOST=db
VIRTUAL_HOST=example.com

We can specify password or hostname etc for using environment variables in containers. So this is an example file, to use those environment variables we need to move the file to .env named file.

2. PHP:

php:
    build:
      context: ./config/php-fpm/
    links:
      - db:mysql
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - "./app/src:/var/www/html"
      - "./config/php-fpm/php.ini:/usr/local/etc/php/php.ini"
      - "./config/php-fpm/composer.json:/var/www/html/composer.json"
      - "./config/php-fpm/www.conf:/usr/local/etc/php-fpm.d/www.conf"
    environment:
      - WORDPRESS_DB_HOST
      - WORDPRESS_DB_USER=${MYSQL_USER}
      - WORDPRESS_DB_PASSWORD=${MYSQL_PASSWORD}
    networks:
     - site-network

Same as database service It has been mapped to host 9000 port so server (Nginx) can listen to PHP service. We are mapping ./app/src from container’s /var/www/html. Here app/src folder will be created when we start the container means the image we are using rtcamp/wordpress include all WordPress core files in /var/www/html folder and that folder is mapped in host ./app/src folder. The New thing in this container is we are customizing image and building it again, So the docker-compose will look into context directory and look for default file named as Dockerfile and build the file before running the container. Other than that we mounting some configuration files like php.ini file and www.conf files.

3. Nginx:

nginx:
    image: nginx:latest
    depends_on:
      - php
    restart: always
    environment:
      - VIRTUAL_HOST
      - LETSENCRYPT_HOST=${VIRTUAL_HOST}
    volumes:
      - "./app/src:/var/www/html"
      - "./config/nginx/default.conf:/etc/nginx/conf.d/default.conf"
      - "./logs/nginx:/var/log/nginx"
    networks:
     - site-network

Most of the part is same as above two services. In Nginx container we provide VIRTUAL_HOST and LETSENCRYPTY_HOST env variable. These variables are used by other two services like nginx-proxy and letsencrypt. Here we have not mounted any ports because we will be using ports of nginx-proxy. nginx-proxy is a Nginx Reverse Proxy image which can take data from Nginx container and use the services. We will take a look at its configuration while running all the containers.

One parameter is common in all services is the network. so we need to connect all containers to one single network so everyone can access to ports and data.

The next part will cover details to set up and run site configurations.

Setup WordPress site using docker compose

This will cover all steps to set up a full WordPress site using a docker-compose file. Some basic knowledge of docker will be great. We will cover everything in parts so this is the first part of a process. In this part, we will see the directory structure of the current GitHub repo WP_Docker.

.
├── app
├── config
│   ├── nginx
│   │   ├── custom_proxy_settings.conf
│   │   └── default.conf
│   └── php-fpm
│       ├── Dockerfile
│       ├── composer.json
│       ├── php.ini
│       └── www.conf
├── docker-compose.yml
├── logs
│   └── nginx
│       ├── access.log
│       └── error.log
└── setup.sh

First and the most important file is a docker-compose.yml file. So docker-compose yml file is used to define multiple Docker containers and use them together with a single common file. Then we have config folder which contains configurations of Nginx and PHP container.

In Nginx, we have default.conf for server configuration which includes all types of configuration of the site including where the root directory and etc. There is one custom_proxy_settings.conf which is used in a nginx-proxy container we will take a look at it in detail when we are ready to run all container.

In Php-Fpm, We have Dockerfile it has some extra customization over a standard PHP image. So In that Dockerfile, I have used rtcamp/wordpress image. so they have configured PHP image with WordPress and also installs wp-cli. Other than that there is some extra configuration file like php.ini and www.conf to customize PHP configs.

Other two directories app and logs are empty. the data will be mounted into those folders once we start all containers.

So setup.php have series of commands required to run all the containers.

This is the directory structure of repository and we will cover each file and configurations in next part.

This will cover all steps to set up a full WordPress site using a docker-compose file. Some basic knowledge of docker will be great. We will cover everything in parts so this is the first part of a process. In this part, we will see the directory structure of the current GitHub repo WP_Docker.

.
├── app
├── config
│   ├── nginx
│   │   ├── custom_proxy_settings.conf
│   │   └── default.conf
│   └── php-fpm
│       ├── Dockerfile
│       ├── composer.json
│       ├── php.ini
│       └── www.conf
├── docker-compose.yml
├── logs
│   └── nginx
│       ├── access.log
│       └── error.log
└── setup.sh

First and the most important file is a docker-compose.yml file. So docker-compose yml file is used to define multiple Docker containers and use them together with a single common file. Then we have config folder which contains configurations of Nginx and PHP container.

In Nginx, we have default.conf for server configuration which includes all types of configuration of the site including where the root directory and etc. There is one custom_proxy_settings.conf which is used in a nginx-proxy container we will take a look at it in detail when we are ready to run all container.

In Php-Fpm, We have Dockerfile it has some extra customization over a standard PHP image. So In that Dockerfile, I have used rtcamp/wordpress image. so they have configured PHP image with WordPress and also installs wp-cli. Other than that there is some extra configuration file like php.ini and www.conf to customize PHP configs.

Other two directories app and logs are empty. the data will be mounted into those folders once we start all containers.

So setup.php have series of commands required to run all the containers.

This is the directory structure of repository and we will cover each file and configurations in next part.