Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dockerizing Symfony Applications - Symfony Live Berlin 2014

31,055 views

Published on

Published in: Internet

Dockerizing Symfony Applications - Symfony Live Berlin 2014

  1. 1. Dockerizing Symfony Apps Dennis Benkert @denderello
  2. 2. Set out to make service orchestration simple for developers. Based in Cologne, Germany. Ten terrific folks, and hiring! http://giantswarm.io/
  3. 3. So, Docker ... It’s that new kid everybody talks about
  4. 4. Ever heard about it?
  5. 5. IT’S FUN!
  6. 6. It will help you to ... ● run your environment everywhere ● try out infrastructure changes easier ● deploy new releases faster ● run PHP without installing it locally!
  7. 7. How is that possible?
  8. 8. Docker Containers!
  9. 9. Docker’s Building Blocks Watch out. Linux Kernel ahead!
  10. 10. What are Docker Containers?
  11. 11. Linux Containers + Union Filesystems
  12. 12. Linux Containers?
  13. 13. Remember Virtual Machines?
  14. 14. They use Hypervisors
  15. 15. Hypervisors virtualize a whole system
  16. 16. Virtual Machines Machine Kernel Init Hypervisor VM Kernel Init Process VM Kernel Init VM Kernel Init Process Process Process Process Process
  17. 17. Try to start 100 of them on your laptop … ;-)
  18. 18. Linux Containers are lightweight virtualization
  19. 19. 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
  20. 20. Linux Containers Machine Kernel Init Container Process Container Process Container Process
  21. 21. Union Filesystem?
  22. 22. Think of stacked layers
  23. 23. Every write operation opens a new layer
  24. 24. Union filesystem Change /etc/php5/fpm/php.ini Install PHP-FPM Basic Ubuntu RootFS
  25. 25. You will see a combined view of all layers
  26. 26. Imagine your webserver is a layer
  27. 27. And every project release becomes a layer on top
  28. 28. Run this filesystem as a single process
  29. 29. You have Docker Containers!
  30. 30. Docker Containers is chroot on steroids.” — Jérôme Petazzoni (Docker) “
  31. 31. Sounds hard to achieve?
  32. 32. Docker makes this super easy!
  33. 33. Four steps needed
  34. 34. 1. Create a PHP file <?php echo "Hello from PHP";
  35. 35. 2. Create a Dockerfile FROM php ADD index.php /var/www EXPOSE 8080 WORKDIR /var/www Every line becomes a layer ENTRYPOINT ["php", "-S", "0.0.0.0:8080"]
  36. 36. 3. Build your Container $ docker build -t denderello/phptest .
  37. 37. 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
  38. 38. You can run this Container everywhere!
  39. 39. The Dockerhub saves your Containers D push run Dev Prod Docker daemon Docker daemon Docker client Docker client
  40. 40. Let’s get started Symfony, get ready to become dockerized!
  41. 41. Imagine a simple Symfony app
  42. 42. Redis nginx / fpm Symfony A classic setup
  43. 43. Let’s break this up into processes
  44. 44. Seperate the processes Redis fpm nginx
  45. 45. Every process becomes a container
  46. 46. Wait
  47. 47. nginx and fpm need to share the source files
  48. 48. Incorporation of nginx and PHP-FPM nginx fpm Symfony
  49. 49. Let’s add a volume container
  50. 50. Volume containers share folders with other containers
  51. 51. Define the containers Redis fpm nginx Symfony
  52. 52. Redis
  53. 53. Redis Container $ docker run -d -p 6379:6379 --name redis redis:2.8.13
  54. 54. Define the containers Redis
  55. 55. Symfony
  56. 56. Symfony Container 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"]
  57. 57. Entrypoint Bash Script #!/bin/bash rm -rf /var/www/app/cache/* /bin/bash -l -c "$*"
  58. 58. Run the Symfony Container $ docker run denderello/symfony composer install $ docker run denderello/symfony app/console cache:clear
  59. 59. No PHP on the local machine anymore
  60. 60. Define the containers Redis Symfony
  61. 61. PHP-FPM
  62. 62. 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"]
  63. 63. Configure your app with environment variables
  64. 64. 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
  65. 65. Docker links?
  66. 66. Containers have no open ports by default
  67. 67. Exposed ones can be opened to the host
  68. 68. Links open exposed ports between two containers
  69. 69. They will not be open to the host
  70. 70. Run the fpm Container $ docker run -d denderello/fpm --link redis:redis --volumes-from symfony --name fpm
  71. 71. Define the containers Redis fpm Symfony
  72. 72. nginx
  73. 73. 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"]
  74. 74. Run the nginx Container $ docker run -d denderello/nginx --link fpm:fpm --volumes-from symfony --name nginx
  75. 75. The final containers Redis fpm nginx Symfony
  76. 76. Running this setup requires 4 cli commands
  77. 77. This can be done easier
  78. 78. Fig
  79. 79. Fast, isolated development environments using Docker” — The Fig Website “
  80. 80. Define your environment using YAML
  81. 81. fig.yml (excerpt) … nginx: build: nginx/ ports: - 8080:80 links: - fpm volumes_from: - symfony …
  82. 82. fig.yml (excerpt) … symfony: build: symfony/ links: - redis volumes: - ./symfony/code:/var/www … Overrides the the folder when running the container
  83. 83. Starting all containers is just a command away
  84. 84. Using fig $ fig up
  85. 85. Running Symfony commands is easier
  86. 86. Using fig $ fig run symfony composer install $ fig run symfony app/console cache:clear
  87. 87. Hosting this is still a challenge
  88. 88. But there is company setting out to change this
  89. 89. Sign up for our private Beta Request Invite http://giantswarm.io/
  90. 90. Thanks for listening! Reach out: Dennis Benkert @denderello @giantswarm

×