The perfect kit starter for a Symfony 4 project with Docker and PHP 7.2
This blog post is an introduction to devs who want to start using Docker with Symfony4. It will guide you through creating a Symfony 4 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 18.09.3, build 774a1f4
docker-machine --version docker-machine version 0.16.0, build 702c267f
docker-compose --version docker-compose version 1.23.2, build 1110ad01
Once all of that is out of the way, we can start with the Symfony 4 project and the Docker environment that will run it.
I’m creating a SF4 project from scratch and I want to set up a development environment with Docker.
Step 1: Clone the docker-symfony repository which has the docker configuration files.
git clone https://github.com/StaffNowa/docker-symfony.git Cloning into 'docker-symfony'... remote: Enumerating objects: 180, done. remote: Counting objects: 100% (180/180), done. remote: Compressing objects: 100% (124/124), done. remote: Total 180 (delta 85), reused 138 (delta 47), pack-reused 0 Receiving objects: 100% (180/180), 34.12 KiB | 6.82 MiB/s, done. Resolving deltas: 100% (85/85), done.
Step 2: Create the Symfony project skeleton.
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 container ./up.sh
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.
docker-compose exec php bash
Once in there, we’re in a PHP7 image, so we should be able to create the skeleton for the Symfony project.
# inside php bash composer create-project symfony/skeleton symfony
Step 3: Move the contents of the skeleton into the root of the project.
Unless you want to change the config of the working directory inside the docker-composer.yml, we need the Symfony project to be in the root folder.
#inside php bash mv symfony/* . mv symfony/.env . mv symfony/.gitignore . mv symfony/var/* /var/
Now we can delete the empty folder we used for creating the skeleton
#inside php bash rm -rf symfony/
Prepare a symlink for the file index.php inside the php container.
# inside php bash cd public/ ln -s index.php app.php
Step 4: Require the components.
We can require whatever components we need.
# inside php bash composer require annotations composer require --dev profiler composer require twig composer require orm composer require form composer require form validator composer require maker-bundle
These are just a few, feel free to add the ones you want.
Step 5: Creating some sample code in Symfony 4 project.
Now lets create a controller to test a sample route to make sure everything works.
# inside php bash bin/console make:controller Prado
Now, open a new google chrome tab and type the following URL.
We should now see our new controller action rendering a response.
Finally, to sync the database, you need to update the .env file with the variables we set on the mysql image.
.env file that has been generated when requiring the orm package in Symfony 4.
So if you check the .env file, you’ll see the credentials under the MySQL configuration. So change the file to
Finally, let’s restart the containers