Hi there. Today, I’m going to take care of PHP framework symfony and I’ll show you how to install it on your Linux machine by using docker.

If you use MacOS and you have installed docker as I described in my last note then you have to wait a while. I will describe it in another post.

As I’ve written before, symfony is a modern PHP framework, you can use it to create complex website. What is important, symfony has one of the biggest community. At this moment, it has more than 2 000 project contributors!

TL;DR: If you don’t want to read this article so you can go directly to the bottom where there is a full recipe ready to copy, paste and run.

[toc]

The most important features

If you don’t know why you should use symfony when you use PHP at your work, let me show the most important features.

  1. It’s open source – look at source code.
  2. Has Long Term Support (LTS) – you can use it for years and be sure it will be safe and supported.
  3. Easy to use – install by using one command.
  4. A lot of components – list on the bottom of this article.
  5. Adopting the programing standards!

What will I show you?

Today:

  1. How to install symfony by using a single container in docker. Without http, database, queue server. Just symfony ready to work.

In future notes:

  1. How to receive easy access to bin/console.
  2. How to tracking the symfony logs.

Let’s start

Create Dockerfile

First, what you need to do is to create your workspace and Dockerfile.

mkdir symfony_project
cd symfony_project
touch Dockerfile

Now open Dockerfile in your favourite code editor. I will use a vim. In the first line you have to put on that code:

FROM php:latest

It gets the latest version of PHP, in my case, it will be 7.1.2. Now you have to run that command:

docker build . --tag "symfony"

It downloads the PHP image from dockerhub, build your local image and allow you to use that container by using simple name symfony.
Let’s check!

docker run symfony php --version

If everything went well, then you should see something like that:

PHP 7.1.2 (cli) (built: Feb 20 2017 18:45:45) ( NTS )
	Copyright (c) 1997-2017 The PHP Group
	Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

Success! You have just installed the latest PHP version. Now we have to update your Dockerfile for next things:

  1. Get and install composer
  2. Install needed PHP extensions
  3. Get symfony package
  4. Set up the symfony
  5. Run PHP server

Get and install composer

Open Dockerfile again. On the second line put on that code:

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

It gets composer installer from composer’s server and installs it at . To test your composer, run:

docker build . --tag "symfony"
docker run symfony composer --version

That you should get output similar to this:

Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer version 1.3.2 2017-01-27 18:23:41

You can ignore the warning from the first line because you are running it at the container.

Install needed extensions

Well, to use symfony and composer we need to install something more. We need to add support to use git and zip. Open your Dockerfile and add it on 3th – 4th lines

RUN apt-get update && apt-get install -y git libpng12-dev
RUN docker-php-ext-install zip && docker-php-ext-enable zip

Install symfony

Ok, you are ready to install symfony in your docker container. Add that lines to your Dockerfile

RUN mkdir -p /var/www
RUN composer create-project symfony/framework-standard-edition /var/www/
RUN chmod +x /var/www/bin/console

It creates /var/www folder where we will storage our app, gets symfony code from server and make bin/console executable.
Save your changes and run again:

docker build . --tag "symfony"

When script finishes its works than run command to tests your symfony app:

docker run symfony /var/www/bin/console --version

The output should looks like this:

Symfony 3.2.4 (kernel: app, env: dev, debug: true)

Run your new app in browser

Ok, you make websites, so you need to run your project in your favourite browser. At the beginning you can run that command:

docker run -p 8080:8080 symfony /var/www/bin/console server:run 0.0.0.0:8080

Then, open your browser and go to http://127.0.0.1:8080 you should see Welcome page from symfony.

Symfony + Docker on Browser

Improve docker server

You want to use symfony and docker, but you don’t want to remember about running that long command docker run ….? Open again Dockerfile and on the end of the file add that:

EXPOSE 8080
CMD /var/www/bin/console server:run 0.0.0.0:8080

After it you have to rebuild your docker image, so run it:

docker build . --tag "symfony"

When it will be ready, just run:

docker run -d -p 8080:8080 --name symfony_demo symfony

As you can see, the command is shorter. But it still works, you can run again your browser to test it.

How can I work with my code?

Ok, you installed symfony but how can you get access to the sources?

VOLUME /var/www/

Yes, you have to add that code to end of your Dockerfile, save and rebuild your docker images.

docker build . --tag "symfony"

Now you have to run three commands, first copies sources to your machine, second kills the container which you run before and the last one recreate a container again, but this time uses files from your host.

docker cp symfony_demo:/var/www .
docker rm -f symfony_demo
docker run -d -p 8080:8080 --name symfony_demo -v $(pwd)/www:/var/www symfony

Let’s test! Create file i.e. info.php on your host in folder ./www/web/, you can use that command:

vim $(pwd)/www/web/info.php

Add put on there that contents:

<?php
echo 'Hello world';
phpinfo();

And just run your browser, go to http://127.0.0.1:8080/info.php. You should see Hello world and information about installed PHP. Everything without touching docker.

Symfony + Docker + phpinfo

Give me feedback!

If you want to read more articles about how to work with symfony on developer machine, let me know about that in comments, tell me what I should describe.
If you want to receive email about more news about symfony, just sign up to newsletter (NO SPAM!!! – max two emails in month)

TL;DR: Recipe

Your Dockerfile:

FROM php:latest
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get update && apt-get install -y git libpng12-dev
RUN docker-php-ext-install zip && docker-php-ext-enable zip
RUN mkdir -p /var/www
RUN composer create-project symfony/framework-standard-edition /var/www/
RUN chmod +x /var/www/bin/console
EXPOSE 8080
CMD /var/www/bin/console server:run 0.0.0.0:8080
VOLUME /var/www/

You need to run:

docker build . --tag "symfony"
docker run -d --name symfony_demo symfony
docker cp symfony_demo:/var/www .
docker rm -f symfony_demo
docker run -d -p 8080:8080 --name symfony_demo -v $(pwd)/www:/var/www symfony

And you can run your browser: http://127.0.0.1:8080

Available components

Component
The Asset Component
The BrowserKit Component
The Cache Component
The ClassLoader Component
The Config Component
The Console Component
The CssSelector Component
The Debug Component
The DependencyInjection Component
The DomCrawler Component
The EventDispatcher Component
The ExpressionLanguage Component
The Filesystem Component
The Finder Component
The Form Component
The HttpFoundation Component
The HttpKernel Component
The Intl Component
The Ldap Component
The OptionsResolver Component
The PHPUnit Bridge
The Process Component
The PropertyAccess Component
The PropertyInfo Component
The Routing Component
The Security Component
The Serializer Component
The Stopwatch Component
The Templating Component
The Translation Component
The Validator Component
The VarDumper Component
The Workflow Component
The Yaml Component

Image: “wall” (CC BY-SA 2.0) by tablexxnx