コマンド実行環境としてのDocker

3cbe54465ab56d3f1012ff98acf6aead?s=47 hamaco
December 08, 2016

 コマンド実行環境としてのDocker

Fukuoka.php Vol.20で発表したDockerの話です。

3cbe54465ab56d3f1012ff98acf6aead?s=128

hamaco

December 08, 2016
Tweet

Transcript

  1. ίϚϯυ࣮ߦ؀ڥͱͯ͠ͷ Docker 2016/12/08 Fukuoka.php Vol.20

  2. ͦΖͦΖແ৬ ᖛதҰ܄ʢ͸·ͳ͔͔ͣͻΖʣ hamaco hamanaka.kazuhiro

  3. એ఻

  4. Agenda • ͸͡Ίʹ • Docker ͱ͸ • Docker Image Λ࡞ͬͯΈΔ

    • ࠷খͷDocker ImageΛ໨ࢦͯ͠ • ·ͱΊ
  5. ͸͡Ίʹ

  6. ͜ͷൃදʹͨ͠੾ֻ͚ͬ

  7. docker-phan஌ͬͯ·͔͢?

  8. https://hub.docker.com/r/cloudflare/phan/

  9. None
  10. phan() { docker run -v $PWD:/mnt/src \ --rm -u "$(id

    -u):$(id -g)" \ cloudflare/phan:latest $@; return $?; }
  11. None
  12. None
  13. http://qiita.com/suin/items/53d184777e989693693c

  14. ં֯ͩ͠ͳʹ͔࡞Ζ͏

  15. Ͱ΋ͳʹ࡞Ζ͏͔?

  16. ͦ͏͍͑͹…

  17. ҋͷPHPerୡ͸ PHPΛશόʔδϣϯϏϧυ ͍ͯ͠ΔΒ͍͠…

  18. Ͱ΋PHPͷϏϧυͬͯ ֻ͔࣌ؒͬͯ ΊΜͲ͍͘͞ΑͶ

  19. ͦΜͳΘ͚Ͱࠓճ࡞ͬͨ΍ͭ • Docker൛ phpall • https://hub.docker.com/r/hamaco/php-all/ • https://github.com/hamaco/docker-php-all/

  20. Docker ͱ͸

  21. Docker ͱ͸ • Go ݴޠ • ίϯςφܕԾ૝Խ • LXC (Linux

    Container) • namespaces • cgroups
  22. DockerͷετϨʔδυϥΠό • AUFS (Docker for Mac / ެࣜεΫϦϓτ ) •

    DeviceMapper ( CentOS / Ubuntu ) • Btrfs • OverlayFS ( CoreOS / Barge ) • ZFS
  23. DockerͷΠϝʔδϨΠϠʔ

  24. Docker Image Λ࡞ͬͯΈΔ

  25. #01 جຊͷDocker Image

  26. #01 جຊͷDocker Image FROM alpine:3.4 ENV PHP_VERSION 7.0.13 WORKDIR /tmp

    RUN apk add --no-cache --virtual .php-builddeps \ ca-certificates bash git patch re2c flex make gcc libc-dev RUN apk add --no-cache libxml2-dev bzip2-dev RUN git clone https://github.com/hnw/phpall.git RUN wget -O php-$PHP_VERSION.tar.gz \ http://jp2.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror RUN /tmp/phpall/install-all-php.sh RUN mv /root/bin/php-$PHP_VERSION /root/bin/php RUN apk del .php-builddeps RUN rm -rf /tmp/php* ENTRYPOINT ["/root/bin/php"] CMD ["-v"]
  27. #01 جຊͷDocker Image FROM alpine:3.4 ENV PHP_VERSION 7.0.13 WORKDIR /tmp

  28. #01 جຊͷDocker Image RUN apk add --no-cache \ --virtual .php-builddeps

    \ ca-certificates bash git \ patch re2c flex make gcc libc-dev RUN apk add --no-cache \ libxml2-dev bzip2-dev
  29. #01 جຊͷDocker Image RUN git clone \ https://github.com/hnw/phpall.git RUN wget

    -O php-$PHP_VERSION.tar.gz \ http://jp2.php.net/get/php- $PHP_VERSION.tar.gz/from/this/mirror RUN /tmp/phpall/install-all-php.sh RUN mv /root/bin/php-$PHP_VERSION /root/bin/ php
  30. #01 جຊͷDocker Image RUN apk del .php-builddeps RUN rm -rf

    /tmp/php*
  31. #01 جຊͷDocker Image ENTRYPOINT ["/root/bin/php"] CMD ["-v"]

  32. 350.4 MB

  33. ࠷খͷDocker ImageΛ ໨ࢦͯ͠

  34. #02 RUNΛ1ͭʹ·ͱΊΔ

  35. #02 RUNΛ1ͭʹ·ͱΊΔ FROM alpine:3.4 ENV PHP_VERSION 7.0.13 WORKDIR /tmp RUN

    apk add --no-cache --virtual .php-builddeps \ ca-certificates bash git patch re2c flex make gcc libc-dev \ && apk add --no-cache libxml2-dev bzip2-dev \ && git clone https://github.com/hnw/phpall.git \ && wget -O php-$PHP_VERSION.tar.gz \ http://jp2.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror \ && /tmp/phpall/install-all-php.sh \ && mv /root/bin/php-$PHP_VERSION /root/bin/php \ && apk del .php-builddeps \ && rm -rf /tmp/php* ENTRYPOINT ["/root/bin/php"] CMD ["-v"]
  36. 22.95 MB

  37. Ͱ΋͜ͷঢ়ଶͩͱ

  38. Docker ImageΛ େྔʹ࡞Δͷʹແବ͕ଟ͍

  39. #03 Ϗϧυ؀ڥͷߏஙΛ ෼཭ͯ͠ΈΔ

  40. #03 ϕʔεΠϝʔδ FROM alpine:3.4 WORKDIR /work RUN apk add --no-cache

    --virtual .php-builddeps \ ca-certificates bash git make \ gcc patch re2c flex libc-dev \ && apk add --no-cache libxml2-dev bzip2-dev \ && git clone https://github.com/hnw/phpall.git
  41. #03 PHPΠϝʔδ FROM hamaco/fukuoka-php-20:03-base ENV PHP_VERSION 7.0.13 WORKDIR /work RUN

    wget -O php-$PHP_VERSION.tar.bz2 \ http://jp2.php.net/get/php-$PHP_VERSION.tar.bz2/from/this/mirror \ && /work/phpall/install-all-php.sh \ && mv /root/bin/php-$PHP_VERSION /root/bin/php \ && apk del .php-builddeps \ && rm -rf /work ENTRYPOINT ["/root/bin/php"] CMD ["-v"]
  42. 134.3 MB

  43. #04 Ϗϧυઐ༻ͷΠϝʔδΛ ༻ҙ͢Δ

  44. #04 Ϗϧυ༻Πϝʔδ FROM alpine:3.4 ADD ./scripts/make-image.bash /work/make-image.bash WORKDIR /work RUN

    apk add --no-cache \ ca-certificates bash git make gcc patch re2c \ flex libc-dev libxml2-dev bzip2-dev \ && git clone https://github.com/hnw/phpall.git ENTRYPOINT ["/work/make-image.bash"]
  45. #04 ϏϧυεΫϦϓτ #!/usr/bin/env bash basedir=/work/images mkdir -p $basedir/etc/apk/ $basedir/root/bin/ cp

    /etc/apk/repositories $basedir/etc/apk/repositories { apk add --no-cache --root "$basedir" \ --keys-dir /etc/apk/keys --initdb libxml2-dev bzip2-dev cd /work/ wget -O php-7.0.13.tar.bz2 \ http://jp2.php.net/get/php-7.0.13.tar.bz2/from/this/mirror /work/phpall/install-all-php.sh cp /root/bin/php-7.0.13 $basedir/root/bin/php } >&2 tar czf images.tar.gz . -C "$basedir" && cat images.tar.gz
  46. #04 Ϗϧυ࣮ߦ $ docker build \ -t hamaco/fukuoka-php-20:04-base base $

    docker run --rm \ hamaco/fukuoka-php-20:04-base \ > images.tar.gz
  47. #04 PHPΠϝʔδ࡞੒ FROM alpine:3.4 ADD ./images.tar.gz / ENTRYPOINT ["/root/bin/php"] CMD

    ["-v"]
  48. 22.93 MB

  49. #05 ϕʔεΠϝʔδΛ scratchʹͯ͠ΈΔ

  50. #05 scratchΛϕʔεʹ͢Δ FROM scratch ADD ./images.tar.gz / ENTRYPOINT ["/root/bin/php"] CMD

    ["-v"]
  51. 18.13 MB

  52. ·ͱΊ

  53. ·ͱΊ • DockerͰಛఆͷίϚϯυ͚ͩಈ͘؀ڥΛ࡞Δͷศར • ී௨ʹ΍ΔͳΒAlpine + RUNΛ1ͭʹ͢Ε͹OK • ؤுΔͳΒscratchΛ࢖͑Δ·Ͱ΍Δ΂͠ •

    ࠓ೔ͷDockerfile͸ҎԼʹ͋Γ·͢ • https://github.com/hamaco/fukuoka.php-vol20/
  54. Thanks!

  55. ͓·͚

  56. #06 ࠷খͷPHP؀ڥ

  57. #06 PHPʹඞཁͳϑΝΠϧ # ldd /root/bin/php /lib/ld-musl-x86_64.so.1 (0x55733b8c7000) libbz2.so.1 => /usr/lib/libbz2.so.1

    (0x7f462d436000) libz.so.1 => /lib/libz.so.1 (0x7f462d220000) libxml2.so.2 => /usr/lib/libxml2.so.2 (0x7f462cef5000) libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x55733b8c7000)
  58. #06 ඞཁͳϑΝΠϧίϐʔ $ tree docker-image/ docker-image/ !"" lib # !""

    ld-musl-x86_64.so.1 # %"" libz.so.1 !"" root # %"" bin # %"" php %"" usr %"" lib !"" libbz2.so.1 %"" libxml2.so.2
  59. #06 docker import $ cd docker-image $ tar -c .

    | docker import - \ hamaco/fukuoka-php-20:06
  60. 17.35 MB

  61. Thanks!