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.

Docker for Developers - Sunshine PHP

440 views

Published on

Thanks to tools like vagrant, puppet/chef, and Platform as a Service services like Heroku, developers are extremely used to being able to spin up a development environment that is the same every time. What if we could go a step further and make sure our development environment is not only using the same software, but 100% configured and set up like production. Docker will let us do that, and so much more. We'll look at what Docker is, why you should look into using it, and all of the features that developers can take advantage of.

Published in: Technology
  • Be the first to comment

Docker for Developers - Sunshine PHP

  1. 1. Docker for PHP Developers Chris Tankersley @dragonmantank Sunshine PHP 2017 Sunshine PHP 2017 1
  2. 2. What Is Docker? “Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments.” Sunshine PHP 2017 2 https://www.docker.com/whatisdocker/
  3. 3. Containers Sunshine PHP 2017 3
  4. 4. Normal Bare-Metal Server Sunshine PHP 2017 4 CPU RAM HD Network Operating System nginx PHP DB
  5. 5. Virtual Machines Sunshine PHP 2017 5 CPU RAM HD Network Operating System nginx PHP DB Operating System nginx PHP DB Operating System Hypervisor
  6. 6. Containers Sunshine PHP 2017 6 CPU RAM HD Network Operating System nginxnginx PHP DB PHP DB
  7. 7. Containers Are Not New • LXC (Linux Containers) • OpenVZ • Systemd-nspawn • Qemu/kvm • BSD Jails • Solaris Zones • chroot Sunshine PHP 2017 7
  8. 8. Docker is an Ecosystem Sunshine PHP 2017 8 Docker Engine
  9. 9. Docker is an Ecosystem Sunshine PHP 2017 9 Docker ComposeDocker Machine Docker Swarm
  10. 10. How does it work? Sunshine PHP 2017 10 Uses a variety of existing Container technologies Server Containers Hyper-V Containers xhyve Virtualization
  11. 11. Sorry OSX < 10.10 and Windows < 10 Users Docker Toolbox Sunshine PHP 2017 11
  12. 12. Let’s use Docker Sunshine PHP 2017 12
  13. 13. Running a container • `docker run` will run a container • This will not restart an existing container, just create a new one • docker run [options] IMAGE [command] [arguments] • [options ]modify the docker process for this container • IMAGE is the image to use • [command] is the command to run inside the container • [arguments] are arguments for the command Sunshine PHP 2017 13
  14. 14. Running a simple shell Sunshine PHP 2017 14
  15. 15. Running a simple shell Sunshine PHP 2017 15
  16. 16. Running a simple shell Sunshine PHP 2017 16
  17. 17. What’s Going On? Sunshine PHP 2017 17 Ubuntu Kernel / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … nginx bash / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … php
  18. 18. Running Two Webservers Sunshine PHP 2017 18
  19. 19. Running Two Webservers Sunshine PHP 2017 19
  20. 20. Running Two Webservers Sunshine PHP 2017 20
  21. 21. Running Two Webservers Sunshine PHP 2017 21
  22. 22. Running Two Webservers Sunshine PHP 2017 22
  23. 23. Running Two Webservers Sunshine PHP 2017 23
  24. 24. Running Two Webservers Sunshine PHP 2017 24
  25. 25. Running Two Webservers Sunshine PHP 2017 25
  26. 26. Some Notes • All three containers are 100% self contained • Docker containers share common ancestors, but keep their own files • `docker run` parameters: • --rm – Destroy a container once it exits • -d – Run in the background (daemon mode) • -i – Run in interactive mode • --name – Give the container a name • -p [local port]:[container port] – Forward the local port to the container port Sunshine PHP 2017 26
  27. 27. Volumes Sunshine PHP 2017 27
  28. 28. Modifying a running container • `docker exec` can run a command inside of an existing container • Use Volumes to share data Sunshine PHP 2017 28
  29. 29. Persistent Data with Volumes • You can designate a volume with –v • Create a named volume with `volume create` • Volumes can be shared amongst containers • Volumes can mount data from the host system Sunshine PHP 2017 29
  30. 30. Mounting from the host machine Sunshine PHP 2017 30
  31. 31. Mounting from the host machine Sunshine PHP 2017 31
  32. 32. Mounting from the host machine Sunshine PHP 2017 32
  33. 33. Mounting from the host machine Sunshine PHP 2017 33
  34. 34. Mounting from the host machine Sunshine PHP 2017 34
  35. 35. Mounting from the host isn’t perfect • The container now has a window into your host machine • Permissions can get screwy if you are modifying in the container • Most things it creates will be root by default, and you probably aren’t root on the host machine • Host-mounted volumes are not portable at all • OSX and Hyper-V VMs have limited pathings to mount • OSX has poor I/O performance Sunshine PHP 2017 35
  36. 36. Named Data Volumes • Creates a space that becomes persistent • Can be mounted anywhere inside your images • Have our app containers use the data volume to store data • Use ‘editor containers’ to go in and modify data when needed Sunshine PHP 2017 36
  37. 37. vim Tutorial • vim is a Modal text editor • ESC will drop you back to default mode • :new /opt/webconfig/default to create a new file • In default mode, i will get us into interactive (edit) mode • :w to save a file • :q will quit Sunshine PHP 2017 37
  38. 38. Mounting Data Volumes Sunshine PHP 2017 38
  39. 39. Mounting Data Volumes Sunshine PHP 2017 39
  40. 40. Mounting Data Volumes Sunshine PHP 2017 40
  41. 41. Mounting Data Volumes Sunshine PHP 2017 41
  42. 42. Mounting Data Volumes Sunshine PHP 2017 42
  43. 43. Mounting Data Volumes Sunshine PHP 2017 43
  44. 44. Why go through the hassle? • Data volumes are portable, depending on the driver • Data volumes are safer • Separates the app containers from data • Production can use a data volume, dev can use a host volume • Our app containers stay small • Works directly with other tools Sunshine PHP 2017 44
  45. 45. Networking Sunshine PHP 2017 45
  46. 46. Networking • Docker can create multiple network “pools” • Each container gets an IP address • Containers can be attached to multiple networks • Docker network allow service discovery inside networks Sunshine PHP 2017 46
  47. 47. Legacy - Docker Links • Legacy Links work with `--link` • Only works on the legacy “bridge” network • Doesn’t support service discovery • Not worth it to use anymore Sunshine PHP 2017 47
  48. 48. Docker Networks • Discreet IP pool for containers • Containers can be added and removed to the network at whim • Service discovery though ‘--network-alias’ • Can be set up to work across hosts Sunshine PHP 2017 48
  49. 49. Create a network Sunshine PHP 2017 49
  50. 50. Attach to a network Sunshine PHP 2017 50
  51. 51. Ping the web container Sunshine PHP 2017 51
  52. 52. Add another web and kill web1 Sunshine PHP 2017 52
  53. 53. BREAK TIME! WOO! Sunshine PHP 2017 53
  54. 54. Other Helpful Commands Sunshine PHP 2017 54
  55. 55. Inspect a container docker inspect [options] CONTAINER_NAME • Returns a JSON string with data about the container • Can also query • docker inspect -f “{{ .NetworkSettings.IPAddress }}” web_server • Really handy for scripting out things like reverse proxies Sunshine PHP 2017 55
  56. 56. Work with images • docker pull IMAGE – Pulls down an image before using • docker images – Lists all the images that are downloaded • docker rmi IMAGE – Deletes an image if it’s not being used Sunshine PHP 2017 56
  57. 57. Containerizing An Application Sunshine PHP 2017 57
  58. 58. Our Goals • Not change our workflow (much) • Run PHP 7, Unit Tests, and webserver • Deploy “easily” Sunshine PHP 2017 58
  59. 59. Just try and run it docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 php:apache Sunshine PHP 2017 59
  60. 60. Sunshine PHP 2017 60
  61. 61. Checking Logs • Containers log to stdout/stderr • Docker aggregates the logs • Can be viewed with docker logs Sunshine PHP 2017 61
  62. 62. Oops Sunshine PHP 2017 62
  63. 63. Custom Images • PHP images are pretty bare • Lots of times need to install extensions Sunshine PHP 2017 63
  64. 64. Dockerfile • Dockerfile is the configuration steps for an image • Can be created from scratch, or based on another image • Allows you to add files, create default volumes, ports, etc • Can be used privately or pushed to Docker Hub Sunshine PHP 2017 64
  65. 65. docker/Dockerfile FROM php:apache RUN a2enmod rewrite Sunshine PHP 2017 65
  66. 66. Build it docker build -t tag_name ./ • This runs through the Dockerfile and generates the image • We can now use the tag name to run the image Sunshine PHP 2017 66
  67. 67. Build it docker build -t d4dapp docker/ Sunshine PHP 2017 67
  68. 68. Sunshine PHP 2017 68
  69. 69. Use the new image docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 d4dapp Sunshine PHP 2017 69
  70. 70. Use the new image Sunshine PHP 2017 70
  71. 71. Slightly better Sunshine PHP 2017 71
  72. 72. Install Dependencies Sunshine PHP 2017 72
  73. 73. Running Composer docker run --rm -v c:/Users/drago/.composer:/root/.composer -v c:/Users/drago/Projects/workshop:/app -v c:/Users/drago/.ssh:/root/.ssh composer/composer install Sunshine PHP 2017 73
  74. 74. Better! Sunshine PHP 2017 74
  75. 75. Look at queues! Sunshine PHP 2017 75
  76. 76. docker/Dockerfile FROM php:apache RUN a2enmod rewrite && docker-php-ext-install pdo_mysql Sunshine PHP 2017 76
  77. 77. Rebuild it docker build -t d4dapp docker/ Sunshine PHP 2017 77
  78. 78. Rebuild the image docker build -t d4dapp docker/ Sunshine PHP 2017 78
  79. 79. Rebuild the container $ docker rm -f d4dapp $ docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 d4dapp Sunshine PHP 2017 79
  80. 80. Progress! Sunshine PHP 2017 80
  81. 81. Docker Compose Sunshine PHP 2017 81
  82. 82. What is Docker Compose? • Multi-container orchestration • A single config file holds all of your container info • Works with Docker Swarm and a few other tools, like Rancher Sunshine PHP 2017 82
  83. 83. Sample docker-compose.yml version: '2' volumes: mysqldata: driver: local services: d4dapp: build: ./docker/ volumes: - ./:/var/www/ ports: - 8080:80 mysqlserver: image: mysql environment: MYSQL_DATABASE: dockerfordevs MYSQL_ROOT_PASSWORD: 's3curep@assword' volumes: - mysqldata:/var/lib/mysql Sunshine PHP 2017 83
  84. 84. No longer use docker run $ docker rm –f d4dapp $ docker-compose up -d Sunshine PHP 2017 84
  85. 85. Now we have 2 containers Sunshine PHP 2017 85
  86. 86. Config for DB now points to the service name Sunshine PHP 2017 86 <?php return [ 'debug' => true, 'config_cache_enabled' => false, 'db' => [ 'driver' => 'Pdo_Mysql', 'hostname' => 'mysqlserver', 'port' => '3306', 'database' => 'dockerfordevs', 'user' => 'root', 'password' => 's3curep@assword', ], ];
  87. 87. Yay! Sunshine PHP 2017 87
  88. 88. Install our DB Migration Software docker run --rm -v c:/Users/drago/.composer:/root/.composer -v c:/Users/drago/Projects/workshop:/app -v c:/Users/drago/.ssh:/root/.ssh composer/composer require robmorgan/phinx Sunshine PHP 2017 88
  89. 89. Set up phinx docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app php:cli php vendor/bin/phinx init Sunshine PHP 2017 89
  90. 90. Run the migration docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app --network dockerfordevsapp_default php:cli php vendor/bin/phinx migrate Sunshine PHP 2017 90
  91. 91. Oops Sunshine PHP 2017 91
  92. 92. Let’s use the existing container docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app --network dockerfordevsapp_default dockerfordevsapp_d4dapp php vendor/bin/phinx migrate Sunshine PHP 2017 92
  93. 93. Good… Sunshine PHP 2017 93
  94. 94. It Lives! Sunshine PHP 2017 94
  95. 95. Other Tools Sunshine PHP 2017 95
  96. 96. Docker Ecosystem • Docker Machine • Docker Swarm Sunshine PHP 2017 96
  97. 97. Thank You! • Software Engineer for InQuest • Author of “Docker for Developers” • https://leanpub.com/dockerfordevs • Co-Host of “Jerks Talk Games” • http://jerkstalkgames • http://ctankersley.com • chris@ctankersley.com • @dragonmantank Sunshine PHP 2017 97

×