-
Notifications
You must be signed in to change notification settings - Fork 27
HOW TO: Run a Symfony Application
Gaudi just requires a configuration file in order to run an architecture. As there is never enough documentation and/or tutorials, this is a full example of how to build and run a Symfony project that can display a username from the database.
To run a Symfony app, we need an Apache server, a PHP-FPM app and a MySQL database.
Create a .gaudi.yml
file in a new folder:
applications:
front:
type: apache
links: [app]
ports:
80: 80
volumes:
.: /var/www
custom:
fastCgi: app
documentRoot: /var/www/web
modules: [rewrite]
app:
type: php-fpm
links: [db]
ports:
9000: 9000
volumes:
.: /var/www
db:
type: mysql
ports:
3306: 3306
after_script: mysql -e "CREATE DATABASE symfony CHARACTER SET utf8 COLLATE utf8_general_ci;" -uroot
volumes:
.gaudi/mysql: /var/lib/mysql
It defines 3 applications.
This application is the Apache server. It redirects all PHP requests to the PHP-FPM application.
The volumes
option mounts a folder of the host machine in the container: we mount the current folder (.
) in /var/www
.
The custom option
defines the specific configuration of an application.
- Activate fastcgi to indicate which application will receive the fastcgi requests.
- Specify document root into the
web
folder (default configuration for a Symfony application) - Enable the
mod_rewrite
module for Apache
Link the application to the PHP-FPM one called app
.
The PHP-FPM app also mount the current folder to /var/www
. This tells PHP which file to process.
It is linked to the db
application.
The MySQL application store all its data into the /var/lib/mysql
by default.
As Docker does not keep data when a container is removed, we need to mount a folder from the host machine into /var/lib/mysql
.
The after_script
runs the script after the launch of MySQL: In this example, create a new database.
To start the Symfony app, run in the current folder:
$ mkdir -p .gaudi/mysql
$ gaudi
Cleaning front ...
Cleaning app ...
Cleaning db ...
Building gaudi/front ...
Building gaudi/app ...
Building gaudi/db ...
Starting db ...
Application db started (172.17.0.153:3306)
Starting app ...
Application app started (172.17.0.154:9000)
Starting front ...
Application front started (172.17.0.155:80)
The building process can take a few minutes the first time. Thereafter Docker will detect the differences between the last build and run only what needed.
The 3 applications are now started: we can attach the app
application to install Symfony.
To run some php commands in the PHP-FPM application, use the docker attach
command.
$ docker attach app # the PHP-FPM application is called `app` in the configuration file
$ cd /var/www
$ # Symfony is first created in a symfony folder because we need an empty folder to run composer
$ composer -n create-project symfony/framework-standard-edition symfony 2.4.1 && mv symfony . && rm -rf symfony
Create the app/config/parameters.php
file to retrieve the db
host in the Symfony configuration :
$container->setParameter('database_host', getenv('DB_PORT_3306_TCP_ADDR'));
Change the app/config/config.yml
to include this new file after the inclusion of parameters.yml
:
imports:
# ...
- { resource: parameters.php }
Create a new entity in src/Acme/DemoBundle/Entity/User.php
:
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table("user")
* @ORM\Entity()
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255)
*/
private $username;
/**
* @param string $username
*/
public function __construct($username = null)
{
$this->username = $username;
}
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getUserName()
{
return $this->username;
}
}
Create the database :
$ php app/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "1" queries were executed
Detach the app with ctrl+p
ctrl+q
.
To retrieve the db
IP, simply run :
$ gaudi --check
Application front is running (172.17.0.155:80)
Application app is running (172.17.0.154:9000)
Application db is running (172.17.0.153:3306)
Create a new user in the database :
$ mysql -h 172.17.0.153
$ INSERT INTO symfony.user (username) VALUES ('Manu');
To check if Symfony can retrieve this user, add in the src/Acme/DemoBunde/Controller/DemoController.php
:
$user = $this->get('doctrine.orm.entity_manager')->getRepository('AcmeDemoBundle:User')->find(1);
var_dump($user->getUsername());