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.
- It’s open source – look at source code.
- Has Long Term Support (LTS) – you can use it for years and be sure it will be safe and supported.
- Easy to use – install by using one command.
- A lot of components – list on the bottom of this article.
- Adopting the programing standards!
What will I show you?
Today:
- How to install
symfony
by using a single container in docker
. Without http, database, queue server. Just symfony ready to work.
In future notes:
- How to receive easy access to bin/console.
- How to tracking the symfony logs.
Let’s start
Create Dockerfile
First, what you need to do is to create your workspace and 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:
It gets the latest version of PHP, in my case, it will be 7.1.2. Now you have to run that command:
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!
If everything went well, then you should see something like that:
Success! You have just installed the latest PHP version. Now we have to update your Dockerfile
for next things:
- Get and install composer
- Install needed PHP extensions
- Get symfony package
- Set up the symfony
- Run PHP server
Get and install composer
Open Dockerfile
again. On the second line put on that code:
It gets composer installer from composer’s server and installs it at …
. To test your composer, run:
That you should get output similar to this:
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
Install symfony
Ok, you are ready to install symfony
in your docker
container. Add that lines to your Dockerfile
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:
When script finishes its works than run command to tests your symfony app:
The output should looks like this:
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:
Then, open your browser and go to http://127.0.0.1:8080
you should see Welcome page
from symfony.
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:
After it you have to rebuild your docker image, so run it:
When it will be ready, just run:
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?
Yes, you have to add that code to end of your Dockerfile
, save and rebuild your docker images.
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.
Let’s test! Create file i.e. info.php
on your host in folder ./www/web/
, you can use that command:
Add put on there that contents:
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.
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
:
You need to run:
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