dockerizing symfony applications - symfony live berlin 2014

91
Dockerizing Symfony Apps Dennis Benkert @denderello

Upload: dennis-benkert

Post on 21-Apr-2017

29.103 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Dockerizing Symfony Apps

Dennis Benkert@denderello

Page 2: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Set out to make service orchestration simple for developers.

Based in Cologne, Germany.Ten terrific folks, and hiring!

http://giantswarm.io/

Page 3: Dockerizing Symfony Applications - Symfony Live Berlin 2014

It’s that new kid everybody talks about

So, Docker ...

Page 4: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Ever heard about it?

Page 5: Dockerizing Symfony Applications - Symfony Live Berlin 2014

IT’S FUN!

Page 6: Dockerizing Symfony Applications - Symfony Live Berlin 2014

It will help you to ...

● run your environment everywhere● try out infrastructure changes easier● deploy new releases faster● run PHP without installing it locally!

Page 7: Dockerizing Symfony Applications - Symfony Live Berlin 2014

How is that possible?

Page 8: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Docker Containers!

Page 9: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Watch out. Linux Kernel ahead!

Docker’sBuilding Blocks

Page 10: Dockerizing Symfony Applications - Symfony Live Berlin 2014

What are Docker Containers?

Page 11: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Linux Containers+

Union Filesystems

Page 12: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Linux Containers?

Page 13: Dockerizing Symfony Applications - Symfony Live Berlin 2014

RememberVirtual Machines?

Page 14: Dockerizing Symfony Applications - Symfony Live Berlin 2014

They use Hypervisors

Page 15: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Hypervisors virtualize

a whole system

Page 16: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Virtual Machines

Machine

Kernel

Init

Hypervisor

VM

Kernel

InitProcess

VM

Kernel

Init

VM

Kernel

Init

Process

Process

Process

Process

Process

Page 17: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Try to start 100 of them on your

laptop … ;-)

Page 18: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Linux Containers are lightweight virtualization

Page 19: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Linux Containers ...

● run in their own Kernel namespace● are standalone processes● only see processes inside them● cannot see outside processes● share the kernel instance● can have their own filesystem● can be isolated using CGroups

Page 20: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Linux Containers

Machine

Kernel

Init

Container

Process

Container

Process

Container

Process

Page 21: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Union Filesystem?

Page 22: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Think of stacked layers

Page 23: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Everywrite operation

opens a new layer

Page 24: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Union filesystem

Basic Ubuntu RootFS

Install PHP-FPM

Change /etc/php5/fpm/php.ini

Page 25: Dockerizing Symfony Applications - Symfony Live Berlin 2014

You will see a combined view of

all layers

Page 26: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Imagine your webserver is a

layer

Page 27: Dockerizing Symfony Applications - Symfony Live Berlin 2014

And every project release becomes a

layer on top

Page 28: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Run this filesystem as a single process

Page 29: Dockerizing Symfony Applications - Symfony Live Berlin 2014

You haveDocker

Containers!

Page 30: Dockerizing Symfony Applications - Symfony Live Berlin 2014
Page 31: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Docker Containers is chroot on steroids.”— Jérôme Petazzoni (Docker)

Page 32: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Sounds hard to achieve?

Page 33: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Docker makes this super easy!

Page 34: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Four steps needed

Page 35: Dockerizing Symfony Applications - Symfony Live Berlin 2014

1. Create a PHP file

<?php

echo "Hello from PHP";

Page 36: Dockerizing Symfony Applications - Symfony Live Berlin 2014

2. Create a Dockerfile

FROM php

ADD index.php /var/www

EXPOSE 8080

WORKDIR /var/www

ENTRYPOINT ["php", "-S", "0.0.0.0:8080"]

Every line

becomes a layer

Page 37: Dockerizing Symfony Applications - Symfony Live Berlin 2014

3. Build your Container

$ docker build -t denderello/phptest .

Page 38: Dockerizing Symfony Applications - Symfony Live Berlin 2014

4. Run your Container

$ docker run -d -p 8080:8080 denderello/phptest

$ curl 127.0.0.1:8080

Hello from PHP

Binds local port to exposed port

Page 39: Dockerizing Symfony Applications - Symfony Live Berlin 2014

You can run this Container

everywhere!

Page 40: Dockerizing Symfony Applications - Symfony Live Berlin 2014

The Dockerhub saves your Containers

D

Dev Prod

Docker daemon Docker daemon

Docker client Docker client

push run

Page 41: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Symfony, get ready to become dockerized!

Let’s get started

Page 42: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Imagine a simple Symfony app

Page 43: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redisnginx / fpm

Symfony

A classic setup

Page 44: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Let’s break this up into processes

Page 45: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis nginx

Seperate the processes

fpm

Page 46: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Every process becomes a container

Page 47: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Wait

Page 48: Dockerizing Symfony Applications - Symfony Live Berlin 2014

nginx and fpm need to share the

source files

Page 49: Dockerizing Symfony Applications - Symfony Live Berlin 2014

nginx

Incorporation of nginx and PHP-FPM

fpm

Symfony

Page 50: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Let’s add avolume container

Page 51: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Volume containers share folders with other containers

Page 52: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis nginx

Define the containers

fpm

Symfony

Page 53: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis

Page 54: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis Container

$ docker run -d -p 6379:6379 \

--name redis redis:2.8.13

Page 55: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis

Define the containers

Page 56: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Symfony

Page 57: Dockerizing Symfony Applications - Symfony Live Berlin 2014

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y \

git curl php5-cli php5-json php5-intl

RUN curl -sS https://getcomposer.org/installer | php

RUN mv composer.phar /usr/local/bin/composer

ADD entrypoint.sh /entrypoint.sh

ADD ./code /var/www

VOLUME /var/www

WORKDIR /var/www

ENTRYPOINT ["/entrypoint.sh"]

CMD ["echo", "hello"]

Symfony Container

Page 58: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Entrypoint Bash Script

#!/bin/bash

rm -rf /var/www/app/cache/*

/bin/bash -l -c "$*"

Page 59: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Run the Symfony Container

$ docker run denderello/symfony composer install

$ docker run denderello/symfony \

app/console cache:clear

Page 60: Dockerizing Symfony Applications - Symfony Live Berlin 2014

No PHP on the local machine

anymore

Page 61: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis

Define the containers

Symfony

Page 62: Dockerizing Symfony Applications - Symfony Live Berlin 2014

PHP-FPM

Page 63: Dockerizing Symfony Applications - Symfony Live Berlin 2014

php-fpm Container

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y \

php5-fpm php5-json php5-intl

ADD entrypoint.sh /entrypoint.sh

EXPOSE 9000

WORKDIR /var/www

ENTRYPOINT ["/entrypoint.sh"]

Page 64: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Configure your app with

environment variables

Page 65: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Entrypoint Bash Script

#!/bin/bash

echo "env[SYMFONY__REDIS_PORT] = ${REDIS_PORT_6379_TCP_PORT}" \

>> /etc/php5/fpm/pool.d/www.conf

echo "env[SYMFONY__REDIS_ADDRESS] = ${REDIS_PORT_6379_TCP_ADDR}" \

>> /etc/php5/fpm/pool.d/www.conf

exec /usr/sbin/php5-fpm --nodaemonize

Docker will set these for links

Page 66: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Docker links?

Page 67: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Containers have no open ports by

default

Page 68: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Exposed ones can be opened to the

host

Page 69: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Links open exposed ports between two containers

Page 70: Dockerizing Symfony Applications - Symfony Live Berlin 2014

They will not be open to the host

Page 71: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Run the fpm Container

$ docker run -d denderello/fpm \

--link redis:redis \

--volumes-from symfony \

--name fpm

Page 72: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis

Define the containers

fpm

Symfony

Page 73: Dockerizing Symfony Applications - Symfony Live Berlin 2014

nginx

Page 74: Dockerizing Symfony Applications - Symfony Live Berlin 2014

nginx Container

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y \

nginx

RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

ADD vhost.conf /etc/nginx/sites-enabled/default

ADD entrypoint.sh /entrypoint.sh

EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]

Page 75: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Run the nginx Container

$ docker run -d denderello/nginx \

--link fpm:fpm \

--volumes-from symfony \

--name nginx

Page 76: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Redis nginx

The final containers

fpm

Symfony

Page 77: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Running this setup requires 4 cli commands

Page 78: Dockerizing Symfony Applications - Symfony Live Berlin 2014

This can be done easier

Page 79: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Fig

Page 80: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Fast, isolated development environments using Docker”— The Fig Website

Page 81: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Define your environment using

YAML

Page 82: Dockerizing Symfony Applications - Symfony Live Berlin 2014

fig.yml (excerpt)

…nginx:

build: nginx/

ports:

- 8080:80

links:

- fpm

volumes_from:

- symfony

Page 83: Dockerizing Symfony Applications - Symfony Live Berlin 2014

fig.yml (excerpt)

…symfony:

build: symfony/

links:

- redis

volumes:

- ./symfony/code:/var/www

Overrides the the folder when running the container

Page 84: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Starting all containers is just a command away

Page 85: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Using fig

$ fig up

Page 86: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Running Symfony commands is

easier

Page 87: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Using fig

$ fig run symfony composer install

$ fig run symfony app/console cache:clear

Page 88: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Hosting this is still a challenge

Page 89: Dockerizing Symfony Applications - Symfony Live Berlin 2014

But there is company setting

out to change this

Page 90: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Sign up for our private Beta

Request Invite

http://giantswarm.io/

Page 91: Dockerizing Symfony Applications - Symfony Live Berlin 2014

Thanks for listening!Reach out:Dennis Benkert@denderello@giantswarm