visit our site

How to Setup Docker for Your Symfony Project

In Blog, PHP and Symfony

by Dragos Holban on September 18, 2017

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 this file in a new folder. We also need the vhost.conf and run.sh files used in the code from above.

The vhost.conf file is used to configure the apache virtual host:

The run.sh file runs when the container starts and just makes sure that the cache and logs folders are set up before launching the apache web server:

That’s all for our main Ubuntu container, we now need to create the docker-compose.yml file:

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:

Here you will start a new Symfony project as always (you will have to delete the my_project folder created by Docker first):

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 cache and 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

+1
Share
Tweet
Share
Stumble
Pin