Dockerize your PHP and PHPMyAdmin app

Levid_ToPast
2 min readSep 7, 2022

--

http://blog.adnansiddiqi.me/wp-content/uploads/2018/12/docker_php_mysql_logo.png

Hi, in this post I will show you how to put your PHP application in a docker container.

If you are making an application with PHP and using MySQL as database, I am pretty sure you are using XAMPP as local web server, if this is the case this is a much better and trouble free alternative.

Before continuing with the post it is necessary as a pre-requisite to have docker and docker-compose installed.

Follow docker official site for more information: https://docs.docker.com/engine/install/
https://docs.docker.com/compose/install/

Configuration

docker-compose.yaml

In this code, three services with the names php, phpmyadmin and mysql are defined these three will connect to run the final application.

‘build’ defines the location of the Dockerfile of the respected service.

‘volume’ mounts the project directory as a volume on the container at /var/www/html. A log directory is also mounted in the respective directory.

The php container exposes the port 8000 of the machine and binds to the port 80 of the container. This will allow access to the application

environment’ is used to inject environment variables.

Remember that I could force Docker to build a container before another by specifying the depends on option. However, in this case, the mysql container would be built first as the php container depends on it.

Dockerfile

In Dockerfile I only use de php official image and copy the source file to the volume. Remember the Dockerfile is used in docker-compose.yaml with php service

Now once the Dockerfile and docker-compose are configured, we compose up the container.

docker-comose up -d --build

We wait for all the configurations to finish, once the process is finished we go to the following link

Note: make sure you have a index.php in the directory

localhost:8000

index.php view

localhost:8080

phpMyAdmin view

With this we can see that our services are working correctly and that they are hosted on the ports we designated.

Thank you very much for reading this post, I hope this post has helped you to manage your php applications in a better and easier way.

--

--

Levid_ToPast

Computer science student , Python developer and Data Science rookie