Speed up web dev using Docker

+

Cédric Vanet

 

Symfony 2

Docker

HTML 5

CSS 3

Javascript

Less

PHP 5

Shell

Linux

Gulp

ZSH

Bower

Composer

DevOps Symfony 2

@cedvan

Web application needs

  • Management sources : Composer
  • Web server
    • Apache 2 + PHP
    • Nginx + PHP
  • Data storage
    • MySQL
    • PostGreSQL
    • ...
  • Management assets : Gulp, Grunt, ...
  • Package manager : NPM
  • Check style : Eslint, Coke
  • Tests : Selenium, PhantomJS, ZombieJS, ...

Problems

  • Installation tools is long and hard
    • Integration new developer is expensive
    • Loss of productivity when a problem with computer
    • Requires operator's knowledge
  • How to get same dev environments in a team ?
    • Disparity of bugs between machine
    • Different configuration tools
    • Different dev operating system (Windows, Mac, Linux, ...)
  • Develop on an iso prod environment

Solutions ?

  • Install tools manually on developers computer
    • Check versions
    • All versions aren't available on differents OS
    • Increase the installation time in function of number of machines
  • Use virtual machines
    • Slow
    • Take bi rom space (several Go)
    • Share vm package is hard (Usb, network,  ...)

Need a real solution...

Docker

This presentation is not intended to detail docker, I have deliberately simplified the explanations and took a few shortcuts

Docker is an open platform for developing, shipping, and running applications. 

Introduce

Equivalent to virtual machines but completely different :

Docker concept

Docker aims : provide applications in containers based on prebuild images

  • Light : Share components with host
  • Fast : Vitualize only one application 
  • Easy : Use with CLI

Image concept

An image is a frozen representation of a type of container

FROM cedvan/ubuntu:14.04.20150311
MAINTAINER dev@cedvan.com

# Install Nginx
RUN apt-get update && apt-get install -y \
    nginx

EXPOSE 80
EXPOSE 443

WORKDIR /src

CMD nginx -c /etc/nginx/nginx.conf

Dockerfile (Nginx image)

It is built using a Dockerfile, which is the template system used by Docker. A Dockerfile consists of a "suite of bash commands"

An image can inherit from another image with FROM

We just share it on the web by a public or private docker registry

Nginx image on console host

Container concept

A container is constructed from an image

The image can be built in local or pull from web

A container represents an application. Executed in service or daemon 

A common host can launch greather than 200 containers simultaneously

211 containers nginx started

Create container nginx

Implement in web application

Image Web

  • Executed as daemon
  • Traitement request HTTP
  • Execution PHP Code 
FROM cedvan/nginx:1.4.6
MAINTAINER dev@cedvan.com

# Install PHP
RUN apt-get update -qq \
    && apt-get install -y \
    php5-dev \
    php5-cli \
    php5-xdebug \
    php5-intl \
    php5-curl \
    php5-mysql \
    php5-pgsql \
    php5-fpm \
    php5-apcu \
    php5-ldap \
    php5-gd \
    php-pear

CMD php5-fpm -R && nginx -c /etc/nginx/nginx.conf

Image Nginx

Image Database

  • Executed as daemon
  • Storage data
  • Execution requests SQL 

FROM sameersbn/ubuntu:14.04.20150220
MAINTAINER sameer@damagehead.com

ENV PG_VERSION 9.4
RUN apt-get update -qq \
 && apt-get install -qqy \
    postgresql-${PG_VERSION} \
    postgresql-client-${PG_VERSION} \
    postgresql-contrib-${PG_VERSION} \
    pwgen \
 && rm -rf /var/lib/postgresql \
 && rm -rf /var/lib/apt/lists/*

ADD start /start
RUN chmod 755 /start

EXPOSE 5432

VOLUME ["/var/lib/postgresql"]
VOLUME ["/run/postgresql"]

CMD ["/start"]

Image PostGreSQL

Images tools

  • Executed as service
  • Tasks Traitement
    • ​Sources
    • Assets
    • Tests
    • ...
FROM cedvan/ubuntu:14.04.20150206
MAINTAINER Cédric Vanet <dev@cedvan.com>

# Install git
RUN apt-get update -qq \
    && apt-get install -qqy git

# install HHVM
RUN wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key \
    | apt-key add -
RUN echo deb http://dl.hhvm.com/ubuntu trusty main \
    | tee /etc/apt/sources.list.d/hhvm.list
RUN apt-get update -qq && apt-get install -qqy hhvm

# Configuration HHVM
RUN echo "date.timezone = Europe/Paris" >> /etc/hhvm/php.ini
COPY assets/config.hdf /etc/hhvm/config.hdf

# install composer
RUN wget -O /usr/local/bin/composer -q \
    http://getcomposer.org/composer.phar
RUN chmod +x /usr/local/bin/composer

# Launch composer cith hhvm
ADD assets/composer-hhvm bin/composer-hhvm
RUN chmod +x /bin/composer-hhvm

# Update Composer
RUN /bin/composer-hhvm self-update

WORKDIR /src

ENTRYPOINT ["composer-hhvm"]

Image Composer using HHVM

  • Composer
  • Bower
  • NPM
  • Selenium
  • ...

Orchestration

Docker compose

docker compose manager containers of our application of a simple and efficient way

Program developed in python and installable with NPM. But not installed on host, it's an image of more !

Uses a single file
docker-compose.yml

docker-compose.yml

Fig ago

proxy:
    image: jwilder/nginx-proxy
    ports:
        - "80:80"
    volumes:
        - /var/run/docker.sock:/tmp/docker.sock

web:
    image: cedvan/nginx-php-fpm:latest
    working_dir: /src
    volumes:
        - $PATH_PROJECT/docker/vhosts:/etc/nginx/sites-enabled
        - $PATH_PROJECT:/src
        - $PATH_PROJECT/var/logs:/var/log/nginx
    links:
        - db
    environment:
        VIRTUAL_HOST: "projet.dev"

db:
    image: sameersbn/docker-postgresql:9.4-1
    environment:
        VIRTUAL_HOST: "db.projet.dev"
        DB_NAME: "project, project_test"
        DB_USER: "project"
        DB_PASS: "project"
        DB_UNACCENT: "true"

composer:
    image: cedvan/composer:latest
    working_dir: /src
    volumes:
        - /var/tmp/composer/cache:/root/.composer/cache
        - $PATH_PROJECT:/src
    net: "host"

builder:
    image: dockerfile/nodejs-bower-grunt
    working_dir: /src
    volumes:
        - $PATH_PROJECT:/src

Makefile

Automation common commands

Little skill required, ideal for common developers

Productivity Gain daily

Makefile

sf=php bin/console
fig=docker run \
    --rm -it \
    -v $(PWD):/src \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -e "PATH_PROJECT=$(PWD)" \
    cedvan/compose:latest

pull:
    @$(fig) pull

start:
    @$(fig) up -d web

stop:
    @$(fig) stop

cc:
    @$(fig) run --rm web $(sf) cache:clear --env=dev

composer-install:
    @$(fig) run --rm composer install -n --prefer-dist

composer-update:
    @$(fig) run --rm composer update -n --prefer-dist

bower-install:
    @$(fig) run --rm builder bower --allow-root install

gulp:
    @$(fig) run --rm builder gulp --dev

coke:
    @$(fig) run --rm web bin/coke

eslint:
    @$(fig) run --rm builder eslint *

atoum:
    @$(fig) run --rm web bin/atoum

...

Result

Questions ?

Have fun !

follow me

MERCI

Copy of Speed up web dev using docker

By Arnaud Benassy

Copy of Speed up web dev using docker

Docker is the solution !

  • 852
Loading comments...

More from Arnaud Benassy