0% found this document useful (0 votes)
9 views

PHP Websites using Docker Containers

This document provides a comprehensive guide on setting up PHP websites using Docker containers with Apache and MySQL. It covers the benefits of using Docker, the steps to create a Docker environment, and how to configure PHP and MySQL services, including the use of PHPMyAdmin for database management. Additionally, it compares Docker with VirtualBox, highlighting their differences in performance, security, and application management.

Uploaded by

Balwinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PHP Websites using Docker Containers

This document provides a comprehensive guide on setting up PHP websites using Docker containers with Apache and MySQL. It covers the benefits of using Docker, the steps to create a Docker environment, and how to configure PHP and MySQL services, including the use of PHPMyAdmin for database management. Additionally, it compares Docker with VirtualBox, highlighting their differences in performance, security, and application management.

Uploaded by

Balwinder Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PHP Websites using Docker Containers

with PHP Apache and MySQL

CS307: Systems Practicum Lecture-6


Prof. Varun Dutt, IIT Mandi 9th March 2023
Why containers?
● Makes application development much easier and faster
● Clean architecture that ensures application services utilize resources sustainably by dividing an
application into smaller services called images
● Allows to setup each service independently without affecting other services
● Docker provides a docker-compose file that allows you to set all your application environments
and run a few commands to fully set up your application in a more elegant and faster
approach.
How to use Docker development environment
● Set and test if docker is running
○ After downloading and installing the Docker demon, open the Docker engine and make sure
the engine is running

○ Run “docker version” on command line to verify if Docker is correctly installed on your
computer

-
Laying down docker-compose YML file

We will use Docker hub official images such as PHP Apache and MySQL

- Create a project folder and create a .yml file inside it


- To set a docker-compose, you need first to select the Docker version you want to
use, the services you want to provide, and the containers you want to run.
Setup and run a local PHP Apache server
instance
To set up a PHP Apache container, you need to specify the following environments,

● The container name - this is just a random name that you would like to name your PHP container. For
example container_name: php-apache.
● The container image - this the official PHP image, the version of PHP Apache you want to use. In this
case, we are pulling image: php:8.0-apache from the Docker hub.
● The volume - this will set up your present working src directory for your code/source files. If you
were to run a PHP script, that file would have to be in that directory.

Such as:
Setup and run a local PHP Apache server
instance
● The port numbers. This defines the ports where the script will run from. It will set up an Apache server
port mapping to the port on your local computer.

For example:

This means that we are setting up an Apache server to expose port 80. Port 8000 reaches out to the PHP
scripts and executes them in a browser from within Docker containers.

This is how you docker-compose.yml

should look like.


Setup and run a local PHP Apache server
instance
Let’s test it out. Go ahead and run docker-compose up. That’s going to pull all the information, download the
Apache server, build the image, and run the container.
Setup and run a local PHP Apache server
instance
If you open the Docker desktop engine, the container should be up and running.
Setup and run a local PHP Apache server
instance
To ensure the container is set to execute the PHP scripts, open your defined local host post in the
browser,i.e., http://localhost:8000/.

This shows the container is set to run some PHP-driven code.


Serve a dynamic PHP-driven website
Let’s now execute some PHP code and get the output in the browser. You will be executing the scripts
from the directory that you defined in the volumes of your docker-compose.

In this case we are using ./php/src.

Head on to your project directory ➙ ./php/src, create an index.php file and start writing your PHP
scripts.
Serve a dynamic PHP-driven website
A simple index.php script.

Refresh on your browser (http://localhost:8000/), and the results of this simple PHP drive website
should be visible.

Congratulations! You now have a containerized PHP website.


Setup a MySQL database container
You would probably want to set up a database to interact with your website. We will create another
service to provide MySQL support inside the PHP container. Let’s add the MySQL service into the
docker-compose.yml file. To setup MySQL, we need to customize some environment, such as:

● Password authentication. To use and access a MySQL server, you need to set authentication
environments that will allow you to access the defined MySQL server and its services, such as
a database. We will use MYSQL_USER: MYSQL_USERand MYSQL_PASSWORD:
MYSQL_PASSWORD to connect to MySQL and access the MYSQL_DATABASE:
MYSQL_DATABASE.
● A restart policy set to restart: always. This restarts the service whenever any defined
configuration changes.
Setup a MySQL database container
Setup a MySQL database container
We need to add some MySQL support tools inside the PHP container for the two services (db and
php-apache) to work correctly. This tool includes mysqli.

Inside your project directory, head to the /php folder, create a Docker file, name it Dockerfile and
add the following PHP configurations.

Here we have created a custom PHP Apache image and an environment that will install mysqli, a
PHP extension that will connect the PHP Apache to the MySQL server.
Now we need to build this
custom image inside
php-apache service in the
docker-compose.yml file.
PHP Apache also depends
on the db service to connect
to MySQL. We need to
configure it by specifying a
depends_on: environment.

This is how your


docker-compose.yml file
should look like.
Setup a MySQL database container

Run docker-compose up to pull and set up the MySQL environment. MySQL will be
added to the container.
Run SQL query using PHP scripts
Let’s test if the container is
working as expected. Head
over to the index.php file and
the following PHP MySQL
connection code.
Run SQL query using PHP scripts
Save the file and refresh your http://localhost:8000/web address.

Boom. There you have it. The PHP Apache and MySQL environments are now set, and you
can start developing your PHP-driven application and communicate with the MySQL server.
Setting PHPMyAdmin
● We can fetch some data from a MySQL database
and display it on a web page using PHP scripts.
● Suppose your application interacts with a
database; you would probably want an interface to
interact with your data. We will add PHPMyAdmin
services to provide us with an interface to interact
with the MySQL database.

● To login to the Phpmyadmin panel, use username


as root and password as MYSQL_ROOT_PASSWORD. The
password was already set in the MySQL
environment variables (MYSQL_ROOT_PASSWORD:
MYSQL_ROOT_PASSWORD)
Fetch records and print them on a PHP-driven
website
● Create a database table and fill in some records. Select the database and execute the
following query.
Fetch records and print them on a PHP-driven
website
● Go ahead and write a select SQL query with PHP.
Conclusion

● I hope this lecture helped you set up a PHP and MySQL development
environment using Docker containers.

● Docker is awe-inspiring container technology with a tone of mesmerizing benefits.


VirtualBox
● VirtualBox is a general-purpose full virtualizer for x86 hardware, targeted at server, desktop and
embedded use.
● Oracle VM VirtualBox is a cross-platform virtualization application. What does that mean? For one
thing, it installs on your existing Intel or AMD-based computers, whether they are running Windows,
macOS, Linux, or Oracle Solaris operating systems (OSes). Secondly, it extends the capabilities of your
existing computer so that it can run multiple OSes, inside multiple virtual machines, at the same time.
As an example, you can run Windows and Linux on your Mac, run Windows Server on your Linux
server, run Linux on your Windows PC, and so on, all alongside your existing applications. You can
install and run as many virtual machines as you like. The only practical limits are disk space and
memory.
● Oracle VM VirtualBox is deceptively simple yet also very powerful. It can run everywhere from small
embedded systems or desktop class machines all the way up to datacenter deployments and even
Cloud environments.
How to Install Apple macOS 12 Monterey in Oracle VirtualBox
on Windows 11
Docker (container) versus VirtualBox (Virtual Machine)
In the world of cloud computing, 2 methodologies are always trending: containers and virtual machines. The main benefit of
both Docker and VirtualBox is that they provide a safe DevOps environment. VirtualBox runs multiple applications, whereas
Docker executes only one at a time, but they never fail to communicate with the underlying operating system.

Performance: Docker is more lightweight than VM VirtualBox. Docker containers set certain limits to how many resources it can
allow without the host enabling more resources.

Hypervisor: For VirtualBox, there are many applications attached, and if the hypervisor fails at some point, then all of them will
go down. In the case of Docker, there is no single point of failure, and it doesn’t use a hypervisor, with one exception coming for
the macOS.

Security: VBox has a huge advantage over Docker in terms of security because the virtualization takes place without sharing
the same operating system.

We should also consider size when choosing between Docker and VirtualBox. As mentioned earlier, Docker is lightweight
software, and it is highly portable, so if you want to run single applications on the development platform, Docker will be the
optimal choice. Docker helps fulfill microservices, so it cannot manage large enterprises, where VirtualBox may be more helpful
References
● https://www.google.com/search?q=virtual+box+mac+os+x+12+on+windows&rlz=1C1FKPE_enIN963IN963&sxsrf=AJOqlzXQXd7xZux
Biw9csD2RnmzFKcFGog%3A1677782226762&ei=0uwAZO-OLu6v4-EP_8ie-AE&ved=0ahUKEwjvv4vt8b39AhXu1zgGHX-kBx8Q4dUDC
A8&uact=5&oq=virtual+box+mac+os+x+12+on+windows&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIKCCEQoAEQwwQQCjIKCCEQoAEQww
QQCjIKCCEQoAEQwwQQCjoKCAAQRxDWBBCwAzoFCAAQogRKBAhBGABQ7wdYljlgvjtoAXABeAGAAZ0LiAG_HJIBCTAuOC4zLjctMZgB
AKABAcgBCMABAQ&sclient=gws-wiz-serp#fpstate=ive&vld=cid:8ab43785,vid:WGrBHjDFVIw
● https://www.section.io/engineering-education/dockerized-php-apache-and-mysql-container-development-environment/
● https://www.virtualbox.org/
● https://www.virtualbox.org/manual/ch01.html#virt-why-useful
● https://kuberty.io/blog/docker-vs-virtualbox/#:~:text=The%20main%20benefit%20of%20both,with%20the%20underlying%20opera
ting%20system
Thank You

You might also like