DEV Community

Cover image for Deploy a Containerized PHP Web Application with Docker
Israel .O. Ayanda
Israel .O. Ayanda

Posted on • Edited on

Deploy a Containerized PHP Web Application with Docker

In this project, you will be deploying a simple PHP-based containerized solution backed by a MySQL database application using Docker.

Docker is an open source platform for shipping, developing and running application on any OS running a docker engine. It is fast, takes less space than VMs and can be distributed or shipped as a Docker image.

A quick 2 minutes read about Docker Container, Docker Image & Dockerfile

Prerequiste

  • Docker is installed on your ubuntu instance.
  • Basic understanding of docker and containers.
  • Basic Linux understanding will be helpful.
  • AWS free tier here

Let's Begin.

MySQL in Container

Let us start assembling the application from the backend Database layer – you will use a pre-built MySQL database container, configure it, and make sure it is ready to receive requests from the frontend PHP application.

Step 1: Pull MySQL Docker Image from Docker Hub Registry

In the termainal,

# Search available MySQL image in the docker hub registry

 docker search mysql-server
Enter fullscreen mode Exit fullscreen mode

docker search

Next, you will pull the first on the list, which is the official and latest version and stored in the docker build cache locally.

# Download docker image locally from docker hub
docker pull mysql/mysql-server:latest
Enter fullscreen mode Exit fullscreen mode

docker image pull

You made this pull to make the container creation process faster. Otherwise, skip step one and move to step two, which does the something.

Step 2: Deploy the MySQL Container to your Docker Engine

Once you have the docker image, move on to deploy a new MySQL container

# Create a MySQL container
docker run --name=mysqldb -e MYSQL_ROOT_PASSWORD=dontusethisinprod -d mysql/mysql-server:latest


# List all running containers
docker ps -a
Enter fullscreen mode Exit fullscreen mode

Mysql container

Connecting to the MySQL Docker Container

Now, let's connect to the MySQL container directly

First Method

# Connect to the MySQL database and enter the from the initial step.

docker exec -it mysqldb mysql -uroot -p

# Exit the MySQL mode
exit
Enter fullscreen mode Exit fullscreen mode

Flags

  • Database name = mysqldb
  • Username = -u
  • Password = -p
  • Interactive mode -it

mysql

You are going to use the second method below, so go ahead remove this container.

docker rm -f mysqldb
Enter fullscreen mode Exit fullscreen mode

Second Method

After connecting to the MySQL container, you could go on can configure the schema and prepare it for the Frontend PHP application but this means you will be using the default bridge network which is the default way for connection for all containers. However, it better to create our own private network which enable us to control the network cidr.

Let's go ahead and create a network

# Create a new bridge network

docker network create --subnet=172.18.0.0/24 tooling_app_network
Enter fullscreen mode Exit fullscreen mode

private

This time, let us create an environment variable to store the root password:

# Save the password using environment variable
export MYSQL_PW=password

# verify the environment variable is created
echo $MYSQL_PW
Enter fullscreen mode Exit fullscreen mode

If you are using Window OS, run above command in your git bash terminal which comes with visual studio code editor.

private

To avoid name conflict, remember to remove the initial container as stated above. Now, pull the image and run the container, all in one command like this below

docker run --network tooling_app_network -h mysqlserverhost --name=mysql-server -e MYSQL_ROOT_PASSWORD=$MYSQL_PW  -d mysql/mysql-server:latest
Enter fullscreen mode Exit fullscreen mode

Flags used

  • -d runs the container in detached mode
  • --network connects a container to a network
  • -h specifies a hostname

private

It is best practice not to connect to the MySQL server remotely using the root user. Therefore, you will create a SQL script that will create a user you can use to connect remotely.

Create a file and name it create_user.sql and add the below code in the file

 CREATE USER '<username>'@'%' IDENTIFIED BY '<password>'; 
 GRANT ALL PRIVILEGES ON * . * TO '<username>'@'%';
Enter fullscreen mode Exit fullscreen mode

Replace the username and password to your values.

private

Now, run the script to create the new user. Ensure you are in the directory create_user.sql file is located.

docker exec -i mysql-server mysql -uroot -p$MYSQL_PW < create_user.sql
Enter fullscreen mode Exit fullscreen mode

private

Prepare Database Schema

Now, you need to prepare a database schema so that the Tooling application can connect to it.

Clone the Tooling-app repository from here

git clone https://github.com/oayanda/tooling-1
Enter fullscreen mode Exit fullscreen mode

private

You can find the schema in tooling PHP application repo.

ls ~/tooling-1/html/
Enter fullscreen mode Exit fullscreen mode

private

Use the SQL script to create the database and prepare the schema. With the docker exec command, you can execute a command in a running container.

docker exec -i mysql-server mysql -uroot -p$MYSQL_PW < ~/tooling-1/html/tooling_db_schema.sql
Enter fullscreen mode Exit fullscreen mode

private

Next, you need to update the .env file with connection details to the database. The .env file is located in the html ~/tooling/html/

# View the location of the file
ls la ~/tooling/html/
Enter fullscreen mode Exit fullscreen mode

private

Let's update the connection to the database using the vi editor

vi ~/tooling-1/html/.env
Enter fullscreen mode Exit fullscreen mode

private

Flags used:

  • MYSQL_IP mysql ip address "leave as mysqlserverhost"
  • MYSQL_USER mysql username for user export as environment variable
  • MYSQL_PASS mysql password for the user exported as environment varaible
  • MYSQL_DBNAME mysql databse name "toolingdb"

Update the servername, username, password& databasename in db_conn.php file in tooling/html directory

private

Run the Tooling App

You are almost there. Now you need to containerized the Frontend Application as well and then connect it to the MySQL database.

However, as you now know that you need a Dock image to create a Container but you need a Dockerfile to create a Docker image. In the cloned tooling application repo you now have on system is a Dockerfile which you going to used to build the docker image.

Ensure you are inside the directory "tooling" that has the file Dockerfile and build your container.

docker build -t tooling:0.0.1 .
Enter fullscreen mode Exit fullscreen mode

In the above command, we specify a parameter -t, so that the image can be tagged "tooling.0.1" - Also, you have to notice the . at the end. This is important as that tells Docker to locate the Dockerfile in the current directory you are running the command. Otherwise, you would need to specify the absolute path to the Dockerfile

private

Run the container

docker run --network tooling_app_network -p 8085:80 -it tooling:0.0.1 
Enter fullscreen mode Exit fullscreen mode

private

Ensure to allow port 8085 for a TCP connection in your security group.

private

View the login page in browser

private

The default email is test@gmail.com, the password is 12345

Web application Repo from Darey.io

Congratulation!!! You have successfully deployed a containerized web application with MySQL backend on docker.

As always, I look forward to getting your thoughts on this article. Please feel free to leave a comment!

Top comments (3)

Collapse
 
reacthunter0324 profile image
React Hunter

great explains!

Collapse
 
oayanda profile image
Israel .O. Ayanda

Thank you!

Collapse
 
otobong_edoho_7796fec1f41 profile image
Otobong Edoho

Your article is very thorough and clear.
I learnt alot from this.
Well done!!