I usually used to develop my Symfony projects locally, installing all the dependencies and managing all the configuration by myself (Yes, I know for some people it may sound crazy).
My laptop which I use both privately and for development has been always full of unnecessary dependencies running in the background.
Every time a new version of the installed dependency has been released or my Mac has been upgraded I needed to maintain all the configuration of each 3rd party services.
I had to make sure everything worked fine. It has been really time-consuming, not effective and a pain in the ass most of the time. At least the positive thing is I learned how to install and configure these dependencies locally running on my Mac 😎.
Then the Docker came to the rescue.
Most developers know what Docker is, but not all of them use it. I assume you already know what Docker is and you have it running on your machine.
In this article, I will explain in short how to configure Docker on MacOS to use it with a new or existing Symfony 4 project running on Nginx, PHP-FPM and PostgreSQL.
Before we start…
Let’s recall some information.
What is Docker platform?
The Docker platform is the only container platform to build, secure and manage the widest array of applications from development to production both on premises and in the cloud. — docker.com
It has a lot of features and advantages which I will not cover in this article as it can be found all on the official docker.com website.
How to install Docker?
Docker is a multi-platform tool that can be installed on Windows, MacOS, Linux.
To find out more on how to install it follow the official Docker Documentation.
For the purpose of this example, I will use Docker Compose.
Compose is a tool for defining and running multi-container Docker applications.
Read more about Docker Compose in Docker Compose Documentation.
What is Dockerfile?
Dockerfile is a simple text file which contains instructions/commands used to build an image by Docker (you define special commands and all the magic happens in the background when you run docker build command).
You can read more about it in Dockerfile Reference Docs.
Setting up Symfony 4 Application with Docker
In this section, I will describe how to set up Docker to run Symfony 4 application. In this case, the Docker will be used to run all the services such as Nginx, PostgreSQL and PHP-FPM. The Symfony 4 application’s code should be located on your local machine.
1. Create a "docker" folder inside your Symfony application root directory
This folder will contain all the configuration files required by Docker.
Let’s assume your SF4 application is located at
The docker folder should be created like:
2. Create Docker Compose configuration file
Let’s create the
docker-compose.yaml file inside the docker directory created in step 1.
This is a configuration file used by Docker Compose where you can define your services, relations between them and more.
Your Symfony 4 application will run inside Docker container using the latest PHP-FPM 7.2, Nginx and PostgreSQL database engine.
The example configuration file will look as follows:
There are three services defined in that file:
The Postgres service will use the default Docker Postgres Image from the Docker Hub and will be exposed on port
5433 on your local machine, internally (inside Docker container) it will run on the default port 5432.
The Nginx service will be exposed on port
8080 and you will be able to access it using your browser on that port. It links php service in order to execute PHP files thanks to PHP-FPM.
There is also a volume created which will “map”
/var/log/nginx directory from the Docker container to your local machine. In this case, it will be:
/home/rafal/symfony_app/docker/logs/nginx directory from which you will be able to view the Nginx web server logs.
The PHP-FPM service will use the configuration file from the
php-fpm directory created in the next steps. The exposed port will be
9002 on your localhost.
There are also two volumes defined for the
php-fpm service. The first one
../:/var/www/symfony:cached, “maps” your local Symfony application to
/var/www/symfony folder inside the Docker container so the PHP code can be executed by the configured services.
./logs/symfony:/var/www/symfony/var/logs:cached “maps” the application logs generated inside the Docker container to your local directory
/home/rafal/symfony_app/docker/logs/symfony in this case. Thanks to this you can read all the SF logs which are generated when running inside Docker container.
3. Create PHP-FPM configuration file
To build a PHP-FPM image we need to create a
Dockerfile configuration file.
Inside docker directory which was created in step 1, create a
Inside it, create a
Dockerfile file with the content:
This image will contain all the required PHP extensions to run SF4 application including Composer.
The directory structure should look like that by now:
4. Create Nginx configuration file
To make the structure a bit cleaner, let’s create
nginx directory inside
docker dir first.
nginx directory, let’s create a
Dockerfile configuration file for Nginx server.
Thanks to this file, Docker will be able to build Nginx image which will be used to web serve our Symfony 4 application.
The content of that file will look as follow:
The above file defines that latest version of Nginx server must be installed and the content from the
default.conf file should be used as a main Nginx server config.
Let’s create the
default.conf file which is configured to serve Symfony 4 application inside Docker container by Nginx:
The directory structure should look like that by now:
See below the directory structure where all configuration files will be located:
5. Build the images
Right now, if we have all the configuration files in place, it is a time to build the images.
To do that, inside
docker directory run the following command in your terminal:
Docker will download all needed files and will build PHP-FPM, Nginx, PostgreSQL images which you will be able to run later on.
6. Run Symfony 4 app using Docker
To run the application, inside
docker directory run the command in your terminal:
docker-compose up -d
The Docker containers will start based on the previously built images. In the terminal you should see something like that:
Success! Your application located on your local machine (
/home/rafal/symfony_app) is now running inside the Docker using the configured Nginx, PostgreSQL and PHP-FPM images.
7. How to Access Symfony 4 App from inside the Docker?
The web server is exposed on port
8080 on your local machine. So to access it you have to go to the url:
http://127.0.0.1:8080 in your browser on MacOS(on Windows you should use
localhost instead of
How to run command inside Docker containers?
To run the console command inside the Docker execute the following command in your terminal:
docker-compose run php composer install
The above command will be executed on
php container where we have the access to the PHP-FPM service and the Composer.
To run any other command:
docker-compose run php bin/console doctrine:schema:update --force
Docker is a really powerful tool.
It increases your productivity and gives you lots of control over the services you wish to use.
Do not hesitate and try using it now if you haven’t already!
Personally, I can’t imagine working without the Docker nowadays and I can recommend it to anyone!
If you need to deploy Symfony 4 app quickly to production I recommend using one of the cheap VPS servers by DigitalOcean. By signing up from here you would receive $10 free credit for your droplet.