Skip to content

Instantly share code, notes, and snippets.

@FaHuSchmidt
Last active November 9, 2018 14:41
Show Gist options
  • Save FaHuSchmidt/8f507afd477d8178778216857f80dfaf to your computer and use it in GitHub Desktop.
Save FaHuSchmidt/8f507afd477d8178778216857f80dfaf to your computer and use it in GitHub Desktop.
Create Symfony project with Docker

Create a Symfony project

...with Docker as only dependency.

No local installations of PHP or composer needed!

This example uses Symfony Flex and symfony/skeleton which contains just the minimum parts of a Symfony project. You'll find further informations at Symfony docs: https://symfony.com/doc/current/setup/flex.html

Create project via composer

If you just want to use the create-project command without some locally installed PHP use the following command and replace my-project in all commands with your prefered project dir name: 

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    composer create-project \
    symfony/skeleton \
    my-project

cd in project folder

cd my-project

For future composer commands you can use

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    composer --version

or create a bash script e.g. composer within the root dir and use it like ./composer --version. https://gist.github.com/FaHuSchmidt/d748b3d732a4ddc6f97f1baefe57452f

Start a PHP dev server within a Docker container

I don't use a webserver like apache or nginx to keep things simple here.

docker run -d -p 8000:80 \
    --name my-project \
    -v "$PWD":/var/www/app \
    -w /var/www/app \
    php:alpine \
    php -S 0.0.0.0:80 -t ./public ./public/index.php

If you open the URL localhost:8000 you will see a page from Symfony which says something like "Sorry, the page you are looking for could not be found.", but we're fine because der isn't neither a controller or a route yet.

To make things more comfortable you can create a script e.g. run within the root dir and use it like ./run. https://gist.github.com/FaHuSchmidt/819ad82d98040d1170878763303d7165

Going further

You may get very far with this approach as you could add nearly all server dependencies without creating local dockerfiles, but I think this is more for bootstrapping a project or to transfer ideas fast into source code, because you don't have to bother with building up an configuration based Docker infrastructure, which can extremly slow down the start of a project.

The next step may be to think about dockerfiles for php-fpm, webserver, database and so on, to create images which can be pushed to production servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment