Quick symfony 4 sandbox with docker

So you want to play with the new version of symfony ? Docker will be a powerful tool to help us setting up a sandbox for our symfony 4 experiments.

We will use the docker engine and docker-compose tool.

The stack :

We will also use composer official image to initialize the project and install additional PHP packages.

To stay really simple we use the PHP built-in webserver

Create the following directory structure : two empty files docker-compose.yml and docker/app/Dockerfile

.
├── docker
│ └── app
│ └── Dockerfile
└── docker-compose.yml

App Dockerfile

As I said earlier we don’t need to install Apache or nginx we only need PHP cli because we will use the built in PHP webserver. We just install a few PHP extensions and tweak opcache according to symfony performance documentation

Now the stack

Not much to say about the db and adminer services. For the app service : we are building the image from our previous Dockerfile, two important things to notice :

  • We override the CMD with PHP built-in webserver (-S option) who is listening on port 8000 inside our container it will serve the content of the /var/www/public directory, this will be our ./app/public outside the container : this directory does not exist yet, we will create symfony app skelton in next section.
  • We set an environment variable that will be read by our symfony app to get the connection to the database. Notice that the login:pass in the connection string (i.e. root:mdp4db) is set indose the mysql container (also with an environment variable MYSQL_ROOT_PASSWORD: mdp4db)

Create the skelton of our symfony 4 app

docker run --rm --interactive --tty --volume $PWD:/app --user $(id -u):$(id -g) composer create-project symfony/website-skeleton app

This is basically equivalent of the command shown in symfony installation doc (i.e. composer create-project symfony/website-skeleton app) except that composer command is running inside a container. The result is that you now have an “app/” directory with a symfony 4 app skelton

Now just launch the stack with

docker-compose up -d

Check the attributed port with

$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
symfony4_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:32794->8080/tcp
symfony4_app_1 docker-php-entrypoint php ... Up 0.0.0.0:32793->8000/tcp
symfony4_db_1 docker-entrypoint.sh mysqld Up 3306/tcp

Here it’s 32794, so I can visit http://localhost:32794

You will have a 404 error because there is no routes defined yet. You can start playing with symfony 4 here : https://symfony.com/doc/current/page_creation.html

NB : if you want to install extra packages/ symfony flex recipes … you can do something like this (this is for installing admin flex package) :

docker run --rm --interactive --tty --volume $PWD/app:/app --user $(id -u):$(id -g) composer require admin

If you want to launch bin/console commands you can do things like :

docker-compose exec app bin/console doctrine:database:create
docker-compose exec app bin/console make:entity Film
docker-compose exec app bin/console doctrine:migrations:diff
docker-compose exec app bin/console doctrine:migrations:migrate
docker-compose exec app bin/console doctrine:query:sql 'SELECT * FROM film'

Enjoy !