Setting Up Docker with PHP 8.2: Dockerfile, Compose, and Container Setup
Symfony 7 Application Development with Docker, PHP 8.2, and MySQL

Setting Up Docker with PHP 8.2: Dockerfile, Compose, and Container Setup

How to Set Up Docker for PHP 8.2: Dockerfile, Compose, and Running Your First Container

Author
Richard Mendes
March 06, 2025 • 8 mins

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

  1. Docker and Docker Compose installed on your machine
  2. Basic knowledge of Symfony, Docker, and MySQL

Components Installed in docker-compose.yml

This project consists of three main services:

  1. PHP-Apache: Runs PHP 8.2 with Apache, serving the Symfony application.
  2. MySQL: A MySQL container to store the database.
  3. 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.

version: "3"

services:
php-apache:
container_name: "${CONTAINER_NAME_PREFIX}-apache-php"
build: .
volumes:
- ${PATH_TO_SYMFONY_PROJECT}:/var/www/html
ports:
- "80:80"

db:
container_name: "${CONTAINER_NAME_PREFIX}-mysql"
image: mysql:8.0
restart: always
environment:
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
volumes:
- db:/var/lib/mysql
ports:
- "3306:3306"

adminer:
container_name: "${CONTAINER_NAME_PREFIX}-adminer"
image: adminer
ports:
- "8080:8080"

volumes:
db:

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.

FROM php:8.2-apache

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# INI-Files
COPY ./opcache.ini "$PHP_INI_DIR/conf.d/docker-php-ext-opcache.ini"
COPY ./xdebug.ini "$PHP_INI_DIR/conf.d/99-xdebug.ini"
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

# Install Packages
RUN apt-get -y update && apt-get install -y libicu-dev libzip-dev zip libjpeg-dev libpng-dev libfreetype6-dev git
RUN docker-php-ext-configure intl
RUN docker-php-ext-configure gd '--with-jpeg' '--with-freetype'
RUN docker-php-ext-install intl opcache pdo_mysql zip gd
RUN pecl install xdebug
RUN a2enmod rewrite

# Install APCU
RUN pecl install apcu-5.1.24 && docker-php-ext-enable apcu
RUN echo "extension=apcu.so" > /usr/local/etc/php/php.ini
RUN echo "apc.enable_cli=1" > /usr/local/etc/php/php.ini
RUN echo "apc.enable=1" > /usr/local/etc/php/php.ini

# Install Composer and Symfony CLI
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -sS https://get.symfony.com/cli/installer | bash && mv /root/.symfony5/bin/symfony /usr/local/bin/symfony

# Install NVM
RUN curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

.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.

COMPOSE_PROJECT_NAME="symfonyproject"
CONTAINER_NAME_PREFIX=symfony-project

PATH_TO_SYMFONY_PROJECT='./symfonyapp'

# DATABASE
ROOT_USER=richard
DB_USER=root
DB_PASSWORD=ChangeMe
DB_NAME=test_db

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:

docker-compose up --build

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.



Latest Articles