In this second part of this blog article we install the Symfony on our newly created Docker Development environment.
To follow this blog article, I assume that you are familiar with the Basics of Docker and Docker Compose and that you followed the first part of this article and have a basic Docker environment running and the corresponding files committed to your related git repository. For all who are mainly interested in installing Symfony on Docker, I will shortly summarize the first steps.
Summary of the previous article
In the first part we outlined the idea of our development environment based on Docker, discussed the components and set up a new project. We initiated a git repository and added a remote git repository from the GitLab platform where we later are going to create our deployment pipeline.
We set up the skeleton of our project folder and created the files needed for our NGINX webserver and the PHP-FPM process manager container. The article ended with booting up our container and we were happy about getting an error 404 from NGINX.
Installing Symfony on Docker
So, how to get rid of the error 404? And why are were we happy about it? That was because we successfully got a response from our Docker environment. But where nothing is, Docker can not find anything. That is why we got the error 404. To get rid of the error message, we need to produce some content. For that we are going to install the current version of Symfony in our Docker environment.
Because we are on our development environment it is completely fine to log into our containers and do things there. Of course, later on the production environment we won’t do that! But it makes development with Symfony a lot easier. Logging into the container allows us to use the PHP package manager Composer and also the Symfony console commands.
Opening our containers shell
So, how to log into the container? And into which one? (You remember, we already have two of them). Because we are going to use Composer and the Symfony console commands, which are both based on PHP, we have to log into the PHP-FPM container. You can do so either via the Docker Desktop app or via the terminal.
To open the command line interface of your container in Docker Desktop, you just have to click on the stacked three dots at the end of the php-fpm container line and choose Open in terminal
. On the newest Docker Desktop version the terminal window is integrated and you will see the terminal directly in Docker Desktop. From here you can work with your container like if you logged in it via SSH and use all the linux commands which are installed in you container.
On the other hand connecting with the CLI via terminal is also very easy. You just have to open the terminal app and navigate into your project folder.
cd myapp/ docker container ls
We need the results of the docker container ls command to identify the container name of our php-fpm container.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1d3d670162be nginx:alpine "/docker-entrypoint.…" 38 hours ago Up 38 hours 0.0.0.0:80->80/tcp myapp-nginx-1 013c2ab97d1e myapp-php-fpm "docker-php-entrypoi…" 43 hours ago Up 38 hours 9000/tcp myapp-php-fpm-1
In our example the container name is myapp-php-fpm-1. If you chose a different name for your project or your Docker services, your container name will be different. Please replace it accordently in the next command.
docker exec -it myapp-php-fpm-1 sh
You should now see in your terminal:
/var/www #
Before we really start installing Symfony let us check if everything works fine with Composer. For that enter in the containers shell:
composer -V
You should get an output which is similar to the following line:
Composer version 2.5.4
Great! Everything works fine! Let’s start with Symfony.
Installing Symfony via Composer
Symfony offers different possibilities to install on your machine. In my opinion the best and easiest way is to use the Composer. Luckily for us, we already installed Composer and verified that it is working well. More information about installing Symfony you can find on https://symfony.com/doc/current/setup.html
To create a basic Symfony app enter the following command inside your PHP-FPM container.
composer create-project symfony/skeleton:"6.2.*" .
Please pay attention to the dot at the end of the command. It is important to install Symfony in the current directory and not to create a subfolder.
The command takes some time depending on your machine and also your internet connection. After the command finished, let us check in your browser if we still get the error 404. Just hit localhost/
in your browsers address bar. You should see something like this:

Take also a look in your project folder into the subfolder src. You should find a lot of data here. That is your Symfony source code.
Adjusting your Dockerfile
Before we are done for today, let’s do some small changes to our PHP-FPM Dockerfile. Since our Symfony app is now installed we want Docker to install and update our composer dependencies whenever we are building our container. For that open you myapp/php-fpm/Dockerfile
:
# myapp/php-fpm/Dockerfile FROM php:8.1-fpm-alpine RUN apk --update \ --no-cache \ add git make g++ autoconf rabbitmq-c-dev libtool tzdata \ && pecl install mongodb \ && docker-php-ext-enable mongodb ENV TZ=Europe/Berlin RUN apk del --purge make g++ autoconf libtool \ && rm -rf /var/cache/apk/* RUN cd /usr/local/etc/php/conf.d/ && \ echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-ram-limit.ini COPY --from=composer /usr/bin/composer /usr/bin/composer WORKDIR /var/www EXPOSE 9000 CMD composer install; php-fpm
In line 19, adjust the command as shown above. Now Docker will always install all dependencies before starting the php-fpm process.
Commit your changes
As last step in this post we are going to commit our changes to our git repository. For that open your terminal (not in your container), navigate to your project folder and enter the following commands:
git add . git commit -m "Installing Symfony" git push -u origin main
Summary
In this blog post, we have built upon part one where we created our basic Docker environment and installed Symfony via Composer. Also we learned how to connect to a container shell with Docker and how to update automatically PHP dependencies upon container building. But for now, all we did is on our development environment.
In the next blog post in this series, we are going to add MongoDB to our Docker environment and install Doctrine MongoDB ODM in our Symfony app.