The perfect kit starter for a Symfony 6 project with Docker and PHP 8.2
This blog post is an introduction to devs who want to start using Docker with Symfony 6. It will guide you through creating a Symfony 6 project running on Docker.
Before we start, you’ll need to install Docker CE, Docker Machine, Docker Compose in your machine.
Install the latest Docker CE here.
Install the latest Docker Machine here.
Install the latest Docker Compose here.
However, if you’re lazy like me, just use this command to make sure it’s installed.
docker --version
Docker version 23.0.1, build a5ee5b1
docker-compose --version
Docker Compose version v2.12.2
Once all of that is out of the way, we can start with the Symfony 6 project and the Docker environment that will run it.
I’m creating a SF6 project from scratch and I want to set up a development environment with Docker.
Step 1: Create a new folder for your new project.
mkdir docker-symfony
Step 2: Download the latest D4D docker version from our repository.
A couple of years ago, we created a D4D project that allows you to quickly launch a Symfony project, which we will use in our Symfony project too.
It can be downloaded from here.
wget https://github.com/StaffNowa/docker-symfony/releases/download/v2.0.14/d4d_linux_amd64.tar.gz && tar xzvf d4d_linux_amd64.tar.gz && rm d4d_linux_amd64.tar.gz
Step 3: Create the Symfony project.
First off, let’s start docker containers with the project we just downloaded.
# cd to the location where you cloned the project
cd docker-symfony
# create and start a container
./d4d start
This command starts the containers.
Docker should start building (if it’s the first time for these images) and running with the containers in the background.
We will need to create the Symfony project inside the php image bash.
./d4d exec php
Once in there, we’re in a PHP8 image, so we should be able to create a new Symfony project via the composer create-project command.
# inside php bash
composer create-project symfony/skeleton .
Step 4: Require the components.
We can require whatever components we need.
# inside php bash
composer require symfony/orm-pack
composer require symfony/maker-bundle --dev
These are just a few, feel free to add the ones you want. You can read more about Symfony components here.
Step 5: Create some sample code in Symfony 6 project.
Now lets create a controller to test a sample route to make sure everything works.
# inside php bash
bin/console make:controller D4d
Now, open a new google chrome tab and type the following URL.
http://symfony.local/d4d
We should now see our new controller action rendering a response.
Finally, to sync the database, you need to update the .env.local file with the variables we set on the mysql image.
.env.local file that has been generated when requiring the orm package in Symfony 6.
DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8&charset=utf8mb4"
So if you check the .env.local file, you’ll see the credentials under the MySQL configuration. So change the file to
DATABASE_URL="mysql://db_user:db_password@mysql:3306/db_name?serverVersion=8&charset=utf8mb4"
Finally, let’s restart the containers.
./d4d stop && ./d4d start