Start With Symfony And Docker

1 November 2017 · 3 minute read

Docker can help you start with new technology or language without installing all programs on your computer just to try it. You can pull docker images, run it and if you don’t like it just remove images.

For examaple for starting learning Symfony and creating your first page there is no need to install nginx/apache, php. You just need to install docker and docker-compose.

Create template for first page in Symfony

After you get Symfony installer we can create simple template for our app:

$ symfony new docker_app
$ cd docker_app
$ tree -L 1
.
├── app
├── bin
├── composer.json
├── composer.lock
├── phpunit.xml.dist
├── README.md
├── src
├── tests
├── var
├── vendor
└── web

This template already has DefaultController with test page. Now we can start it with docker. We will be using docker-compose. It’s a tool for running multi-container Docker application. Docker-compose needs file docker-compose.yml in this file we will add destription of way docker-compose should create and start two containers nginx and php-fpm.

# docker-compose.yml
version: '3'

services:
    host:
        image: nginx:alpine
        ports:
            - '8080:80'
        volumes:
            - ./:/app
            - ./default.conf:/etc/nginx/conf.d/default.conf
    php:
        image: php:fpm-alpine     
        volumes:
            - ./:/app
        container_name: php

You can see in section volumes that docker maps local directory ./ to directory /app inside container.

Now we should create simple configuration file for nginx. We can use config for Symofny. In this config nginx will use directory /app/web like a root directory.

# default.conf
server {
    listen       80;
    server_name  localhost;
    root /app/web;

    location / {
        # if you wat use app_dev you should remove
        # some rows in web/app_dev.php
        # try_files $uri /app_dev.php$is_args$args;

        try_files $uri /app.php$is_args$args;
    }

    # location ~ ^/(app_dev|config)\.php(/|$) {
    location ~ ^/app\.php(/|$) {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Now we have this two files to start docker-compose. For starting our application you just need inside directory with our app run command docker-compose up -d:

$ tree -L 1
.
├── app
├── bin
├── composer.json
├── composer.lock
├── default.conf
├── docker-compose.yml
├── phpunit.xml.dist
├── README.md
├── src
├── tests
├── var
├── vendor
└── web
$ docker-compose up -d
Creating network "dockerapp_default" with the default driver
Creating dockerapp_host_1 ... 
Creating php ... 
Creating dockerapp_host_1
Creating php ... done
Attaching to dockerapp_host_1, php

And now you can visit localhost:8080.

That’s it. Now you can work with symfony and add new pages.

After you are done you should run command docker-compose down to remove containers, networks, volumes and images.

$ docker-compose down
Stopping php              ... done
Stopping dockerapp_host_1 ... done
Removing php              ... done
Removing dockerapp_host_1 ... done
Removing network dockerapp_default
comments powered by Disqus
github