Développer et packager votre application Symfony2 avec Docker et Vagrant

Preview:

DESCRIPTION

Présentation donnée au PHP Tour Lyon 2014

Citation preview

Développer et packager votreapplication Symfony2 avec

Docker et Vagrant

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

1

Proposition À partir d'une application Symfony2 ayant fait ses preuves :

- Utilisateurs

- Intégration continue / déploiements automatisés

=> Passer à Docker (sans Puppet / Chef / Ansible ...)

https://github.com/thierrymarianne/symfony2-docker-vagrant

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

2

Projet interne chez DotCloud, Solomon Hykes

Janvier 2014 : Levée de fond de 15M$

9 Juin 2014 : Sortie de la 1.0 à la DockerCon 2014

10 Juin 2014 : Intégration de Docker dans Google App Engine

Un peu d'histoire

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

3

Machines virtuelles !== Containers

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

5

Développement

Intégration continue

Déploiement

Dans quels contextes utiliser Docker ?

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

6

Pour le développeurDavantage d'indépendance vis à vis des Ops

Meilleure sensation d'être en production sur son poste de travail

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

7

Pour l'opérationnelDavantage d'indépendance vis à vis des Devs

Faciliter la surcharge des configurations utilisées en développement

(variables d'environnements, fichiers de configuration, persistence)

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

8

En pratique Container Machine ("machine virtuelle" rapide)

Container Application (rôle au sens gestion de configuration)

Container Volume de Données (point de montage)

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

9

Intégration avec Vagrant En mode provider

Rôle tenu de manière traditionnel par VirtualBox / VMware

=> Container Machine

$ vagrant init [...]config.vm.provider "docker" do |d| d.image = "afup/nginx"end[...]

01.02.03.04.05.06.07.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

10

"Machine virtuelle" légère ?Utilisation des images de base Phusion

/sbin/my_init (collecte des processus orphelins)

Runit à la place d'upstart (supervision de service)

Syslog-ng / logrotate

ssh

/!\ Non-recommandé par Michael Crosby

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

11

Intégration avec Vagrant• Provisioning

Vagrant.configure("2") do |config| config.vm.provision "docker", images: ["afup/symfony2"]end

• Rôle aussi tenu par Shell / Puppet / Chef

=> Container Application

01.02.03.04.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

12

Selon le fondateur de Docker

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

13

Ou alors, Boot2Docker! Linux très léger optimisé pour lancer des containers rapidement

Installation pour OSX / Windows

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

14

Comment utiliser Docker?Ligne de commande / Dockerfile

https://docs.docker.com/installation/

$ docker -h

Outils de configuration / orchestration

# pip install -U fig

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

15

Application Symfony2 associée à un indexde recherche

16

Le contenu de notre Dockerfile pour notre container Elasticsearch :

$ vi Dockerfile FROM tutum/elasticsearchEXPOSE 9200VOLUME ['/var/lib/elasticsearch']ENTRYPOINT ["/usr/share/elasticsearch/bin/elasticsearch"]

Ne pas réinventer la roue

01.02.03.04.05.06.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

17

docker build -t afup/elasticsearch:0.1 .# => nouvelle image créée pour le référentiel# afup/elasticsearch

# Envoi sur index publicdocker push afup/elasticsearch:0.1

# Récupération à partir de l'index publicdocker pull afup/elasticsearch:0.1

Contruire une image Elasticsearch01.02.03.

01.02.

01.02.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

18

docker run -d -p :9200 \--name elasticsearch-server \-v ̀pwd̀/elasticsearch:/var/lib/elasticsearch \afup/elasticsearch:0.1

Démarrer notre container à partir de l'image01.02.03.04.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

19

docker run -d -p :9200 \--name elasticsearch-server \--volumes-from elasticsearch-data-volume \-v ̀pwd̀/elasticsearch:/var/lib/elasticsearch \afup/elasticsearch:0.1

Tagger notre container Elasticsearch01.02.03.04.05.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

20

docker run -d -p :9200 \--name elasticsearch-server \--volumes-from elasticsearch-data-volume \-v ̀pwd̀/elasticsearch:/var/lib/elasticsearch \afup/elasticsearch:0.1

Déclarer un Container Volume de Données01.02.03.04.05.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

21

Avec montage de notre application Symfony2

docker run -t -i -p 80:80 \--name php-nginx-server \--link elasticsearch-server:symfony__elasticsearch_ \-v symfony2:/var/www/symfony2 \-v ̀pwd̀/nginx/sites-enabled:/etc/nginx/sites-enabled \# [...]afup/nginx:0.1

Lancer un Container Application (nginx)

01.02.03.04.05.06.07.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

22

docker run -t -i -p 80:80 \--name php-nginx-server \--link elasticsearch-server:symfony__elasticsearch_ \-v symfony2:/var/www/symfony2 \-v ̀pwd̀/nginx/sites-enabled:/etc/nginx/sites-enabled \afup/nginx:0.1

Lier nos containers Elasticsearch et nginx01.02.03.04.05.06.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

23

# Définition de paramètres de configuration en yamlparameters: elasticsearch_host: %elasticsearch.host% elasticsearch_port: %elasticsearch.port% gmail_search_index: gmail twitter_search_index: twitter

Configuration de notre application01.02.03.04.05.06.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

24

docker run -t -i -p 80:80 \--name php-nginx-server \--e SYMFONY__ELASTICSEARCH__HOST=127.0.0.1-v symfony2:/var/www/symfony2 \-v ̀pwd̀/nginx/sites-enabled:/etc/nginx/sites-enabled \afup/nginx:0.1

root@ad09c8a66671:# dans mon container Elasticsearchroot@ad09c8a66671:envroot@ad09c8a66671:SYMFONY__ELASTICSEARCH__HOST=127.0.0.1

Variables d'environnement (1/2)Injection d'une variable au démarrage d'un container

01.02.03.04.05.06.

01.02.03.

25

docker run -t -i -p 80:80 \--name php-nginx-server \--link elasticsearch-server:symfony__elasticsearch_ \-v symfony2:/var/www/symfony2 \-v ̀pwd̀/nginx/sites-enabled:/etc/nginx/sites-enabled \afup/nginx:0.1

SYMFONY__ELASTICSEARCH__HOST <=> %elasticsearch.host%

Lier deux containers transfère les variables d'environnement

Variables d'environnement (2/2)01.02.03.04.05.06.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

26

FROM ubuntu:14.04ENV DEBIAN_FRONTEND noninteractive # RUN apt-get upgrade à bannir RUN apt-get updateRUN apt-get install -y --force-yes software-properties-common

Pro Tip: Abuser des images officielles01.02.03.04.05.06.07.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

27

sudo apt-get install squid-deb-proxy avahi-utils

https://github.com/yasn77/docker-squid-repo-cache.git

Pro Tip: Proxy Packages Debian (1/2)Proxy pour gestionnaire de paquets sur la machine hôte

Peut être même dans son propre container ?

01.02.03.

01.02.03.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

28

RUN route -n | awk '/̂0.0.0.0/ {print $2}' > /tmp/host_ip.txtRUN echo "HEAD /" | nc ̀cat /tmp/host_ip.txt̀ 8000 | \grep squid-deb-proxy \ && (echo "Acquire::http::Proxy \"http://$(cat /tmp/host_ip.txt):8000\";" > \ /etc/apt/apt.conf.d/30proxy) \ && (echo "Acquire::http::Proxy::ppa.launchpad.net DIRECT;" >> \ /etc/apt/apt.conf.d/30proxy) \ || echo "No squid-deb-proxy detected on docker host"

Pro Tip: Proxy Packages Debian (2/2)Dans mon Dockerfile, avant l'installation de paquets :01.02.03.04.05.06.07.08.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

29

RUN apt-get install -y --force-yes nginxRUN apt-get install -y --force-yes php5-fpm

Différent deRUN apt-get install -y --force-yes php5-fpmRUN apt-get install -y --force-yes nginx

Pro Tip: Tirer parti du cache de DockerDans vos Dockerfiles :

Conserver au maximum l'ordre des instructions!01.02.

01.02.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

30

Pro Tip: Proxy Toran pour composerPar l'un des lead développeurs de Composer (Jordi Boggiano)

Accélérer l'installation des vendors

Sert de point de redondance avec github / référentiels privés

=> Container Application

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

31

# fig.yml nginx: build: /home/afup/php-nginx links: - elasticsearch:symfony__elasticsearch_ [...] ports: - "8081:80" expose: - "80"

Et si on se débarrassait (en partie) du shell ?01.02.03.04.05.06.07.08.09.10.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

32

elasticsearch: image: afup/elasticsearch:0.1 volumes_from: elasticsearch-data-volume expose: - "9200" ports: - ":9200"

Fig

Où se cache notre application Symfony2 cette fois ?

01.02.03.04.05.06.07.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

33

nginx: [...] volumes: - nginx/sites-enabled:/etc/nginx/sites-enabled - symfony2:/var/www/symfony2 $ fig up

Fig01.02.03.04.05.06.07.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

34

La vie des containers avec Cadvisor

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

35

Aller encore plus loin avec OpenStackAvec OpenStack (cloud manager)

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

36

description: > Stack Heat (Orchestration)resources: nginx: type: DockerInc::Docker::Container properties: image: afup/php-nginx:0.1 elasticsearch: type: DockerInc::Docker::Container [...]

Démarrer une "Stack" avec Heat

heat stack-create nginx -f ../openstack/php-nginx.yml

01.02.03.04.05.06.07.08.09.10.

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

37

Et ce n'est que le début !Orchestration avec Maestro-Ng

Configuration visuelle avec Gaudi

Déploiement avec Deis / CoreOS

Support de Docker avec AppEngine

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

38

Showtime• /!\ Effet démo en perspective !!

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

39

Questions ?

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

40

Sources• https://linuxcontainers.org

• http://en.wikipedia.org/wiki/Chroot

• http://en.wikipedia.org/wiki/Cgroups

• https://www.docker.io

• https://hub.docker.com/

• https://speakerdeck.com/ubermuda/a-multi-container-symfony2-setup-with-docker

• https://wiki.openstack.org/wiki/Solum

• https://speakerdeck.com/ubermuda/a-multi-container-symfony2-setup-with-docker

• http://blog.docker.com/2014/04/openstack-update-icehouse-release-update/

• https://github.com/phusion/baseimage-docker

• http://crosbymichael.com/dockerfile-best-practices.html

• https://toranproxy.com/

• http://openstack.redhat.com/Quickstart/

https://joind.in/11240 (@thierrymarianne | thierrym@theodo[point]fr)

41

Recommended