December 16, 2016 0 comments

Setup a Docker development environment for your Symfony project

Docker is an open-source project that automates the deployment of an application inside containers.

For a Symfony project, Docker allows you to setup your working environment really easily without installing anything on your computer except Docker.

In this tutorial, we will learn to install Docker and getting started to work in a few steps.

Like in most of my tutorials, I will write the following steps for an Ubuntu distribution, so apologize for non-Linux users.


1 - Installing Docker

Update your package information and ensure that APT works with https, and that CA certificates are installed.


$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates


Now add the new GPG key.


$ sudo apt-key adv \ 
                --keyserver hkp:// \
                --recv-keys 58118E89F3A912897C070ADBF76221572C52609D 


In the following table, find the entry corresponding to your Ubuntu version and run the corresponding command


Ubuntu version Repository
Precise 12.04 (LTS) deb ubuntu-precise main
Trusty 14.04 (LTS) deb ubuntu-trusty main
Wily 15.10 deb ubuntu-wily main
Xenial 16.04 (LTS)

deb ubuntu-xenial main

Run the following command, substituting the entry for your operating system for the placeholder <REPO> 


$ echo "<REPO>" | sudo tee /etc/apt/sources.list.d/docker.list


Now update your packages again


$ sudo apt-get update


Time to install Docker ! 


$ sudo apt-get install docker-engine


Start Docker daemon


$ sudo service docker start


Verify that Docker is installed properly by running


$ sudo docker run hello-world


2 - Installing docker-compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.


To install docker-compose type the cURL command below


$ curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose


Apply executable permissions to the binary


$ chmod +x /usr/local/bin/docker-compose


Test the installation by typing


$ docker-compose --version
#docker-compose version: 1.9.0


Congrats ! You can now use Docker on your computer !


3 - Creating a new Symfony project

Symfony provide an application called Symfony Installer to ease the creation of a new Symfony project. Follow the official tutorial to install it.

Once this is done, open a new terminal a navigate to your working directory 


$ cd /var/www


Now use the installer to create your new project


$ symfony new my-project


4 - Use Docker in your Symfony project

Start with navigate in your project directory then create a new docker-compose file.


$ cd /var/www/my-project
$ touch docker-compose.yml


Let's assume that you need Apache, PHP, MySQL and phpMyAdmin.


Here is the content you need in your docker-compose file



    image: iamluc/symfony
        - "80:80"
        - ./:/var/www/html
        - database

    image: mysql
        - "3306:3306"
        - MYSQL_ROOT_PASSWORD=password
        - MYSQL_USER=root
        - MYSQL_PASSWORD=password

    image: corbinu/docker-phpmyadmin
        - "8080:80"
        - MYSQL_USERNAME=root
        - MYSQL_PASSWORD=password
        - database:mysql

As you can see, the syntax is pretty simple to understand, but some explanations are needed if you haven't used Docker before.


Each "block" (my-project, database, phpmyadmin) defines a tool that you need in your project. Each of those blocks are composed of multiple properties (image, ports, ...). Those properties corresponds to the tool required configuration. 


  • image : If you specify image, then docker-compose names the built image with the webapp and optional tag specified in image.
    For example, the iamluc/symfony image provides you Apache and PHP with some useful tools like Blackfire or Composer.
  • ports : Like most of dependencies, you must specify to Docker which port you want to use.
    For example, MySQL generic port is 3306
  • environment : Here you will define the needed environment variable to the good working of the tool
  • links : Some tools depends from others to work, so in the links property you have to specify them so Docker will understand that and link them.
    For example, phpMyAdmin needs MySQL. That's why we have "database:mysql" in the links of phpMyAdmin.


Now that your docker-compose.yml file is ready, open a new terminal to build our container.


$ cd /var/www/my-project
$ docker-compose build


Docker will download images specified in your docker-compose file and build them. Once this is done, it only remains to start our container with the following command.


$ docker-compose up


There you go ! Your new project is now available at http://localhost:80 and phpMyAdmin at http://localhost:8080 !


If you need any other tools in your container, take a look at Docker Hub, which provide you a large collection of all kinds.


I hope you liked this tutorial, leave me a comment if you have some questions.



Hugo Soltys

Who is talking to you ?

My name is Hugo, I'm 29 and I'm a Symfony developer since 2013. I love to create websites by myself to learn new technologies or increase my skills. I also like to share my knowledge so I created this blog. I hope you enjoy it :)