I have managed to resolve the issue. The lab check is incorrectly built. Even after I was being able to connect to the postgres db from the wordpress site, the question check boxes were still showing that the link with the db was not established.
So, first things first. It might be very frustrating to know that postgres is not a supported db for wordpress. However, there are workarounds to configure the wordpress application to connect to the postgres db. I spent not hours but quite a few days to figure the whole solution out.
The following areas need your attention:
A) The wordpress image (wordpress:latest) is being used in this case. While the wordpress container is running, the entire wordpress content is located under /var/www/html/ within the container.
B) The wp-config.php file under /var/www/html is the configuration file which will need to be modified with the “name of the db (DB_NAME)”, “password for the db (DB_PASSWORD)”, “user of the db (DB_USER)” and “db hostname or IP address along with port details (DB_HOST)”.
For example, I used: DB_NAME=postgres, DB_PASSWORD=postgres, DB_USER=postgres. All of these are default postgres db elements. If you want to change them then you will have to go into the postgres db container and create a new database and specific users and generate passwords for it. Then you will have to come back to the wp-config.php file to update that. The DB_HOST=<the ip address found within the db container after using “cat /etc/hosts” followed by :5432>. So it looks like DB_HOST=:
C) Wordpress is developed in python. However, there was no driver developed for Wordpress to connect to the db. There is a wordaround to this though. There is a special plugin enabling Wordpress to be used with a PostgreSQL database has been developed by kevinoid. This can be found in the below link:
A temporary fork of https://wordpress.org/plugins/postgresql-for-wordpress/ with changes from kevinoid. - kevinoid/postgresql-for-wordpress
The above github link will need to be cloned into the running Wordpress container under folder /var/www/html/wp-content. The folder “PG4WP” under “postgresql-for-wordpress” directory will need to be copied directly under /var/www/html/wp-content/. The file “db.php” under “PG4WP”
will also need to be copied under /var/www/html/wp-content.
D) Additionally, there are some python libraries or extensions for PostgreSQL which will also need to be downloaded and installed within the Wordpress container. These are as follows:
2) PostgreSQL DB:
PostgreSQL container will be deployed as usual with the password environment variable POSTGRES_PASSWORD=postgres with the help of docker-compose command.
3) Finally, a WordPress image will need to be created with the additional details above and then docker-compose will be used to deploy it.
A) I am adding my Dockerfile details which I used to create the new Wordpress image which was then used to deploy the Wordpress container.
# Updating container, installing git, installing vim (editor), installing libpq-dev, pdo, pdo_pgsql, pgsql extentions. (This line can be removed)
RUN apt-get update
&& apt-get install git -y
&& apt-get install vim -y
&& apt-get install libpq-dev -y
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql
&& docker-php-ext-install pdo pdo_pgsql pgsql
# Creating a directory under /home called wp and git cloning kevinoid’s wordpress plugin for postgress. (This line can be removed)
RUN mkdir /home/wp
&& cd /home/wp
&& git clone https://github.com/kevinoid/postgresql-for-wordpress.git \
B) The docker image will be built with the following command run from the same directory where the Dockerfile is located:
docker build . -t wordpress:new
C) After the image is created the following docker-compose will need to be deployed to run the containers. The following is my docker-compose.yml file.
D) Once both postgres db and wordpress containers are up and running then use the following commands to log on to the wordpress container.
docker ps #To note the container ids.
docker exec -it (wordpress container id) bash
cp postgresql-for-wordpress/pg4wp /var/www/html/wp-content/pg4wp -r #copy pg4wp to wp-content folder.
cp /var/www/html/wp-content/pg4wp/db.php /var/www/html/wp-content/db.php #copy pg4wp/db.php to wp-content folder. I tried to copy this while building the image but then I realised that when I run the container with docker-compose.yml the wp-content folder is overwritten by apache2 with a new set of wordpress files. I am not sure why. So, I thought of doing this after the container was running.
vi /var/www/html/wp-config.php # Here edit the DB_HOST, DB_USER, DB_PASSWORD & DB_NAME with the following values:
DB_HOST= (as discussed earlier this will need to be obtained by connecting to db running container the same way wordpress is connected. E.g. docker exec -it (db container id) bash. Then by running command : cat /etc/hosts.). This will be followed by port number 5432.
Save the file and exit out of the wordpress container.
Once done, you will be able to access the wordpress site and it will be able to connect to the database.
This however, will still not allow the KodeKloud quiz to check the connectivity of the db from wordpress container and acknowledge that.
Hope this helps. Thanks for reading.
PS: This post does not maintain the indent of the Dockerfile or the docker-compose.yml. This will need to be checked seperately.