getting instantly up and running with docker and symfony

19
GETTING INSTANTLY UP AND RUNNING WITH DOCKER AND SYMFONY GETTING STARTED AND BEYOND WITH DOCKER AND SYMFONY André Rømcke - VP Engineering @ezsystems Symfony Live London, 16th of September 2016

Upload: andre-romcke

Post on 15-Apr-2017

593 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Getting instantly up and running with Docker and Symfony

GETTING INSTANTLY UP AND RUNNINGWITH DOCKER AND SYMFONYGETTING STARTED AND BEYOND WITH DOCKER AND SYMFONY

André Rømcke - VP Engineering @ezsystems

Symfony Live London, 16th of September 2016

Page 2: Getting instantly up and running with Docker and Symfony

www.ez.no

Warning

๏Won’t cover performance issues on Win/Mac in dev mode due to very slow FS

๏Pragmatic “fix” is to use Linux, or VM with files shared to host over nfs

๏Besides that there are third party solutions with own pitfalls, eg docker-sync

๏This is the first time I do this talk, so hold on tight

๏Including demo effect ;)

Page 3: Getting instantly up and running with Docker and Symfony

www.ez.no

Who?

๏André Rømcke | @andrerom

๏Norwegian, like being social, mountains, skiing, biking, running, technology

๏PHP for 11 years. From 96: Html, CSS, JS, VB, C#, PHP, Bash, Groovy, (Hack)

๏Contributed to Symfony, FOS, Composer, FIG, Docker, and attempting for PHP

๏VP Engineering at eZ Systems

๏eZ Systems AS | ez.no

๏Global, 70 people across 7 countries, partners & community in many many more

๏Maker of eZ Publish since 2001, 6th+ gen called eZ Studio (commercial) & eZ Platform

๏eZ Platform | ezplatform.com

๏Open source Content Management System, a super flexible Full and Headless CMS

๏Developed since 2011, on Symfony Full Stack since 2012 (v2.0)

Page 4: Getting instantly up and running with Docker and Symfony

www.ez.no

Getting started basic example

The instant part, what we’ll cover:

1.Clone Symfony standard

2.Add Dockerfile for building our application container

3.Add docker-compose.yml to define our software services

4.[Optional] Add .dockerignore file to hint which files should be ignored

5.[Optional] Add .env variable for default variables

Page 5: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #1 Clone Symfony standard

$ git clone https://github.com/symfony/symfony-standard.git

$ cd symfony-standard

# Or using composercreate-project, or using Symfony installer, ..

Page 6: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #2 Add basic Dockerfile

$ vim Dockerfile

FROM php:7.0-apacheMAINTAINER André Rømcke "[email protected]"ENV COMPOSER_HOME=/root/.composer SYMFONY_ENV=prodCOPY . /var/www/html# … (install Composer, or skip the composer instal step below)RUN composer install -o -n --no-progress --prefer-dist \ && echo "Clear cache and logs so container starts clean" \ && rm -Rf var/logs/* var/cache/*/* \ && echo "Set perm for www-data, add data folder if you have" \ && chown -R www-data:www-data var/cache var/logs var/sessionsEXPOSE 80CMD ["apache2-foreground"]

Page 7: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #3 Add basic docker-compose.yml

$ vim docker-compose.yml

version: '2'services: web: build: . image: my_web ports: [ "8088:80" ] environment: - SYMFONY_ENV - SYMFONY_DEBUG

Page 8: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #4 Add basic .dockerignore

$ vim .dockerignore

# GIT files.git/

# Cache, session files and logs (Symfony3)var/cache/*var/logs/*var/sessions/*!var/cache/.gitkeep!var/logs/.gitkeep!var/sessions/.gitkeep

Page 9: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #5 Add default .env variables

Or having to hard code and duplicate defaults in your docker-compose.yml:

environment: - SYMFONY_ENV=prod - SYMFONY_DEBUG=0

Instead of having to always specify ENV variables on command line:

export SYMFONY_ENV=prod SYMFONY_DEBUG=0docker-compose up -d

Page 10: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Getting started: #5 Add default .env variables

Instead we can set all default in one file: $ vim .env

# Default env variables, see: https://docs.docker.com/compose/env-file/#COMPOSE_FILE=docker-compose.ymlSYMFONY_ENV=prodSYMFONY_DEBUG=0

Page 11: Getting instantly up and running with Docker and Symfony

Demo time Up and running

git clone https://github.com/andrerom/symfony-live-london-2016-docker-demo.git sf_demo cd sf_demo git showdocker up -d

Page 12: Getting instantly up and running with Docker and Symfony

Conventions in official docker images order in docker, in official images, and in php image

As the getting started example is way to simple, lets go over some things to know before we make a more advance example.

We’ll cover:

1.Entrypoints

2.Data dirs

3.EVN variables & Symfony

4.Image Layers (and how it affects image size)

Page 13: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Convention: Entrypoint

Found in for instance official mysql/mariadb, postgres, solr images:

# https://github.com/docker-library/mariadb/blob/master/10.1/docker-entrypoint.sh for f in /docker-entrypoint-initdb.d/*; do case "$f" in *.sh) echo "$0: running $f"; . "$f" ;; *.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;; *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;; *) echo "$0: ignoring $f" ;; esac echo done

This lets you to pass in sql and/or shell scripts to enhance a official image without having to extend it with own docker image:

db: image: ${MYSQL_IMAGE} volumes_from: - db_vol:ro

Page 14: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Convention: Data dir

Found in for instance mysql/mariadb, postgres, elastic:

Same benefit as entrypoint usage with sql files, but more specifically for mounting backup data for start up when starting a new environment.

VOLUME /var/lib/mysql

VOLUME /usr/share/elasticsearch/data

VOLUME /var/lib/postgresql/data

Page 15: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Convention: ENV variables with Symfony

As “SYMFONY__” ENV variables are broken, common solution is:

env/docker.php

imports: - { resource: default_parameters.yml } - { resource: parameters.yml } - { resource: security.yml } - { resource: env/docker.php}

<?php// Executed on symfony container compilationif ($value = getenv('SYMFONY_SECRET')) { $container->setParameter('secret', $value); }

Page 16: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Convention: ENV variables with Symfony

But this is hopefully not needed anymore, as of 3.2 we will have this:

Page 17: Getting instantly up and running with Docker and Symfony

www.ez.nowww.ez.no

Convention: Image Layers & image size

• Each instruction in Dockerfile will by default create a new layer • Which means files added in one layer continues taking space in final image • Which means you will have to do things like:

# Install and configure php pluginsRUN set -xe \ && buildDeps=" \ $PHP_EXTRA_BUILD_DEPS \ libfreetype6-dev \ libjpeg62-turbo-dev libxpm-dev libpng12-dev libicu-dev libxslt1-dev \ && apt-get update && apt-get install -y --force-yes $buildDeps --no-install-recommends && rm -rf /var/lib/apt/lists/* \ # Extract php source and install missing extensions && docker-php-source extract \ && (…)# Delete source & builds deps so it does not hang around in layers taking up space && docker-php-source delete \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $buildDeps

Page 18: Getting instantly up and running with Docker and Symfony

Advance use extending, composing and using

Lets have a look at some actual code.

Sources:

- https://github.com/ezsystems/docker-php/blob/master/php/Dockerfile-7.0#L33

- https://github.com/ezsystems/ezplatform/tree/master/doc/docker-compose

Page 19: Getting instantly up and running with Docker and Symfony

Fin the end, but hopefully just the beginning

For more on eZ Platform see ezplatform.com