Kickass Development Environments with Docker (BrightonPHP, May, 2017)

Kickass Development Environments with Docker (BrightonPHP, May, 2017)

Docker, the hottest technology around at the moment. It swept the Ops world by storm in 2014, became mainstream in 2015, and now it’s set to dominate the developer world, in 2016.

Docker is a tool that allows you to package your application up into a single-runnable distributable binary - akin to the phar, but in Hulk mode. Docker allows you, a developer, to specify the exact environment your application needs to run, across development; test; staging; and production.

In this talk I will cover the creation of this utopian distributable and show you how to can compose your entire production infrastructure locally with only a small YAML file and without installing a single thing.

Lets say hello, to Docker.

69172dc4e4cc3e4cdd234c40adf395fa?s=128

David McKay

May 15, 2017
Tweet

Transcript

  1. Hi Brighton PHP

  2. Kickass Development Environments with Docker

  3. @rawkode Organiser of Things: ◍ ScotlandPHP ◍ Docker Glasgow ◍

    DevOps Glasgow ◍ MongoDB Glasgow ◍ PairProg Glasgow Consultant: ◍ PHP / Go / Elixir ◍ Docker / CI / CD ◍ DevOps / SaltStack ◍ Event-Driven Systems ◍ CQRS / ES
  4. Let’s address the whale in the room ...

  5. Docker & Moby Moby ◍ containerd ◍ RunC ◍ Libnetwork

    ◍ Compose ◍ Notary ◍ All the Kits Docker, Inc. ◍ Docker CE ◍ Docker EE
  6. Kickass Development Environments with DockerCE™©®

  7. Let us travel through time ...

  8. My Development Environment Circa 2000 $ tree awesome-million-pound-project └── src

    ├── game.php ├── game.php.bk-david ├── game.php.bk-deano ├── main.php ├── main.php.maybe-fixed ├── main.php.bk-1999-12-02 ├── main.php.bk-1999-12-02.2 ├── player.php ├── player.php.orig └── .swp.player.php
  9. My Development Production Environment Circa 2000 $ tree awesome-million-pound-project └──

    src ├── game.php ├── game.php.bk-david ├── game.php.bk-deano ├── main.php ├── main.php.maybe-fixed ├── main.php.bk-1999-12-02 ├── main.php.bk-1999-12-02.2 ├── player.php ├── player.php.orig └── .swp.player.php
  10. Things eventually got better ...

  11. None
  12. None
  13. Dev / Prod Parity

  14. Eurgh, DSL Hell

  15. None
  16. Problems with Vagrant Slow to provision RAM intensive Really, development

    only Requires a CM tool
  17. How long does your vagrant up take?

  18. None
  19. “ Docker allows you to package an application with all

    of its dependencies into a standardized unit for software development.
  20. Docker ◍ Image Builder ◍ Virtualisation ◍ Image Delivery ◍

    Orchestration (Dev) ➔ docker build ➔ docker run ➔ docker push / pull ➔ docker-compose
  21. Image Building Introducing the Dockerfile

  22. (Simplified) Dockerfile for PHP FROM ubuntu:17.04 RUN apt install -y

    php-cli COPY hello.php /code WORKDIR /code ENTRYPOINT [“php”] CMD [“-v”]
  23. (Simplified) Dockerfile for PHP FROM ubuntu:17.04 RUN apt install -y

    php-cli COPY hello.php /code WORKDIR /code ENTRYPOINT [“php”] CMD [“-v”]
  24. (Simplified) Dockerfile for PHP FROM ubuntu:17.04 RUN apt install -y

    php-cli COPY hello.php /code WORKDIR /code ENTRYPOINT [“php”] CMD [“-v”]
  25. (Simplified) Dockerfile for PHP FROM ubuntu:17.04 RUN apt install -y

    php-cli COPY hello.php /code WORKDIR /code ENTRYPOINT [“php”] CMD [“-v”]
  26. (Simplified) Dockerfile for PHP FROM ubuntu:17.04 RUN apt install -y

    php-cli COPY hello.php /code WORKDIR /code ENTRYPOINT [“php”] CMD [“-v”]
  27. None
  28. ENTRYPOINT & CMD Explained in 20 seconds

  29. ENTRYPOINT and CMD Explained in 17 seconds ... CMD [“echo”,

    “Hello”] in a Dockerfile docker run my-image == $ Hello
  30. ENTRYPOINT and CMD Explained in 13 seconds ... CMD [“echo”,

    “Hello”] in a Dockerfile docker run my-image echo Goodbye == $ Goodbye
  31. ENTRYPOINT and CMD Explained in 10 seconds ... ENTRYPOINT [“echo”]

    CMD [“Hello”] in a Dockerfile docker run my-image == $ Hello
  32. ENTRYPOINT and CMD Explained in 7 seconds ... ENTRYPOINT [“echo”]

    CMD [“Hello”] in a Dockerfile docker run --entrypoint=”echo” my-image Woop! == $ Woop!
  33. ENTRYPOINT and CMD Explained in 4 seconds ... ENTRYPOINT [“echo”]

    CMD [“Hello”] in a Dockerfile docker run --entrypoint=”echo” my-image == $
  34. None
  35. Docker ◍ Image Builder ◍ Virtualisation ◍ Image Delivery ◍

    Orchestration (Dev) ➔ docker build ➔ docker run ➔ docker push / pull ➔ docker-compose
  36. Virtualisation Running Docker Images as Containers

  37. Demo

  38. Docker ◍ Image Builder ◍ Virtualisation ◍ Image Delivery ◍

    Orchestration (Dev) ➔ docker build ➔ docker run ➔ docker push / pull ➔ docker-compose
  39. Orchestration (Dev) Composing Services

  40. Your Super Application

  41. Your Super Application

  42. Docker Compose

  43. docker-compose.yml version: “2.1”

  44. docker-compose.yml version: “2.1” services: php: image: php:7 ports: - 80:80

    volumes: - .:/code
  45. docker-compose.yml version: “2.1” services: php: image: php:7 … database: image:

    mariadb:latest environment: MYSQL_USERNAME: rawkode MYSQL_PASSWORD: *******
  46. docker-compose.yml version: “2.1” services: php: image: php:7 … database: image:

    mariadb:latest environment: MYSQL_USERNAME: rawkode MYSQL_PASSWORD: *******
  47. docker-compose.yml version: “2.1” services: php: image: php:7 healthcheck: test: nc

    -z localhost 80 depends_on: database: condition: service_healthy
  48. Demo

  49. Docker ◍ Image Builder ◍ Virtualisation ◍ Image Delivery ◍

    Orchestration (Dev) ➔ docker build ➔ docker run ➔ docker push / pull ➔ docker-compose
  50. Tips I’ve learnt the hard way, so you don’t have

    to
  51. Tips: Use Random Ports services: php: ports: - 80

  52. Tips: Mindful of Network Collisions Every new docker-compose file is,

    potentially, a new docker network / bridge on your host. Eventually, you’ll get a collision docker-compose down
  53. Tips: Prune docker system prune Docker CE >= 17.04

  54. Tips: “Enterprise” Networks Start the Docker Engine with --dns or

    use dns: inside docker-compose.yml
  55. Tips: env_file Lots of environment variables defined inside compose.yml? Duplication?

    env_file: - some file
  56. Tips: Alpine Linux Unless you need Ubuntu / Fedora, use

    Alpine Linux Ubuntu -- 130MB Alpine -- 3.99MB
  57. Tips: Keep a Single Dockerfile Using ONBUILD, you can usually

    keep your project to a single Dockerfile ONBUILD COPY . /code echo “FROM base” > Dockerfile
  58. Tips: Routine Build of Base Images There’s no build cascading,

    ensure you have nightly / weekly / regular triggers on images that aren’t modified often
  59. Tips: Alias Everything docker-compose run --rm -it \ php cache:clear

    Vs. dcr php cache:clear
  60. Tips: Be Lazy! Boilr github:tmrts/boilr github:rawkode/boilr-docker-compose-php

  61. Docker in Production Next Time ... or ask me in

    the pub!
  62. Docker ◍ Image Builder ◍ Virtualisation ◍ Image Delivery ◍

    Orchestration (Dev) ◍ Log Collection ◍ Resource Management ◍ Orchestration (Prod) ◍ Secret Management ➔ docker build ➔ docker run ➔ docker push / pull ➔ Docker-compose ➔ docker run ➔ docker run ➔ docker swarm ➔ docker secret
  63. Questions? https://gitlab.com /rawkode /docker-guide-book