PHP Websites using Docker Containers
PHP Websites using Docker Containers
○ 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
● 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.
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.
● 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.
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.
● I hope this lecture helped you set up a PHP and MySQL development
environment using Docker containers.
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