Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to deploy PHP projects with docker

Ruoshi Ling
October 18, 2014

How to deploy PHP projects with docker

Ruoshi Ling

October 18, 2014
Tweet

More Decks by Ruoshi Ling

Other Decks in Technology

Transcript

  1. Who am I ? • Ruoshi Lin (@fntsrlike)! • 深藏若虛!

    • 2.5 Y PHPer ! • fntsr.tw Photo by
  2. Laravel Symfony CodeIgniter Phalcon Yii Wordpress Drupal 7 Drupal 8

    Drupal 6 Joomla MediaWiki DokuWiki There are so many! PHP Projects… PHP Version Requirement
  3. Laravel 5.4+ Symfony 5.3.3+ CodeIgniter 5.1.6+ Phalcon 5.3+! Yii 5.1+

    Wordpress 5.2.4+ Drupal-7 5.2.5+ Drupal-8 5.4+ Drupal-6 5.2.x Joomla! 5.4+ MediaWiki 5.3.2+ DokuWiki 5.2 But version ! requirement ! are different!? PHP Version Requirement
  4. ` Laravel 5.4+ Symfony 5.3.3+ CodeIgniter 5.1.6+ Phalcon 5.3+! Yii

    5.1+ Wordpress 5.2.4+ Drupal-7 5.2.5+ Drupal-8 5.4+ Drupal-6 5.2.x Joomla! 5.4+ MediaWiki 5.3.2+ DokuWiki 5.2 WHICH PHP VERSION ! SHOULD I INSTALLED ?
  5. Multifarious Extension apxs2, bcmath, bz2, calendar, cgi, cli, ctype, dba,

    debug, dom, embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, hash, iconv, icu, imap, intl, ipc, ipv6, json, kerberos, mbregex, mbstring, mcrypt, mhash, mysql, openssl, pcntl, pcre, pdo, pgsql, phar, posix, readline, session, soap, sockets, sqlite, tidy, tokenizer, xml_all, xmlrpc, zip, zlib
  6. Multifarious Extension apxs2, bcmath, bz2, calendar, cgi, cli, ctype, dba,

    debug, dom, embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, hash, iconv, icu, imap, intl, ipc, ipv6, json, kerberos, mbregex, mbstring, mcrypt, mhash, mysql, openssl, pcntl, pcre, pdo, pgsql, phar, posix, readline, session, soap, sockets, sqlite, tidy, tokenizer, xml_all, xmlrpc, zip, zlib PECL? ! Recompiled?
  7. One Server Multi Projects Server App 1 PHP MySQL etc…

    App 2 PHP MySQL etc… App n PHP, DB, etc… ……
  8. One Server Multi Projects Server App 1 PHP 5.3.2, MySQL

    5.0.2, etc… App 2 PHP 5.4, MySQL 5.0.15, etc… App n PHP 5.4, MariaDB, etc… …… BUT DIFFERENT REQUIRMENT
  9. How docker can help us? Built specify environment for target

    project.! Isolate every project space.
  10. How docker can help us? Built specify environment for target

    project.! Isolate every project space.! Package it and be portable.
  11. How docker can help us? Built specify environment for target

    project.! Isolate every project space.! Package it and be portable.! Deploy lightweight and fast.
  12. What is it? Build, Ship and Run Any App, Anywhere

    Docker - An open platform for distributed applications for developers and sysadmins. -docker.io
  13. LXC Linux Container is a userspace interface ! for the

    Linux kernel containment features.
  14. LXC Linux Container is a userspace interface ! for the

    Linux kernel containment features. What?
  15. LXC ! Kernel 
 namespaces! Cgroups Chroot PID, mount, user,

    network, UTS, IPC! cpu, memory, disk I/O! File system!
  16. Virtual Machine Server Host OS Hypervisor Guest OS /usr/bin, /usr/lib,

    … App Guest OS /usr/bin, /usr/lib, … App 2 Guest OS /usr/bin, /usr/lib, … App'
  17. Virtual Machine Server Host OS Hypervisor Guest OS /usr/bin, /usr/lib,

    … App Guest OS /usr/bin, /usr/lib, … App 2 Guest OS /usr/bin, /usr/lib, … App' VM1 VM2 VM3
  18. Virtual Machine Server Host OS Hypervisor Guest OS PHP 5.2

    PHP App Guest OS PHP 5.4 PHP App2 Guest OS PHP 5.3 PHP App' VM1 VM2 VM3
  19. Docker Server Host OS /bin, /lib PHP ! App3 Docker

    /bin, /lib PHP ! App4 /bin, /lib PHP ! App PHP ! App`
  20. Docker Server Host OS /bin, /lib PHP ! App3 Docker

    Docker container /bin, /lib PHP ! App4 /bin, /lib PHP ! App PHP ! App` /bin, /lib
  21. Docker Server Host OS PHP 5.3 PHP ! App3 Docker

    Docker container PHP 5.4 PHP ! App4 PHP 5.2 PHP ! App PHP ! App` PHP 5.2
  22. Docker Server Host OS PHP 5.3 PHP ! App3 Docker

    Docker container PHP 5.4 PHP ! App4 Shared FS with AUFS PHP 5.2 PHP ! App PHP ! App` PHP 5.2
  23. Docker Container Kernel LXC AUFS Docker Base image (Ubuntu) Image

    (php) Image (apache) Container Base image (CentOS) Image (nginx) Container Container
  24. Docker Container Kernel LXC AUFS Docker Base image (Ubuntu) Image

    (php) Image (apache) Container Base image (CentOS) Image (nginx) Container Container Parent ! Reference
  25. AUFS • Layered FS! • Share commonFS! • RO &

    RW! • Used to share between container
  26. AUFS • Layered FS! • Share commonFS! • RO &

    RW! • Used to share between container Can't understand? Let's see the Picture!
  27. AUFS Add Files A Add Files B Add Files C

    Delete Files B Add Files E Add Files D
  28. AUFS Add Files A Add Files B Add Files C

    Delete Files B Add Files E Add Files D Layered FS
  29. AUFS Add Files A Add Files B Add Files C

    Delete Files B Add Files E Add Files D Share commonFS
  30. Docker Container Kernel LXC AUFS Docker Base image (Ubuntu) Image

    (php) Image (apache) Container Base image (CentOS) Image (nginx) Container Container
  31. Docker Container Kernel LXC AUFS Docker Base image (Ubuntu) Image

    (php) Image (apache) Container Base image (CentOS) Image (nginx) Container Container Read! Only
  32. Docker Container Kernel LXC AUFS Docker Base image (Ubuntu) Image

    (php) Image (apache) Container Base image (CentOS) Image (nginx) Container Container Read! Only Writable
  33. Relationship between image and container Image! (registry) Container RUN Container'

    CHANGE COMMIT Image! (local) PULL PUSH Image! (registry)
  34. Installation • Linux by package manager! • OS X, Windows

    by boot2docker! • Version 1.20 1.3.0 released! 2014/10/16 Same day as my birthday >///<
  35. Version $ docker version Client version: 1.3.0 Client API version:

    1.15 Go version (client): go1.3.3 Git commit (client): c78088f OS/Arch (client): linux/amd64 Server version: 1.3.0 Server API version: 1.15 Go version (server): go1.3.3 Git commit (server): c78088f
  36. Info $ docker info Containers: 4 Images: 71 Storage Driver:

    aufs Root Dir: /var/lib/docker/aufs Dirs: 79 Execution Driver: native-0.2 Kernel Version: 3.13.0-36-generic Operating System: Ubuntu 14.04.1 LTS
  37. Pull base image(s) $ docker pull ubuntu Pulling repository ubuntu

    463ff6be4238: Download complete 9cbaf023786c: Download complete 2185fd50e2ca: Download complete a9561eb1b190: Download complete 3db9c44f4520: Download complete 195eb90b5349: Download complete c5881f11ded9: Download complete 511136ea3c5a: Download complete 500cae81e00f: Download complete 3ed75c4904e0: Download complete b8c495ea8a4e: Download complete 0f154c52e965: Download complete f180ea115597: Download complete bac448df371d: Download complete ……
  38. List images $ docker images REPOSITORY TAG IMAGE ID CREATED

    VIRTUAL SIZE ubuntu 14.10 2185fd50e2ca 28 hours ago 237.2 MB ubuntu utopic 2185fd50e2ca 28 hours ago 237.2 MB ubuntu 14.04.1 9cbaf023786c 28 hours ago 192.8 MB ubuntu 14.04 9cbaf023786c 28 hours ago 192.8 MB ubuntu trusty 9cbaf023786c 28 hours ago 192.8 MB ubuntu latest 9cbaf023786c 28 hours ago 192.8 MB ubuntu precise a9561eb1b190 28 hours ago 120.2 MB ubuntu 12.04.5 a9561eb1b190 28 hours ago 120.2 MB ubuntu 12.04 a9561eb1b190 28 hours ago 120.2 MB ubuntu 12.10 c5881f11ded9 3 months ago 172.2 MB ubuntu quantal c5881f11ded9 3 months ago 172.2 MB ubuntu 13.04 463ff6be4238 3 months ago 169.4 MB ubuntu raring 463ff6be4238 3 months ago 169.4 MB ubuntu 13.10 195eb90b5349 3 months ago 184.7 MB ubuntu saucy 195eb90b5349 3 months ago 184.7 MB ……
  39. Run and List container $ docker run ubuntu:trusty /bin/echo "hello,

    world" hello, world image name execute argument
  40. Run and List container $ docker run ubuntu:trusty /bin/echo "hello,

    world" hello, world $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73ec6de5630b ubuntu:14.04 "/bin/echo 'hello, w 2 hours ago Exited (0) sad_engelbart image name execute argument
  41. Run and List container $ docker run ubuntu:trusty /bin/echo "hello,

    world" hello, world $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73ec6de5630b ubuntu:14.04 "/bin/echo 'hello, w 2 hours ago Exited (0) sad_engelbart image name execute argument The container is finished, so we use -a
  42. Run and list running container docker run ubuntu:latest /bin/sh -c

    \
 "while true; do echo hello, world; sleep 1; done;" hello, world hello, world …… $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e46bc2bd17d ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
  43. Run and list running container docker run ubuntu:latest /bin/sh -c

    \
 "while true; do echo hello, world; sleep 1; done;" hello, world hello, world …… $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e46bc2bd17d ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow The container is running until we stop it
  44. Run running container in deamon docker run -d ubuntu:latest /bin/sh

    -c \
 "while true; do echo hello, world; sleep 1; done;" 90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
  45. Run running container in deamon docker run -d ubuntu:latest /bin/sh

    -c \
 "while true; do echo hello, world; sleep 1; done;" 90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow
  46. Run running container in deamon docker run -d ubuntu:latest /bin/sh

    -c \
 "while true; do echo hello, world; sleep 1; done;" 90f0ca0a13014ef82f33a2fbef646d8c2cd2b1b260b0d6e71fc46e553560babb $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90f0ca0a1301 ubuntu:14.04 "/bin/sh -c 2 hours ago Up 5 seconds lonely_brow We use container id to manage it
  47. Logs and Attach Container $ docker logs 90f0ca hello, world

    hello, world …… $ docker attach 90f0ca hello, world hello, world ……
  48. Logs and Attach Container $ docker logs 90f0ca hello, world

    hello, world …… $ docker attach 90f0ca hello, world hello, world …… Get logs
  49. Logs and Attach Container $ docker logs 90f0ca hello, world

    hello, world …… $ docker attach 90f0ca hello, world hello, world …… Get logs Back to container console
  50. Logs and Attach Container $ docker logs 90f0ca hello, world

    hello, world …… $ docker attach 90f0ca hello, world hello, world …… Get logs Back to container console 「CTRL-p CTRL-q」 to detach.!
  51. Commit $ docker run -i -t php:5.6.1-apache /bin/bash! You do

    some change …… $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse
  52. Commit $ docker run -i -t php:5.6.1-apache /bin/bash! You do

    some change …… $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse $ docker commit 6a495e871389 fntsr/first-image! 2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df
  53. Commit $ docker run -i -t php:5.6.1-apache /bin/bash! You do

    some change …… $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse $ docker commit 6a495e871389 fntsr/first-image! 2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df <username>/<image_name>
  54. Commit $ docker run -i -t php:5.6.1-apache /bin/bash! You do

    some change …… $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a495e871389 php:5-apache "/bin/bash" 24 minutes ago Exited (0) cranky_morse $ docker commit 6a495e871389 fntsr/first-image! 2006ba62eb6508560ae38782ee514a4212d29ea4e4a2e7495b1eedf155fe13df $ docker images! REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE fntsr/first-image latest 2006ba62eb65 22 minutes ago 803.6 MB
  55. Expose Port docker run -d -p 80 nginx c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb $

    docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp
  56. Expose Port docker run -d -p 80 nginx c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb -p

    <guest_port>! $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp
  57. Expose Port docker run -d -p 80 nginx c63fda2248d0dcf90a90cc817d1acb6c9546db841128ca7558ff1f496ad4f8fb -p

    <guest_port>! $ docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS c63fda2248d0 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:49153->80/tcp Expose 80 to 49153 (random) Expose to random port
  58. docker run -d -p 10080:80 nginx afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658 $ docker ps

    -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp Expose Port
  59. docker run -d -p 10080:80 nginx afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658 -p <host_port>:<guest_port> $

    docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp Expose Port
  60. docker run -d -p 10080:80 nginx afd45d0f7d34baf90dbef109ae167c99a49432080adc8621e8cd7e18a4004658 -p <host_port>:<guest_port> $

    docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS afd45d0f7d34 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 0.0.0.0:10080->80/tcp Expose 80 to 10080 (assigned) Expose to assigned port Expose Port
  61. docker run -d -p 127.0.0.1:10080:80 nginx 76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526 $ docker ps

    -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp Expose Port
  62. docker run -d -p 127.0.0.1:10080:80 nginx 76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526 -p <hostname>:<host_port>:<guest_port> $

    docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp Expose Port
  63. docker run -d -p 127.0.0.1:10080:80 nginx 76d00613ccb3576ef604c61f4d086aa6f090ed7268c42638f1931e235c0fe526 -p <hostname>:<host_port>:<guest_port> $

    docker ps -a! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 76d00613ccb3 nginx:1 "nginx… 6 seconds ago Up 5 seconds 443/tcp, 127.0.0.1:10080->80/tcp Expose with host name Expose to host with hostname Expose Port
  64. docker run -d -P nginx 2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048 $ docker ps -a!

    CONTAINER ID IMAGE COMMAND CREATED PORTS afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp Expose Port
  65. docker run -d -P nginx 2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048 -P $ docker ps

    -a! CONTAINER ID IMAGE COMMAND CREATED PORTS afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp Expose Port
  66. docker run -d -P nginx 2123f55d26fc3d5824492ed8ad4d7f9e28ed5d749e7d401e21a1bb0002f65048 -P $ docker ps

    -a! CONTAINER ID IMAGE COMMAND CREATED PORTS afd45d0f7d34 nginx:1 "nginx… Up 5 seconds 0.0.0.0:49154->443/tcp, 0.0.0.0:49155->80/tcp Expose all port of container ! with host random port 443 expose to 49154 (random) Expose Port
  67. How could I back to running container and do another

    thing? • attach! It can only run one process.
  68. How could I back to running container and do another

    thing? • attach! • exec It can only run one process.
  69. How could I back to running container and do another

    thing? • attach! • exec It can only run one process. It's added on docker 1.3 (rock)
  70. $ docker run nginx --name my-nginx -d nginx 4441f36ebab1975004782ea6d03198fd8ced8472cf34721f9906cbebfe61872d" Alias

    $ docker exec -it nginx bin/bash CONTAINER ID IMAGE …… STATUS PORTS NAMES 4441f36ebab1 nginx:latest Up 4 seconds 443/tcp, 0.0.0.0:80->80/tcp my-nginx
  71. $ docker run nginx --name my-nginx -d nginx 4441f36ebab1975004782ea6d03198fd8ced8472cf34721f9906cbebfe61872d" $

    docker exec -it my-nginx bin/bash root@4441f36ebab1:/# Alias $ docker ps CONTAINER ID IMAGE …… STATUS PORTS NAMES 4441f36ebab1 nginx:latest Up 4 seconds 443/tcp, 0.0.0.0:80->80/tcp my-nginx
  72. volume directory Host /var/www/html …… …… Container Volume • Real

    time changes! • Exist until container destroyed!
  73. volume directory Host /var/www/html …… …… Container Volume • Real

    time changes! • Exist until container destroyed! • Can set RO mode.
  74. Dockerfile • Automatically built images and deploy! • Automated Builds

    on Docker Hub! • Easy to share and customize config! !
  75. Single app container PHP App Volume /var/www/html PHP 5.6 ……

    Nginx MySQL …… Port Not recommend use for production
  76. Container Groups PHP App Volume /var/www/html …… Nginx /var/lib/mysql/ ……

    PHP 5.6 MySQL …… App Container DB Container Port Port Easy to manage
  77. Separate containers …… PHP-apache FS php container …… MySQL FS

    MySQL container PHP App Volume …… Link Link Port …… Nginx FS nginx container volume Using in production
  78. Multi PHP Projects Nginx HOST PHP project A /var/www/html ……

    PHP:5.6-apache ! Container PHP project B /var/www/html …… PHP:5.5-apache ! Container …… …… /var/www/html
  79. Multi PHP Projects Nginx HOST PHP project A /var/www/html ……

    PHP:5.6-apache ! Container PHP project B /var/www/html …… PHP:5.5-apache ! Container …… …… /var/www/html volume
  80. Multi PHP Projects Nginx HOST PHP project A /var/www/html ……

    PHP:5.6-apache ! Container /data …… DB Container PHP project B /var/www/html …… PHP:5.5-apache ! Container …… …… /var/www/html DB! Link volume
  81. Multi PHP Projects Nginx HOST PHP project A /var/www/html ……

    PHP:5.6-apache ! Container /data …… DB Container PHP project B /var/www/html …… PHP:5.5-apache ! Container …… …… /var/www/html DB! Link volume Port
  82. Resource & Reference • Docker Documentation! • Digital Ocean! •

    boot2docker! • Ship it with Docker! by Xabier Larrakoetxea! • Docker.Taipei! • Flat UI Colors! • IcoMoon App