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.

From Docker to Production - SunshinePHP 2017

617 views

Published on

Congrats! You and your coworkers love Docker. Docker has become an increasingly helpful tool when it comes to devops. We can now build smaller, more robust local development setups with the promise of mirroring production. One thing that still plagues many situations is how to get those containers into production and update them over time. We will explore different tools for setting up, configuring, and maintaining containers as they go live.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

From Docker to Production - SunshinePHP 2017

  1. 1. From Docker to Production Chris Tankersley @dragonmantank SunshinePHP 2017 SunshinePHP 2017 1
  2. 2. SunshinePHP 2017 2
  3. 3. Subliminal Advertisement SunshinePHP 2017 3
  4. 4. SunshinePHP 2017 4
  5. 5. SunshinePHP 2017 5
  6. 6. SunshinePHP 2017 6
  7. 7. SunshinePHP 2017 7
  8. 8. SunshinePHP 2017 8
  9. 9. SunshinePHP 2017 9
  10. 10. Considerations SunshinePHP 2017 10
  11. 11. How does your app handle storage? SunshinePHP 2017 11
  12. 12. What does your build process look like? SunshinePHP 2017 12
  13. 13. How well does your app handle scaling? SunshinePHP 2017 13
  14. 14. Does it make sense? SunshinePHP 2017 14
  15. 15. The Build Process SunshinePHP 2017 15
  16. 16. 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 SunshinePHP 2017 16
  17. 17. 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 SunshinePHP 2017 17
  18. 18. Sample usage docker build --no-cache –f docker/php/phpserver.dockerfile –t prod_php /opt/builds/20161010 SunshinePHP 2017 18
  19. 19. phpserver.dockerfile FROM php:fpm RUN docker-php-ext-install pdo pdo_mysql COPY ./ /var/www SunshinePHP 2017 19
  20. 20. Docker Compose SunshinePHP 2017 20
  21. 21. Very Good for Small Deployments • Can be used to augment your dev environment • Works well with Docker Machine SunshinePHP 2017 21
  22. 22. Create a machine docker-machine create --driver digitalocean --digital-ocean-access-token [token] sunshinephp2017 SunshinePHP 2017 22
  23. 23. SunshinePHP 2017 23
  24. 24. Switch to the remote node • Run docker-machine env sunshinephp2017 & "C:Program FilesDockerDockerResourcesbindocker- machine.exe" env sunshinephp2017 | Invoke-Expression SunshinePHP 2017 24
  25. 25. Set up docker-compose • Docker Compose allows multiple config files with -f • Have a base docker-compose.yml for Production • Add a secondary one for Development SunshinePHP 2017 25
  26. 26. version: '2' volumes: mysqldata: driver: local services: nginx: build: context: ./ dockerfile: ./nginx.dockerfile ports: - 80:80 - 443:443 phpserver: build: context: ./ dockerfile: ./phpserver.dockerfile working_dir: /var/www/public mysqlserver: image: mysql environment: [redacted] volumes: - mysqldata:/var/lib/mysql SunshinePHP 2017 26 docker-compose.yml
  27. 27. version: '2' volumes: mysqldata: driver: local services: nginx: image: nginx volumes: - ./output_dev:/var/www/public:ro - ./app/nginx/default.conf:/etc/nginx/conf.d/default.conf - ./ssl:/etc/nginx/ssl/ phpserver: build: context: ./ dockerfile: ./phpserver.dockerfile working_dir: /var/www/public volumes: - ./app:/var/www/ - ./vendor:/var/www/vendor mysqlserver: image: mysql environment: [redacted] volumes: - mysqldata:/var/lib/mysql SunshinePHP 2017 27 docker-compose.dev.yml
  28. 28. When doing development docker-compose –f docker-compose.yml –f docker-compose.dev.yml up -d SunshinePHP 2017 28
  29. 29. When doing a deployment docker-compose up -d SunshinePHP 2017 29
  30. 30. Other Alternative – Variable Substitution • Docker Compose allows variable substitution inside the file • Wrap variables in ${} • image: ${DEPLOY_VERSION}_php SunshinePHP 2017 30
  31. 31. When doing a deployment docker build –f […] –t 20161010_php /opt/builds/20161010 DEPLOY_VERSION=20161010 docker-compose up -d SunshinePHP 2017 31
  32. 32. Docker Swarm SunshinePHP 2017 32
  33. 33. What is Swarm? • Docker-supplied clustering • Define a series of services that can be deployed SunshinePHP 2017 33
  34. 34. Setup SunshinePHP 2017 34 Manager Node 2Node 1
  35. 35. Register a node as a Manager docker swarm init --advertise-addr 172.16.0.245 SunshinePHP 2017 35
  36. 36. Add nodes docker swarm join --token [token] 172.16.0.245:2377 SunshinePHP 2017 36
  37. 37. docker-compose.yml version: '3' services: phpserver: image: php:apache ports: - 80:80 SunshinePHP 2017 37
  38. 38. Deploy the stack $ docker stack deploy -c docker-compose.yml myapp Creating network myapp_default Creating service myapp_phpserver SunshinePHP 2017 38
  39. 39. docker-compose.yml version: '3' services: phpserver: image: nginx ports: - 80:80 SunshinePHP 2017 39
  40. 40. Deploy the stack $ docker stack deploy -c docker-compose.yml myapp Updating service myapp_phpserver (id: sy8qtegme06g8wqgl0w9707fr) SunshinePHP 2017 40
  41. 41. docker-compose.yml version: '3' services: phpserver: image: nginx ports: - 80:80 deploy: replicas: 5 SunshinePHP 2017 41
  42. 42. docker-compose.yml version: '3' services: phpserver: image: nginx ports: - 80:80 deploy: replicas: 5 SunshinePHP 2017 42
  43. 43. docker stack ps myapp ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 72mud5othsjf myapp_phpserver.1 nginx:latest node2 Running Running 8 minutes ago wsf3m32u9vcr _ myapp_phpserver.1 php:apache manager1 Shutdown Shutdown 11 minutes ago xf3wyh289bec myapp_phpserver.2 nginx:latest node1 Running Preparing 20 seconds ago fehf1vdx4m0r myapp_phpserver.3 nginx:latest manager1 Running Preparing 20 seconds ago pwnq65e6w7ew myapp_phpserver.4 nginx:latest manager1 Running Preparing 20 seconds ago roxtanjughq8 myapp_phpserver.5 nginx:latest node2 Running Running 20 seconds ago SunshinePHP 2017 43
  44. 44. Rancher SunshinePHP 2017 44
  45. 45. Rancher and RancherOS SunshinePHP 2017 45
  46. 46. Manages your Containers SunshinePHP 2017 46
  47. 47. Manages your Hosts SunshinePHP 2017 47
  48. 48. Allows you to monitor containers SunshinePHP 2017 48
  49. 49. Allows you to manage your applications SunshinePHP 2017 49
  50. 50. Allows you to deploy your applications SunshinePHP 2017 50
  51. 51. Allows you to deploy your applications SunshinePHP 2017 51
  52. 52. Supports Docker Compose SunshinePHP 2017 52
  53. 53. Blue-Green Deployments SunshinePHP 2017 53 Router App v1 App v2
  54. 54. Blue-Green Deployments SunshinePHP 2017 54 Router App v3 App v2
  55. 55. Blue-Green Deployments SunshinePHP 2017 55 Router App v3 App v2
  56. 56. Add rancher/server to the master SunshinePHP 2017 56 docker run -d --restart=always -p 8080:8080 –name=rancher rancher/server
  57. 57. Add rancher/agent to nodes SunshinePHP 2017 57 docker run -d --privileged -v /var/run/docker.sock:/var/run/docker.sock rancher/agent:v0.7.9 http://192.168.99.100:8080/v1/scripts/[hash]
  58. 58. All Done! SunshinePHP 2017 58
  59. 59. Deploying with Rancher CLI SunshinePHP 2017 59
  60. 60. What is it? • Rancher has an API! • Small executable that interacts with Rancher API • Kind of like a custom docker-compose SunshinePHP 2017 60
  61. 61. Get an API Key SunshinePHP 2017 61
  62. 62. Get an API Key SunshinePHP 2017 62
  63. 63. Download Rancher CLI SunshinePHP 2017 63
  64. 64. Export Stack Config SunshinePHP 2017 64
  65. 65. Two Config Files SunshinePHP 2017 65
  66. 66. docker-compose.yml SunshinePHP 2017 66
  67. 67. Deploy Script SunshinePHP 2017 67 ACCESS_KEY="C4F407CE1D8C59EB53BE" SECRET="daNENHR241Jzm5Z9iw6VsujD9hWfjHWrDzkKmKiA" RANCHER_URL="http://192.168.99.100:8080" ./rancher-compose --secret-key=${SECRET} --access-key=${ACCESS_KEY} --url=${RANCHER_URL} -- file=docker-compose.yml --rancher-file=rancher-compose.yml -p phptest up --upgrade -d
  68. 68. Edit and Deploy SunshinePHP 2017 68
  69. 69. Edit and Deploy SunshinePHP 2017 69
  70. 70. Finish Upgrade SunshinePHP 2017 70
  71. 71. 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 SunshinePHP 2017 71

×