How to dockerize Symfony project ?
This quickstart guide tutorial provides a basic introduction to the Symfony framework running on docker. This is a great starting point if you are brand new to the Symfony framework or if you want to have your docker Symfony app.
Prerequisites
Before you start, you need to have some certain prerequisites in place:
Local machine running :
- the latest version of Docker.
- the latest version of composer.
Get Started
Let’s create a Symfony project and then see how we can dockerize it using Docker including Mysql database and phpMyAdmin.
Step 1 - Install Docker
Before creating a container for the Symfony application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:
Step 2 — Prepare project file structure
Create new folder called ‘__your_project_name__’ then let’s perpare the files needed to dockerize the symfony project :
- docker-compose.yml file :
We should add the mysql-server, phpMyAdmin, and the apache server to the services list as follow to get this final version of the file :
version: "3.8"
services:
# Mysql database container
db:
image: mysql
container_name: tutorial_db_symfony
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
networks:
- tutorial
# phpmyadmin container
phpmyadmin:
image: phpmyadmin
container_name: tutorial_phpmyadmin
restart: always
depends_on:
- db
ports:
- 8080:80
environment:
PMA_HOST: db
networks:
- tutorial
# Symfony apache project container
www:
build: docker/php
container_name: tutorial_symfony
ports:
- "8741:80"
volumes:
- ./docker/php/vhosts:/etc/apache2/sites-enabled
- ./:/var/www
restart: always
networks:
- tutorial
networks:
tutorial:
volumes:
db-data:
- docker/php/Dockerfile :
We should create a Dockerfile file inside docker/php folder to create our custom php apache image as folow :
FROM php:8.1-apache
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN apt-get update \
&& apt-get install -qq -y --no-install-recommends \
cron \
vim \
locales coreutils apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev;
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
RUN curl -sSk https://getcomposer.org/installer | php -- --disable-tls && \
mv composer.phar /usr/local/bin/composer
RUN docker-php-ext-configure intl
RUN docker-php-ext-install pdo pdo_mysql mysqli gd opcache intl zip calendar dom mbstring zip gd xsl && a2enmod rewrite
RUN pecl install apcu && docker-php-ext-enable apcu
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions amqp
WORKDIR /var/www
- docker/php/vhosts/vhosts.conf :
And a vhosts.conf file inside docker/php/vhosts folder as folow :
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/project/public
DirectoryIndex /index.php
<Directory /var/www/project/public>
AllowOverride None
Order Allow,Deny
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/project/public/bundles>
FallbackResource disabled
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
so the final folder structure should be as follow :
Step 3 — Run && Build the containers :
Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile
and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile
and leave you with a container that is ready to start.
To build your all your project docker containers, you will use the docker-compose up --build
command. So make sure you run this command on your terminal / cmd after locating on your main folder where the docker-compose.yml exist.
Step 4 — Create Symfony project :
To create your Symfony project you need the composer and PHP to be installed into your machine, we already have it installed in the docker container, so we need to access the container and run the composer create-project symfony/skeleton:"6.1.*" project
:
composer create-project symfony/skeleton:"6.1.*" project
Congratulation your Symfony app now is accessible on localhost:8741
Step 5 — Config the Mysql connection with your Symfony project :
To create the database we will need these important packages to be installed : so first of all lets install ORM Doctrine and maker bundle :
First, install Doctrine support via the orm
Symfony pack, as well as the MakerBundle, which will help generate some code:
$ cd project
$ composer require symfony/orm-pack
$ composer require --dev symfony/maker-bundle
All you have to do now is to add the database connection to your .env file to let the application connect with the Mysql container created previously.
So you will add this line as follow :
DATABASE_URL="mysql://root:@tutorial_db_symfony:3306/tutorial?serverVersion=8&charset=utf8mb4"
Your .env file should be like this :
Step 6— Now you can test your database connection :
Then we can create the database now using the following command :
$ php bin/console doctrine:database:create
Go to http://localhost:8080/ and login with (username : root / without password) and you will find your database created named ‘tutorial’.