How to Setup Docker for Your Symfony Project
As you probably know, I am a big Symfony fan :) In the last few years I used Vagrant to set up my Symfony development environment more or less as described here. But we now have Docker and it’s time to move forward with a new setup. Docker containers are generally more lightweight than Vagrant virtual machines, so starting and stopping them is extremely fast. They also take a lot less disk space.
For this post I got some inspiration from here but extended it to have an additional MySQL database container.
To setup a Docker container you need a
Dockerfile file and to combine several containers we use the
docker-compose.yml file. For our environment we will need two containers, one with the latest Ubuntu LTS to host the web server and our project files and one for MySQL.
The first container is defined in the
Dockerfile file as below:
ADD . /app
RUN apt-get update
RUN apt-get install -y php apache2 libapache2-mod-php7.0 php-mysql php-intl git git-core curl php-curl php-xml composer zip unzip php-zip
# Configure Apache
RUN rm -rf /var/www/* \
&& a2enmod rewrite \
&& echo "ServerName localhost" >> /etc/apache2/apache2.conf
ADD vhost.conf /etc/apache2/sites-available/000-default.conf
# Install Symfony
RUN mkdir -p /usr/local/bin
RUN curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
# Add main start script for when image launches
ADD run.sh /run.sh
RUN chmod 0755 /run.sh
Add this file in a new folder. We also need the
run.sh files used in the code from above.
vhost.conf file is used to configure the apache virtual host:
Require all granted
run.sh file runs when the container starts and just makes sure that the
logs folders are set up before launching the apache web server:
mkdir -p my_project/app/cache my_project/app/logs
chgrp -R www-data .
chmod -R g+w my_project/app/cache my_project/app/logs
tail -F /var/log/apache2/* my_project/app/logs/prod.log my_project/app/logs/dev.log &
exec apache2 -D FOREGROUND
That’s all for our main Ubuntu container, we now need to create the
This will tell Docker to first start an MySQL 5.7 container, then our Ubuntu container that will be able to access the MySQL container using the
mysql host name.
Start everything with the
docker-compose up command.
When it’s done, open a new terminal (let the latest docker compose command run, do not stop it) and use the
docker ps command to see the running containers:
Now, you can take the
id of the Ubuntu container and ssh into it:
docker exec -t -i [ID] bash
Here you will start a new Symfony project as always (you will have to delete the
my_project folder created by Docker first):
rm -rf my_project
symfony new my_project 2.8
If you name you project something else (yes, you will probably do that) just replace the all
my_folder occurrences in the files and commands above with the actual name of your project.
After you created the new Symfony project, it is better to rebuild the Docker containers so the setup of the
logs folders will be as intended. In the terminal window where you launched Docker, press
Ctrl+C to stop it then run
docker-compose up again.
That’s it! You can now access your new Symfony project using the following URL: http://localhost:8080/. To connect to MySQL you will use the
mysql host name:
Thank you! Please let me know in the comments if you have any suggestions to improve this setup or problems running it.
Originally published at intelligentbee.com.