Dockerizing a PHP application with a MySQL database is similar to the general process of dockerizing an application, but with a few additional steps to set up the database. Here are the steps to dockerize a PHP application with a MySQL database:
- Install Docker on your development machine, if it is not already installed.
- Create a
Dockerfile
in the root of your PHP application directory. This file will contain the instructions for building the PHP application's Docker image. - In the
Dockerfile
, specify the base image that your PHP application will be built on. For example, you might use a base image ofphp:7.4-apache
. - In the
Dockerfile
, copy the PHP application code and its dependencies into the image. For example, you might copy thecomposer.json
file and runcomposer install
to install the dependencies. - In the
Dockerfile
, specify the command to run the PHP application. For example, you might useCMD ["apachectl", "-D", "FOREGROUND"]
. - Build the PHP application's Docker image using the command
docker build -t php_image_name .
- Create a
docker-compose.yml
file in the root of your application directory. This file will be used to start the PHP application and the MySQL database in separate containers. - In the
docker-compose.yml
file, specify the PHP application's image and the ports to map to the host. Also specify the environment variables such as database name, username and password - In the
docker-compose.yml
file, specify the MySQL image, the ports to map to the host, and the environment variables for the database such as the root password. - Start the application and the database using
docker-compose up -d
- Once the containers are running, you can check the logs of the running containers using
docker-compose logs
command. - Once the application is running in the container, you can test it by accessing it through the specified host and port.
Note: This is a high-level overview of the process and it may vary depending on the specific requirements and the type of your application.
By using this process, you'll have your PHP application and its MySQL database running in separate containers, making it easy to scale and manage each component independently. Additionally, this will make it easy to deploy the application in any environment that has Docker support.
Here's an example of a Dockerfile
for a PHP application:
FROM php:7.4-apache COPY . /var/www/html RUN apt-get update && \ apt-get install -y libpq-dev && \ docker-php-ext-install pdo pdo_mysql CMD ["apachectl", "-D", "FOREGROUND"]
This Dockerfile
uses the php:7.4-apache
base image and copies the application code into the /var/www/html
directory in the image. It then installs the libpq-dev
package and the pdo
and pdo_mysql
PHP extensions. The CMD
instruction specifies the command to run the Apache web server.
Here's an example of a docker-compose.yml
file that uses the above Dockerfile
and starts a MySQL database along with the PHP application:
version: '3' services: php-app: build: . ports: - "80:80" environment: - DB_HOST=db - DB_NAME=mydb - DB_USER=myuser - DB_PASSWORD=mypassword db: image: mysql:8.0 ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=myrootpassword - MYSQL_DATABASE=mydb - MYSQL_USER=myuser - MYSQL_PASSWORD=mypassword
In this docker-compose.yml
file, we have two services php-app
and db
. The php-app
service uses the Dockerfile
in the current directory to build the image and maps port 80 in the container to port 80 on the host. It also specifies the environment variables that the PHP application needs to connect to the MySQL database. The db
service uses the mysql:8.0
image and maps port 3306 in the container to port 3306 on the host. It also specifies the environment variables for the root password and the database name, username and password.
With these files, you can build and run the PHP application and its MySQL database using the command docker-compose up -d
.
Please note that this is a simple example for demonstration purpose and you might need to modify it based on your specific requirements and your application's structure.