keyboard

Hugo Soltys

Symfony developer

Since 2013

Setup a Docker development environment for your Symfony project

Posted on by Hugo - 1398 views - 0 Comments


Warning : Please notice this article is more than 1 year old. Some mechanics may have changed due to the newer versions of the used tools.

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.

INSTALLING DOCKER

 

PREREQUISITES FOR APT

 

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://ha.pool.sks-keyservers.net:80 \
                --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 https://apt.dockerproject.org/repo ubuntu-precise main
Trusty 14.04 (LTS) deb https://apt.dockerproject.org/repo ubuntu-trusty main
Wily 15.10 deb https://apt.dockerproject.org/repo ubuntu-wily main
Xenial 16.04 (LTS)

deb https://apt.dockerproject.org/repo 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

 

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 "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(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 !

 

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

 

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

#/var/www/my-project/docker-compose.yml

my-project:
    image: iamluc/symfony
    ports:
        - "80:80"
    volumes:
        - ./:/var/www/html
    links:
        - database

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

phpmyadmin:
    image: corbinu/docker-phpmyadmin
    ports:
        - "8080:80"
    environment:
        - MYSQL_USERNAME=root
        - MYSQL_PASSWORD=password
    links:
        - 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.


Hugo Soltys

My name is Hugo, I'm 25 and I'm a Symfony developer since 2013. I love to create websites by myself to learn new technologies or increase my skills.
I love movies, books, music and video games. I also like to drink a few beers with my friends. I'm from Lille (France) and I currently work as Symfony developer at Decathlon since 2016. Before that, I worked as Symfony developer for the IT Room company, in Roubaix, France.


Older articles