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 PHP Developers - Madison PHP 2017

1,372 views

Published on

Docker is quickly becoming an invaluable development and deployment tool for many organizations. Come and spend the day learning about what Docker is, how to use it, how to integrate it into your workflow, and build an environment that works for you and the rest of your team. This hands-on tutorial will give you the kick-start needed to start using Docker effectively.

Published in: Technology
  • Be the first to comment

Docker for PHP Developers - Madison PHP 2017

  1. 1. Docker for PHP Developers Chris Tankersley @dragonmantank Madison PHP 2017 1Madison PHP 2017
  2. 2. Madison PHP 2017 Wifi Settings 2 ● SSID: Radisson ● Password: laborday
  3. 3. Madison PHP 2017 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.” 3 https://www.docker.com/whatisdocker/
  4. 4. Madison PHP 2017 What is a Container? 4
  5. 5. Madison PHP 2017 Normal Bare-Metal Server 5 CPU RAM HD Network Operating System nginx PHP DB
  6. 6. Madison PHP 2017 Normal Bare-Metal Server 6 CPU RAM HD Network Operating System nginx PHP DB
  7. 7. Madison PHP 2017 Virtual Machines 7 CPU RAM HD Network Operating System nginx PHP DB Operating System nginx PHP DB Operating System Hypervisor
  8. 8. Madison PHP 2017 Containers 8 CPU RAM HD Network Operating System nginxnginx PHP DB PHP DB
  9. 9. Madison PHP 2017 Containers vs VMs
  10. 10. Madison PHP 2017 Containers Are Not New • LXC (Linux Containers) • OpenVZ • Systemd-nspawn • BSD Jails • Solaris Zones • chroot 10
  11. 11. Madison PHP 2017 Containers are just walled processes 11 Ubuntu Kernel / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … nginx bash / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … php
  12. 12. Madison PHP 2017 What is Docker? 12
  13. 13. Madison PHP 2017 Docker is an Ecosystem 13 Docker Engine
  14. 14. Madison PHP 2017 Docker is an Ecosystem 14 Docker ComposeDocker Machine Docker Swarm
  15. 15. Madison PHP 2017 How does it work? 15 Uses a variety of existing Container technologies Server Containers Hyper-V Containers xhyve Virtualization
  16. 16. Madison PHP 2017 Sorry OSX < 10.10 and Windows < 10 Users Docker Toolbox 16
  17. 17. Madison PHP 2017 Let’s use Docker 17
  18. 18. Madison PHP 2017 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 18
  19. 19. Madison PHP 2017 Running a simple shell 19
  20. 20. Madison PHP 2017 Running a simple shell 20
  21. 21. Madison PHP 2017 Running a simple shell 21
  22. 22. Madison PHP 2017 What’s Going On? 22 Ubuntu Kernel / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … nginx bash / + bin/ + etc/ + dev/ + home/ + usr/ + var/ + lib/ + … php
  23. 23. Madison PHP 2017 Running Two Webservers 23
  24. 24. Madison PHP 2017 Running Two Webservers 24
  25. 25. Madison PHP 2017 Running Two Webservers 25
  26. 26. Madison PHP 2017 Running Two Webservers 26
  27. 27. Madison PHP 2017 Running Two Webservers 27
  28. 28. Madison PHP 2017 Running Two Webservers 28
  29. 29. Madison PHP 2017 Running Two Webservers 29
  30. 30. Madison PHP 2017 Running Two Webservers 30
  31. 31. Madison PHP 2017 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 31
  32. 32. Madison PHP 2017 Volumes 32
  33. 33. Madison PHP 2017 Modifying a running container • `docker exec` can run a command inside of an existing container • Use Volumes to share data 33
  34. 34. Madison PHP 2017 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 34
  35. 35. Madison PHP 2017 Mounting from the host machine 35
  36. 36. Madison PHP 2017 Mounting from the host machine 36
  37. 37. Madison PHP 2017 Mounting from the host machine 37
  38. 38. Madison PHP 2017 Mounting from the host machine 38
  39. 39. Madison PHP 2017 Mounting from the host machine 39
  40. 40. Madison PHP 2017 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 40
  41. 41. Madison PHP 2017 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 41
  42. 42. Madison PHP 2017 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 42
  43. 43. Madison PHP 2017 Mounting Data Volumes 43
  44. 44. Madison PHP 2017 Mounting Data Volumes 44
  45. 45. Madison PHP 2017 Mounting Data Volumes 45
  46. 46. Madison PHP 2017 Mounting Data Volumes 46
  47. 47. Madison PHP 2017 Mounting Data Volumes 47
  48. 48. Madison PHP 2017 Mounting Data Volumes 48
  49. 49. Madison PHP 2017 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 49
  50. 50. Madison PHP 2017 Networking 50
  51. 51. Madison PHP 2017 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 51
  52. 52. Madison PHP 2017 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 52
  53. 53. Madison PHP 2017 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 53
  54. 54. Madison PHP 2017 Create a network 54
  55. 55. Madison PHP 2017 Attach to a network 55
  56. 56. Madison PHP 2017 Ping the web container 56
  57. 57. Madison PHP 2017 Add another web and kill web1 57
  58. 58. Madison PHP 2017 Other Helpful Commands 58
  59. 59. Madison PHP 2017 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 59
  60. 60. Madison PHP 2017 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 60
  61. 61. Madison PHP 2017 Containerizing An Application 61
  62. 62. Madison PHP 2017 Our Goals • Not change our workflow (much) • Run PHP 7, Unit Tests, and webserver • Deploy “easily” 62
  63. 63. Madison PHP 2017 Just try and run it docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 php:apache 63
  64. 64. Madison PHP 2017 64
  65. 65. Madison PHP 2017 Checking Logs • Containers log to stdout/stderr • Docker aggregates the logs • Can be viewed with docker logs 65
  66. 66. Madison PHP 2017 Oops 66
  67. 67. Madison PHP 2017 Custom Images • PHP images are pretty bare • Lots of times need to install extensions 67
  68. 68. Madison PHP 2017 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 68
  69. 69. Madison PHP 2017 docker/Dockerfile FROM php:apache RUN a2enmod rewrite 69
  70. 70. Madison PHP 2017 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 70
  71. 71. Madison PHP 2017 Build it docker build -t d4dapp docker/ 71
  72. 72. Madison PHP 2017 72
  73. 73. Madison PHP 2017 Use the new image docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 d4dapp 73
  74. 74. Madison PHP 2017 Use the new image 74
  75. 75. Madison PHP 2017 Slightly better 75
  76. 76. Madison PHP 2017 Install Dependencies 76
  77. 77. Madison PHP 2017 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 77
  78. 78. Madison PHP 2017 Better! 78
  79. 79. Madison PHP 2017 Look at queues! 79
  80. 80. Madison PHP 2017 docker/Dockerfile FROM php:apache RUN a2enmod rewrite && docker-php-ext-install pdo_mysql 80
  81. 81. Madison PHP 2017 Rebuild the image docker build -t d4dapp docker/ 81
  82. 82. Madison PHP 2017 Rebuild the container $ docker rm -f d4dapp $ docker run -d --name d4dapp -v C:dragoProjectsdockerfordevs-app:/var/www/ -p 8080:80 d4dapp 82
  83. 83. Madison PHP 2017 Progress! 83
  84. 84. Madison PHP 2017 Docker Compose 84
  85. 85. Madison PHP 2017 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 85
  86. 86. Madison PHP 2017 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 86
  87. 87. Madison PHP 2017 No longer use docker run $ docker rm –f d4dapp $ docker-compose up -d 87
  88. 88. Madison PHP 2017 Now we have 2 containers 88
  89. 89. Madison PHP 2017 Config for DB now points to the service name 89 <?php return [ 'debug' => true, 'config_cache_enabled' => false, 'db' => [ 'driver' => 'Pdo_Mysql', 'hostname' => 'mysqlserver', 'port' => '3306', 'database' => 'dockerfordevs', 'user' => 'root', 'password' => 's3curep@assword', ], ];
  90. 90. Madison PHP 2017 Yay! 90
  91. 91. Madison PHP 2017 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 91
  92. 92. Madison PHP 2017 Set up phinx docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app php:cli php vendor/bin/phinx init 92
  93. 93. Madison PHP 2017 Run the migration docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app --network dockerfordevsapp_default php:cli php vendor/bin/phinx migrate 93
  94. 94. Madison PHP 2017 Oops 94
  95. 95. Madison PHP 2017 Let’s use the existing container docker-compose run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app d4dapp php vendor/bin/phinx migrate 95
  96. 96. Madison PHP 2017 Good… 96
  97. 97. Madison PHP 2017 It Lives! 97
  98. 98. Madison PHP 2017 Unit Testing docker run --rm -v C:UsersdragoProjectsdockerfordevs-app:/app -w /app d4dapp php vendor/bin/phpunit -c . 98
  99. 99. Madison PHP 2017 Running the tests
  100. 100. Madison PHP 2017 Build a service service: testrunner: build: ./docker/ volumes: - ./:/app working_dir: /app command: vendor/bin/phpunit -c . 100
  101. 101. Madison PHP 2017 Run the tests with the service docker-compose run --rm testrunner 101
  102. 102. Madison PHP 2017 Running the tests
  103. 103. Production Considerations Madison PHP 2017 103
  104. 104. 12 Factor Applications Madison PHP 2017 104
  105. 105. 1. Codebase One codebase tracked in revision control, many deploys Madison PHP 2017 105
  106. 106. Repo Tips • Keep everything in your repository • Tag releases • Never move tags Madison PHP 2017 106
  107. 107. 2. Dependencies Explicitly declare and isolate dependencies Madison PHP 2017 107
  108. 108. Dependencies • Commit both composer.json and composer.lock files • Commit Dockerfiles to the same repo as the codebase Madison PHP 2017 108
  109. 109. 3. Config Store config in the environment 109Madison PHP 2017
  110. 110. Configuration • Anything that is environment specific should move to environment vars • Makes it much easier to build and deploy code • Code cares less what external services it is talking to 110Madison PHP 2017
  111. 111. Use Environment Vars • Can specify them one-by-one – docker run ­e VAR_NAME=value • Can specify a file – docker run ­­env­file=filename • Can specify in docker-compose.yml 111Madison PHP 2017
  112. 112. 4. Backing Services Treat backing services as attached resources 112Madison PHP 2017
  113. 113. Everything is “external” • Never talk to local sockets • Don’t make a determination between “locally” hosted and third party • Easier to switch environments • Easier to scale up 113Madison PHP 2017
  114. 114. 5. Build, release, run Strictly separate build and run stages 114Madison PHP 2017
  115. 115. The Workflow • Build step installs dependencies, compiles files, and generates a Build Artifact that can be deployed – Does not contain any deployment configuration • Release step pushes a Build Artifact into an environment – Runs DB migrations, anything needed to happen before running • Run step runs the app fully in the environment 115Madison PHP 2017
  116. 116. Tips • Build Artifact can be an image • Builds should be completely reproducible • Release always take a build artifact, never directly from the repo • Tag all your builds • Track all your releases 116Madison PHP 2017
  117. 117. Build Step - Start Small • Build your application • Run composer • Run npm/bower • Build JS/CSS • Use the compiled output to build an image with docker build • Push full image to private registry 117Madison PHP 2017
  118. 118. docker build • Additional options to look at • -f, --file – Specify a different filename for the Dockerfile • --no-cache – Don’t use a cached layer • --pull – Always pull a new version of the image 118Madison PHP 2017
  119. 119. Sample usage docker build --no-cache –f docker/php/phpserver.dockerfile –t prod_php /opt/builds/20161010 119Madison PHP 2017
  120. 120. phpserver.dockerfile FROM php:fpm RUN docker-php-ext-install pdo pdo_mysql COPY ./ /var/www 120Madison PHP 2017
  121. 121. 6. Processes Execute the app as one or more stateless processes 121Madison PHP 2017
  122. 122. Built Into Docker • One Process per container • Allows tools to scale just what needs to be scaled • Allows images to be swapped out as needed 122Madison PHP 2017
  123. 123. 7. Port Binding Export services via port binding 123Madison PHP 2017
  124. 124. Built Into Docker (Again) • Each container gets its own IP and exposes its own ports • Processes should already be talking over a network • Can work with service locators that are port-based 124Madison PHP 2017
  125. 125. 8. Concurrency Scale out via the process model 125Madison PHP 2017
  126. 126. How well does your app handle scaling? 126Madison PHP 2017
  127. 127. Built Into Docker (Again) (Again) • One Process per container • Scale up just the container that is needed • App should not care how many instances of each service are running 127Madison PHP 2017
  128. 128. 9. Disposability Maximize robustness with fast startup and graceful shutdown 128Madison PHP 2017
  129. 129. Signals • Docker starts containers fairly quickly • Applications should gracefully shut down, not just die • Docker sends a SIGTERM when shutting down a container • Your CLI apps may need to handle SIGTERM properly – Cal Evans, “Signalling PHP” 129Madison PHP 2017
  130. 130. 10. Dev/prod Parity Keep development, staging, and production as similar as possible 130Madison PHP 2017
  131. 131. 11. Logs Treat logs as event streams 131Madison PHP 2017
  132. 132. Logging in Docker • Various logging options built in – JSON file (default) – Fluentd – Syslog – Journald – Gelf – Splunk – Aws – Etwlogs – Gcplogs 132Madison PHP 2017
  133. 133. Push logs remotely • When possible, push Docker logs to a remote service – Container logs only exist while the container exists • Allows logs to be viewed in a single place • No need to get into actual servers • Can host yourself, or pay for a SaaS • ELK stack is very popular – Docker uses fluentd instead 133Madison PHP 2017
  134. 134. 12. Admin Processes Run admin/management tasks as one-off processes 134Madison PHP 2017Madison PHP 2017
  135. 135. https://leanpub.com/dockerfordevs/c/madison2017
  136. 136. Madison PHP 2017 Thank You! • Software Engineer for InQuest • Author of “Docker for Developers” • https://leanpub.com/dockerfordevs • Co-Host of “Jerks Talk Games” • http://jerkstalkgames.com • http://ctankersley.com • chris@ctankersley.com • @dragonmantank 136

×