DEV Community

Mert Simsek
Mert Simsek

Posted on • Updated on

Symfony 4 Running on Docker Compose

I am developing an application to improve vocabulary. You will enter in this application the words you learned. Then, this application asks you randomly. For this, i need to establish the workspace. The workspace will become Symfony 4 on Docker Compose. It dependent Nginx, Php-Fpm and Mysql containers.

Firstly, i will adjust docker-compose.yml file. Mysql is simple and pure. Nginx and Php-fpm are establishing interested with their files. Project structure is like this.

alt text

The following lines mean docker.compose.yml file.

version: '2'
services:
    php:
        build: ./php-fpm
        volumes:
            - ./individual-vocabulary:/var/www/individual-vocabulary
            - ./php-fpm/php.ini:/usr/local/etc/php/php.ini
            - ./assets/vocabulary-gc-project.json:/var/www/vocabulary-gc-project.json
        environment:
            GOOGLE_APPLICATION_CREDENTIALS: /var/www/vocabulary-gc-project.json
        depends_on:
            - mysql
    web:
        image: nginx:latest
        ports:
            - "8888:80"
        volumes:
            - ./individual-vocabulary:/var/www/individual-vocabulary
            - ./nginx/individual-vocabulary.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - php
    mysql:
        image: mysql:5.6
        environment:
            MYSQL_ROOT_PASSWORD: symf0ny
        ports:
            - "3333:3306"
Enter fullscreen mode Exit fullscreen mode

I need a Nginx configuration file. I defined as below.

server {
    listen 80;
    server_name web;
    root /var/www/individual-vocabulary/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        #fastcgi_pass unix:/var/run/php7.2-fpm.sock;
        fastcgi_pass php:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        internal;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}
Enter fullscreen mode Exit fullscreen mode

Well, right now we must configure Dockerfile for Php-fpm.

FROM php:7.2-fpm-alpine

RUN apk update \
    && apk add  --no-cache git mysql-client curl libmcrypt libmcrypt-dev openssh-client icu-dev \
    libxml2-dev freetype-dev libpng-dev libjpeg-turbo-dev g++ make autoconf \
    && docker-php-source extract \
    && pecl install xdebug redis \
    && docker-php-ext-enable xdebug redis \
    && docker-php-source delete \
    && docker-php-ext-install pdo_mysql soap intl zip \
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_port=10000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    #&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && rm -rf /tmp/*

CMD ["php-fpm", "-F"]

WORKDIR /var/www/individual-vocabulary

EXPOSE 9000
Enter fullscreen mode Exit fullscreen mode

After this, i need a Php.ini file. For this, i defined like this.

[PHP]
engine = On
short_open_tag = On
precision = 14
output_buffering = Off
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 100
disable_functions = dl
disable_classes =
zend.enable_gc = On
expose_php = Off
max_execution_time = 30
max_input_time = 60
memory_limit = 256M
error_reporting = E_ALL & ~E_NOTICE
display_errors = On
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
error_log = error_l
variables_order = "EGPCS"
request_order = "GP"
register_argc_argv = On
auto_globals_jit = On
post_max_size = 64M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
include_path = ".:/opt/php72/lib/php"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 64M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = On
default_socket_timeout 
cli_server.color 
date.timezone = Asia/Riyadh
pdo_mysql.cache_size = 2000
pdo_mysql.default_soc
sendmail_path = /usr/sbin/sendmail -t 
mail.add_x_header 
sql.safe_mode =
odbc.allow_persistent = Off
odbc.check_persistent = Off
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M"
mysqli.max_persistent = -1
mysqli.allow_persistent = Off
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect =
mysqlnd.collect_statistics = Off
mysqlnd.collect_memory_statistics =
pgsql.allow_persistent = Off
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice
bcmath.scale
session.save_handler = files
session.save_path = "/tmp"
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fields"
zend.assertions 
tidy.clean_output =
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit
ldap.max_links 
Enter fullscreen mode Exit fullscreen mode

From now on, i can install Symfony 4. I run this command in project directory into as document.

composer create-project symfony/website-skeleton individual-vocabulary
Enter fullscreen mode Exit fullscreen mode

After installation, you will see .env file in application. I have changed DATABASE_URL environment variable as below.

APP_ENV=dev
APP_SECRET=9258a6c0e5c19d0d58a8c48bbc757491
DATABASE_URL=mysql://root:symf0ny@mysql:3306/individual-vocabulary
Enter fullscreen mode Exit fullscreen mode

I have defined root for user, symf0ny for password and mysqlfor host. This host name is coming from docker-compose.yml file. As you remember, we have defined mysqlname for mysql service in this file.

Right now, we can run this command. So, all images become a container.

docker-compose up
Enter fullscreen mode Exit fullscreen mode

From now on, :8888port is listening to http request. If you open http://localhost:8888, it answers you.

alt text

Top comments (8)

Collapse
 
deadpoolzzz profile image
Anton Belykh

volumes:

  • ./php-fpm/config/php.ini in docker-compose.yml, but there is no config directory on the screenshot
Collapse
 
_mertsimsek profile image
Mert Simsek • Edited

Good point. Thank your for reply, i thought that i already have been changed that line.

Collapse
 
prinze77 profile image
prinze77

Thanks!

Please show us the same how to deploy on prod symfony docker app.

Collapse
 
_mertsimsek profile image
Mert Simsek

In fact, i prefer Google Cloud App Engine for production, if i need to run Symfony on Docker. Except that, i never have experience custom Docker for production.

Collapse
 
samayo profile image
samayo

Thanks, this looked like what I was looking for until I saw you are using docker-compose v2 ... why? I thought v3 came out a long time ago

Collapse
 
_mertsimsek profile image
Mert Simsek

In fact, this's a habit which i should change for me. From now on, i am gonna pay attention about that. Thank you for reply :)

Collapse
 
samayo profile image
samayo

Thanks. Let me know when you change it, pls.. I have been crawling the internet for the perfect setup I need, and yours is the one!! without the version part

Thread Thread
 
_mertsimsek profile image
Mert Simsek

I really appreciate that, thank you. I am going to inform you for that :)