
Building a Symfony 7.2 Application with Docker, PHP 8.2, and MySQL
In this blog post, we'll walk you through how to set up a Symfony 7.2 application using Docker, PHP 8.2, and MySQL. Docker makes it easier to manage dependencies, run applications in isolated environments, and ensures consistent setups across different machines. We will be using Docker Compose to manage multiple services, including PHP with Apache, MySQL, and Adminer for database management.
Requirnments
- Docker and Docker Compose installed on your machine
- Basic knowledge of Symfony, Docker, and MySQL
Components Installed in docker-compose.yml
This project consists of three main services:
- PHP-Apache: Runs PHP 8.2 with Apache, serving the Symfony application.
- MySQL: A MySQL container to store the database.
- Adminer: A web-based database management tool.
Creating docker-compose.yml file
The docker-compose.yml file configures the services for our application. It defines how each container should behave, including environment variables, volumes, and ports.
Dockerfile for PHP and Apache
The Dockerfile sets up a PHP 8.2 image with Apache and installs the important extensions like pdo_mysql
, intl
, gd
, and opcache
. First, it changes the Apache config so that it points to the Symfony public directory. Then, it installs the necessary PHP extensions like pdo_mysql
, intl
, and gd
, which are needed for Symfony to run properly. Since Symfony needs URL rewriting, the Apache rewrite module is enabled. After that, Composer is installed to manage dependencies, and the Symfony CLI is added to make it easier to work with Symfony projects, helping you create, manage, and deploy your applications more easily.
.env Configuration
The .env file defines the necessary environment variables for the Docker setup. This includes the project name, container names, database credentials, and paths.
Running the Application
Once everything is set up, you can build and start your Docker containers using Docker Compose. Run the following command in your terminal:
This will build the images as defined in the Dockerfile, start the containers, and map the necessary ports (80 for PHP, 3306 for MySQL, and 8080 for Adminer). You can access your Symfony app in the browser at http://localhost and Adminer at http://localhost:8080.