$30 off During Our Annual Pro Sale. View Details »

Kickass Development Environments with Docker (PHPWarks, April, 2017)

Kickass Development Environments with Docker (PHPWarks, April, 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.

David McKay

April 27, 2017
Tweet

More Decks by David McKay

Other Decks in Technology

Transcript

  1. Hi
    PHPWarks!

    View Slide

  2. Kickass Development
    Environments
    with Docker

    View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. @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

    View Slide

  13. Let’s address the whale in the room ...

    View Slide

  14. Docker & Moby
    Moby
    ◍ containerd
    ◍ RunC
    ◍ Libnetwork
    ◍ Compose
    ◍ Notary
    ◍ All the Kits
    Docker, Inc.
    ◍ Docker CE
    ◍ Docker EE

    View Slide

  15. Kickass Development
    Environments
    with DockerCE™©®

    View Slide

  16. Let us travel through time ...

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. Things eventually got better ...

    View Slide

  20. View Slide

  21. View Slide

  22. Dev / Prod Parity

    View Slide

  23. Eurgh, DSL Hell

    View Slide

  24. View Slide

  25. Problems with Vagrant
    Slow to provision
    RAM intensive
    Really, development only
    Requires a CM tool

    View Slide

  26. How long does your vagrant up take?

    View Slide

  27. View Slide


  28. Docker allows you to package an
    application with all of its dependencies into
    a standardized unit for software
    development.

    View Slide

  29. Docker
    ◍ Image Builder
    ◍ Virtualisation
    ◍ Image Delivery
    ◍ Orchestration (Dev)
    ➔ docker build
    ➔ docker run
    ➔ docker push / pull
    ➔ docker-compose

    View Slide

  30. Image Building
    Introducing the Dockerfile

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. View Slide

  37. ENTRYPOINT & CMD
    Explained in 20 seconds

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  43. View Slide

  44. Docker
    ◍ Image Builder
    ◍ Virtualisation
    ◍ Image Delivery
    ◍ Orchestration (Dev)
    ➔ docker build
    ➔ docker run
    ➔ docker push / pull
    ➔ docker-compose

    View Slide

  45. Virtualisation
    Running Docker Images as Containers

    View Slide

  46. Demo

    View Slide

  47. Docker
    ◍ Image Builder
    ◍ Virtualisation
    ◍ Image Delivery
    ◍ Orchestration (Dev)
    ➔ docker build
    ➔ docker run
    ➔ docker push / pull
    ➔ docker-compose

    View Slide

  48. Orchestration (Dev)
    Composing Services

    View Slide

  49. Your Super Application

    View Slide

  50. Your Super Application

    View Slide

  51. Docker Compose

    View Slide

  52. docker-compose.yml
    version: “3”

    View Slide

  53. docker-compose.yml
    version: “3”
    services:
    php:
    image: php:7
    ports:
    - 80:80
    volumes:
    - .:/code

    View Slide

  54. docker-compose.yml
    version: “3”
    services:
    php:
    image: php:7

    database:
    image: mariadb:latest
    environment:
    MYSQL_USERNAME: rawkode
    MYSQL_PASSWORD: *******

    View Slide

  55. docker-compose.yml
    version: “3”
    services:
    php:
    image: php:7

    database:
    image: mariadb:latest
    environment:
    MYSQL_USERNAME: rawkode
    MYSQL_PASSWORD: *******

    View Slide

  56. docker-compose.yml
    version: “3”
    services:
    php:
    image: php:7
    healthcheck:
    test: nc -z localhost 80
    depends_on:
    database:
    condition: service_healthy

    View Slide

  57. Demo

    View Slide

  58. Docker
    ◍ Image Builder
    ◍ Virtualisation
    ◍ Image Delivery
    ◍ Orchestration (Dev)
    ➔ docker build
    ➔ docker run
    ➔ docker push / pull
    ➔ docker-compose

    View Slide

  59. Tips
    I’ve learnt the hard way, so you don’t have to

    View Slide

  60. Tips: Use Random Ports
    services:
    php:
    ports:
    - 80

    View Slide

  61. 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

    View Slide

  62. Tips: Prune
    docker system prune
    Docker CE >= 17.04

    View Slide

  63. Tips: “Enterprise” Networks
    Start the Docker Engine with
    --dns
    or use
    dns:
    inside docker-compose.yml

    View Slide

  64. Tips: env_file
    Lots of environment variables defined
    inside compose.yml? Duplication?
    env_file:
    - some file

    View Slide

  65. Tips: Alpine Linux
    Unless you need Ubuntu / Fedora, use
    Alpine Linux
    Ubuntu -- 130MB
    Alpine -- 3.99MB

    View Slide

  66. Tips: Keep a Single Dockerfile
    Using ONBUILD, you can usually keep your
    project to a single Dockerfile
    ONBUILD COPY . /code
    echo “FROM base” > Dockerfile

    View Slide

  67. 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

    View Slide

  68. Tips: Alias Everything
    docker-compose run --rm -it \
    php cache:clear
    Vs.
    dcr php cache:clear

    View Slide

  69. Tips: Be Lazy!
    Boilr
    github:tmrts/boilr
    github:rawkode/boilr-docker-compose-php

    View Slide

  70. Docker in Production
    Next Time ... or ask me in the pub!

    View Slide

  71. 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

    View Slide

  72. Questions?

    View Slide