Laravel + Docker 環境構築ハンズオン

Laravel + Docker 環境構築ハンズオン

39e7d3ad2417a5fd26d816a51c99e7ce?s=128

ゆうきゃん

July 18, 2019
Tweet

Transcript

  1. Laravel + Docker ؀ڥߏஙϋϯζΦϯ by @ucan_lab

  2. ࣗݾ঺հ • Name: Ώ͏͖ΌΜ(@ucan_lab) • Company: גࣜձࣾROLO • Skill: PHPer(6೥)/Laravel(3೥)/GraphQL(1೥)/Docker(൒೥)

    • Favorite: λϐΦΧ/εϚϒϥSP/αόήʔ • Twitter: https://twitter.com/ucan_lab • Qiita: https://qiita.com/ucan-lab • MENTA: https://menta.work/user/6135
  3. Ξϯέʔτ

  4. Laravel։ൃ؀ڥΛ ߏங͢Δํ๏

  5. Laravel؀ڥΛߏங͢Δํ๏ • ௚Πϯετʔϧ • XAMPP, MAMP • Laravel Valet •

    Cloud9, IBM Cloud • Vagrant (VirtualBox, VMware) • Homestead • Docker • Laradock
  6. ͲΕΛ࢖͑͹͍͍ΜͩΖ͏

  7. ؀ڥߏஙͷ͋Ε͜Ε • νʔϜͰಉ͡؀ڥͰ։ൃ͍ͨ͠ • ߏ੒มߋ࣌ʹଈ࣌ڞ༗͍ͨ͠ • ଞϓϩδΣΫτͷઃఆΛӨڹΛड͚ͨ͘ͳ͍ • ޷͖ͳΤσΟλͰ։ൃ͍ͨ͠ •

    Excelͷखॱॻ͸։͖ͨ͘ͳ͍
  8. Ծ૝Խٕज़ʹ͍ͭͯ

  9. ϗετܕԾ૝Ϛγϯ

  10. ϗετܕԾ૝Ϛγϯ • VirtualBox, VMware౳ • Ծ૝తͳPCʢCPUɺϝϞϦɺετϨʔδɺωοτϫʔ ΫΞμϓλɺUSB౳ʣΛγϛϡϨʔτ • ར༻͍ͯ͠Δαʔόʔ΍PCʹಋೖ͕खܰ •

    ϗετOSΛܦ༝ͯ͠ϋʔυ΢ΣΞ΁ΞΫηε͢ΔͷͰ Φʔόʔϔου͕େ͖͍
  11. ϋΠύʔόΠβܕԾ૝Ϛγϯ

  12. ϋΠύʔόΠβܕԾ૝Ϛγϯ • ESXi, Hyper-V, Xen౳ • ͢΂ͯͷOSΛԾ૝Խ؀ڥͰಈ࡞ • ϋʔυ΢ΣΞΛ௚઀੍ޚՄೳ •

    Ծ૝Ϛγϯͷ଎౓௿ԼΛ࠷খݶʹ཈͑ΒΕΔ • طଘͷϗετOSΛͦͷ··ར༻Ͱ͖ͳ͍ • ઐ༻ͷ෺ཧαʔόʔΛ༻ҙ͢Δඞཁ͕͋Δ
  13. ίϯςφ

  14. ίϯςφܕԾ૝Խ • VirtualBox౳ͷ׬શԾ૝ԽΛߦ͏ϋΠύʔόΠβܕ΍ϗετܕʹൺ΂ͯɺ • σΟεΫ༰ྔ͸গͳ͍ • Ծ૝؀ڥͷ࡞੒΍ىಈ͸଎͘ • ੑೳྼԽ͕΄ͱΜͲͳ͍ •

    OSͱҟͳΔγεςϜΛίϯςφͰىಈͤ͞Δ͜ͱ͸Ͱ͖ͳ͍ • ΧʔωϧΛશͯͷίϯςφͰڞ༗͢ΔͨΊɺΧʔωϧʹର͢Δมߋૢ࡞ Λݸผʹߦ͏͜ͱ͕Ͱ͖ͳ͍
  15. ݸਓతͳDockerͷϝϦοτ • গͳ͍खॱͰಉ͡؀ڥΛߏஙͰ͖Δ • Ծ૝؀ڥͷ࡞੒΍ىಈ͕଎͍ • Vagrantͷ࣌ͷΑ͏ʹChef΍Ansible౳৭ʑ֮͑ͳ͘ ͯྑ͍ • ෳ਺ͷ؀ڥΛ༰қʹ࡞੒Ͱ͖Δ

    • ࢖͍ࣺͯ͠΍͍͢
  16. DockerͷσϝϦοτ • ϑΝΠϧ໊ͷେจࣈɾখจࣈΛ۠ผ͢Δ͔Ͳ ͏͔͸OS΍ϑΝΠϧγεςϜʹΑͬͯҟͳΔ • ಛʹࠔΔ΄Ͳ·ͩ࢖͍͜ͳͤͯͳ͍

  17. DockerͷΞʔΩςΫνϟ

  18. DockerͷΞʔΩςΫνϟ

  19. DockerͷΞʔΩςΫνϟ • Docker͸ΫϥΠΞϯτɾαʔόܕͷΞʔΩςΫνϟ • DockerΫϥΠΞϯτͱσʔϞϯ͸ɺ͓ޓ͍ʹιέοτ ͔ RESTful APIͰ௨৴͢Δ • ΫϥΠΞϯτͱσʔϞϯಉ͡γεςϜ্Ͱ΋࣮ߦՄ

    ೳ • DockerσʔϞϯ͸ίϯςφͷߏஙɾ࣮ߦɾ഑෍Λߦ͏
  20. Dockerͷίϯϙʔωϯτ • Docker Πϝʔδʢimageʣ • Docker ϨδετϦʢregistryʣ • Docker ίϯςφʢcontainerʣ

  21. Docker Πϝʔδ • ಡΈࠐΈઐ༻ͷςϯϓϨʔτ • ΠϝʔδϨΠϠ(૚)ͷੵΈॏͶ • ϑΝΠϧγεςϜͱΞϓϦέʔγϣϯ΍ϛυϧ΢ΣΞɺ࣮ߦ࣌ʹ ඞཁͱ͢Δύϥϝʔλ͔Βߏ੒ •

    ଞͷਓ͕࡞੒ͨ͠ Docker ΠϝʔδΛμ΢ϯϩʔυ͢Δ͜ͱ΋Մ ೳ • Dockerfileʹ໋ྩΛهड़͠ɺ৽͍͠ΠϝʔδϨΠϠΛ௥Ճ͍ͯ͘͠
  22. Docker ϨδετϦ • Docker ϨδετϦ͸ΠϝʔδΛอ࣋ • ύϒϦοΫ΋͘͠͸ϓϥΠϕʔτʹอ؅ • ύϒϦοΫͳ Docker

    ϨδετϦͱͯ͠ Docker Hub Λఏڙ • ϓϥΠϕʔτͳ Docker ϨδετϦΛࣗલͰߏஙͨ͠Γɺ AWS(ECR), GCP(Container Registry)ͳͲͷαʔϏε΋࢖͑ ·͢
  23. Docker ίϯςφ • ϗετOS্Ͱઃఆ΍ϑΝΠϧΛಠཱͯ͠࡞੒ Ͱ͖ΔശͷΑ͏ͳ΋ͷ • DockerΠϝʔδΛϏϧυ͢Δ͜ͱͰ࡞੒ • ໊લۭؒ΍Ϧιʔε͕ଞͷϓϩηε΍ίϯς φ͔Β͸ִ཭ɺಠཱͨ͠ίϯϐϡʔλ্Ͱಈ

    ࡞͍ͯ͠Δ͔ͷΑ͏ʹৼΔ෣͏
  24. Dockerͷίϯϙʔωϯτ·ͱΊ • ΞϓϦέʔγϣϯΛอ࣋͢Δ Docker ΠϝʔδΛ ߏஙͰ͖Δɻ • ͜ΕΒ Docker ΠϝʔδͰΞϓϦέʔγϣϯΛ࣮

    ߦ͢Δ Docker ίϯςφΛ࡞੒Ͱ͖Δɻ • ͜ΕΒ Docker ΠϝʔδΛ Docker Hub ΍ࣗ෼ͷ ϨδετϦͰڞ༗Ͱ͖Δɻ
  25. DockerΛମݧ͠Α͏

  26. DockerͰHello World

  27. docker run hello-world $ docker run hello-world Unable to find

    image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bc c1d20f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. ...
  28. docker run hello-worldͷิ଍ • runίϚϯυͰ͸3ͭͷ͜ͱΛҰ౓ʹߦ͏ • Πϝʔδͷऔಘ(pull) • ίϯςφͷ࡞੒(create) •

    ίϯςφͷىಈ(start)
  29. DockerͰUbuntuΛ࢖͏

  30. ubuntuΛىಈ͢Δ $ docker pull ubuntu $ docker images $ docker

    create ubuntu $ docker ps -a $ docker start <CONTAINER ID> $ docker ps -a $ docker run -it <CONTAINER ID> /bin/bash root@<CONTAINER ID>:/# cat /etc/os-release NAME="Ubuntu" VERSION="18.04.2 LTS (Bionic Beaver)" ... root@<CONTAINER ID>:/# exit
  31. psίϚϯυΦϓγϣϯͷิ଍ • -a ఀࢭ͍ͯ͠Δίϯςφ΋ؚΊͯදࣔ

  32. runίϚϯυΦϓγϣϯͷҙຯ • -i Dockerίϯςφىಈ࣌ʹɺඪ४ೖྗ(STDIN) ΛอͭΑ͏ʹ͢Δ • -t Ծ૝୺຤(pseudo0TTY)ΛίϯςφʹׂΓ౰ ͯΔ

  33. ΦϓγϣϯͷڍಈΛ֬ೝ͢Δ $ docker run -i ubuntu /bin/bash $ docker run

    -t ubuntu /bin/bash $ docker run ubuntu /bin/bash
  34. ίϯςφɺΠϝʔδͷ࡟আ # コンテナIDを指定して削除 $ docker rm <CONTAINER ID> # イメージIDを指定して削除

    $ docker rmi <IMAGE ID> # コンテナ、イメージ全て削除 $ docker system prune -af # コンテナ、イメージ、ボリューム全て削除 $ docker system prune -af --volumes
  35. None
  36. None
  37. Docker Compose ͱ͸ • ෳ਺ίϯςφͷߏஙखॱΛઃఆϑΝΠϧͰ؅ཧ • 1ͭͷϑΝΠϧʹෳ਺ͷαʔϏεΛఆٛ • 1ίϚϯυͰෳ਺ίϯςφΛىಈɾఀࢭ •

    DockerϨδετϦ͔Β1ͭͣͭΠϯετʔϧ͢ ΔΑΓ෼͔Γ΍͍͢
  38. Dockerͷ֓ཁઆ໌͓ΘΓ

  39. ࠓճͷϋϯζΦϯͷΰʔϧ

  40. ΰʔϧ • app(php-fpm)ίϯςφΛཱͯΔ • web(nginx)ίϯςφΛཱͯΔ • LaravelΛΠϯετʔϧ͢Δ • db(MySQL)ίϯςφΛཱͯΔ •

    ؀ڥΛഁغͯ͠ɺ࠶ߏங͢Δ
  41. ࠷ऴతͳσΟϨΫτϦߏ੒ . ├── README.md ├── docker │ ├── mysql │

    │ └── my.cnf │ ├── nginx │ │ └── default.conf │ └── php │ ├── Dockerfile │ └── php.ini ├── docker-compose.yml ├── logs │ ├── access.log │ ├── error.log │ ├── mysql-error.log │ ├── mysql-query.log │ ├── mysql-slow.log │ └── php-error.log └── src └── readme.md
  42. Laravel5.8ͷΠϯετʔϧཁ݅ • PHP >= 7.1.3 • BCMath PHP֦ு • Ctype

    PHP֦ு • JSON PHP֦ு • Mbstring PHP֦ு • OpenSSL PHP֦ு • PDO PHP֦ு • Tokenizer PHP֦ு • XML PHP֦ு
  43. ϋϯζΦϯελʔτʂ https://qiita.com/ucan-lab/items/ 56c9dc3cf2e6762672f4

  44. appίϯςφΛ࡞Δ

  45. COMPOSE_PROJECT_NAME=docker-laravel-handson DB_NAME=homestead DB_USER=homestead DB_PASS=secret TZ=Asia/Tokyo .env

  46. ؀ڥϑΝΠϧ • docker-compose.ymlͱಉ͡Ґஔʹ഑ஔ • σϑΥϧτͷ؀ڥม਺Λఆٛ • COMPOSE_PROJECT_NAME
 ͸ComposeͷϓϩδΣΫτ໊Λࢦఆ
 σϑΥϧτ͸ϓϩδΣΫτͷσΟϨΫτϦ

  47. ؀ڥϑΝΠϧ-ࢀߟࢿྉ • http://docs.docker.jp/compose/env-file.html • http://docs.docker.jp/compose/reference/ envvars.html

  48. version: "3" services: app: build: context: ./docker/php args: - TZ=${TZ}

    volumes: - ./src:/work - ./logs:/var/log/php - ./docker/php/php.ini:/usr/local/etc/php/php.ini working_dir: /work environment: - DB_CONNECTION=mysql - DB_HOST=db - DB_DATABASE=${DB_NAME} - DB_USERNAME=${DB_USER} - DB_PASSWORD=${DB_PASS} - TZ=${TZ} docker-compose.yml
  49. docker-compose.ymlͷิ଍ • version: docker-composeͷઃఆϑΝΠϧͷόʔδϣϯ(ࠓ͸3.7͕࠷ ৽) • ֤αʔϏε͸image·ͨ͸buildΛࢦఆ͢Δ • context: DockerϑΝΠϧ΁ͷ૬ରύε

    • args: DockerϑΝΠϧʹ౉͢Ҿ਺ • volumes: ϘϦϡʔϜͱͯ͠DockerίϯςφʹϚ΢ϯτ • working_dir: ࡞ۀ৔ॴ(ίϯςφʹೖͬͨͱ͖ͷ৔ॴʹͳΔ) • environment: ؀ڥม਺Λઃఆ
  50. FROM php:7.3.7-fpm-alpine LABEL maintainer "ucan-lab" ARG TZ ENV COMPOSER_ALLOW_SUPERUSER 1

    ENV COMPOSER_HOME /composer RUN set -eux && \ apk add --update-cache --no-cache --virtual=.build-dependencies tzdata && \ cp /usr/share/zoneinfo/${TZ} /etc/localtime && \ apk del .build-dependencies && \ docker-php-ext-install bcmath pdo_mysql && \ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/ bin --filename=composer && \ composer config -g repos.packagist composer https://packagist.jp && \ composer global require hirak/prestissimo docker/php/Dockerfile
  51. FROM໋ྩͷิ଍ • Alpine Linux • LinuxσΟετϦϏϡʔγϣϯ • ૊ΈࠐΈܥͰΑ͘࢖ΘΕ͍ͯΔ • BusyBoxͱmuslΛϕʔε

    • BusyBox + ύοέʔδϚωʔδϟ(apk)͕AlpineLinux
  52. Alpine Linux ͷΠϝʔδαΠζ $ docker pull alpine $ docker pull

    ubuntu $ docker pull centos $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest b7b28af77ffe 6 days ago 5.58MB ubuntu latest 4c108a37151f 4 weeks ago 64.2MB centos latest 9f38484d220f 4 months ago 202MB • Alpine͸Θ͔ͣ5MB • Ubuntuͷ1/12ɺCentOSͷ1/36ͷαΠζ
  53. docker/php/Dockerfile • FROM ͰϕʔεΠϝʔδΛࢦఆ • LABEL Ͱ࡞ऀΛઃఆ • ARG Ͱ

    docker-compose.ymlͷҾ਺औಘ • ENV Ͱ ؀ڥม਺ઃఆ • RUN ͰඞཁͳϥΠϒϥϦΛΠϯετʔϧ
  54. RUN໋ྩͷิ଍1 • set -eux • -e θϩ(ਖ਼ৗ)Ͱͳ͍εςʔλεͰίϚϯυ͕ऴྃ͠ ͨΒɺଈ࠲ʹऴྃ͢Δɻ • -u

    ະׂΓ౰ͯͷม਺ͳͲ͕͋Ε͹Τϥʔͱ͢Δɻ • -x ίϚϯυΛ࣮ߦͨ࣌͠ʹɺͦͷίϚϯυͱҾ਺ Λදࣔ͢Δɻ
  55. RUN໋ྩͷิ଍2 • set -eux γΣϧεΫϦϓτͷΦϓγϣϯ • -e θϩ(ਖ਼ৗ)Ͱͳ͍εςʔλεͰίϚϯυ͕ऴྃͨ͠ Βɺଈ࠲ʹऴྃ͢Δɻ •

    -u ະׂΓ౰ͯͷม਺ͳͲ͕͋Ε͹Τϥʔͱ͢Δɻ • -x ίϚϯυΛ࣮ߦͨ࣌͠ʹɺͦͷίϚϯυͱҾ਺Λද ࣔ͢Δɻ
  56. RUN໋ྩͷิ଍3 • apk add ʙ apk del ·Ͱ
 alpineͷλΠϜκʔϯΛઃఆ͍ͯ͠Δ •

    docker-php-ext-install
 LaravelͰඞཁͳPHP֦ுϥΠϒϥϦΛΠϯε τʔϧ • ComposerͷΠϯετʔϧˍߴ଎Խઃఆ
  57. RUN໋ྩͷิ଍4 • RUN, ADD, COPY໋ྩΛ࣮ߦ͢ΔͱΠϝʔδ ϨΠϠ͕1ຕ૿͑Δ • ΠϝʔδϨΠϠͷ਺ΛݮΒͨ͢Ίʹ && \

    Ͱϫ ϯϥΠφʔʹઃఆ͍ͯ͠Δ • ෆཁͳϥΠϒϥϦ͸ೖΕͳ͍ɺ࢒͞ͳ͍
  58. error_reporting = E_ERROR | E_WARNING | E_PARSE | E_NOTICE display_errors

    = stdout display_startup_errors = on log_errors = on error_log = /var/log/php/php-error.log upload_max_filesize = -1 memory_limit = -1 post_max_size = 100M max_execution_time = 900 max_input_vars = 100000 default_charset = UTF-8 [Date] date.timezone = ${TZ} [mbstring] mbstring.language = Japanese docker/php/php.ini
  59. docker/php/php.ini • Τϥʔϩάͷઃఆ • ϝϞϦपΓͷઃఆ • จࣈίʔυͷઃఆ • ೔ຊޠ؀ڥͷઃఆ(mbstring) •

    λΠϜκʔϯͷઃఆ
  60. Ϗϧυ • docker-compose up ͸ docker-compose.yml ʹఆٛͨ͠αʔϏεΛىಈ͠ ·͢ɻ • -d

    ʮσλονυʯϞʔυͰίϯςφΛىಈ͠·͢ɻ • σϑΥϧτ͸ʮΞλονυʯϞʔυͰશͯͷίϯςφϩάΛը໘্ʹදࣔ • ʮσλονυʯϞʔυͰ͸όοΫάϥ΢ϯυͰಈ࡞ • --build ίϯςφͷ։࢝લʹΠϝʔδΛߏங͠·͢ • ಛʹมߋ͕ͳ͍৔߹͸Ωϟογϡ͕࢖༻͞Ε·͢ɻ $ docker-compoose up -d --build
  61. webίϯςφΛ࡞Δ

  62. web: image: nginx:1.17.1-alpine depends_on: - app ports: - 10080:80 volumes:

    - ./src:/work - ./logs:/var/log/nginx - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf environment: - TZ=${TZ} docker-compose.yml
  63. • image: ϕʔεΠϝʔδΛͦͷ··࢖͏ • depends_on: αʔϏεͷىಈॱ൪ઃఆ • ports: ϙʔτެ։ઃఆ(ϗετ:ίϯςφ) docker-compose.ymlͷิ଍

  64. server { listen 80; root /work/public; index index.php; charset utf-8;

    location / { root /work/public; try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass app:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } docker/nginx/default.conf
  65. docker/nginx/default.confͷิ଍ • nginxͷઃఆϑΝΠϧ • fastcgi_pass app:9000; • αʔϏε໊Λࢦఆͯ͠php-fpmͷύεΛࢦ ఆͰ͖·͢

  66. LaravelΛΠϯετʔϧ͢Δ

  67. db: image: mysql:8.0.16 volumes: - db-store:/var/lib/mysql - ./logs:/var/log/mysql - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf

    environment: - MYSQL_DATABASE=${DB_NAME} - MYSQL_USER=${DB_USER} - MYSQL_PASSWORD=${DB_PASS} - MYSQL_ROOT_PASSWORD=${DB_PASS} - TZ=${TZ} volumes: db-store: driver: local LaravelΛΠϯετʔϧ͢Δ
  68. Composer Create-Project • LaravelΠϯετʔϥ • ࠷৽൛͕Πϯετʔϧ͞ΕΔ • Composer Create-Project •

    όʔδϣϯࢦఆ͕Ͱ͖Δ
  69. dbίϯςφΛ࡞Δ

  70. db: image: mysql:8.0.16 volumes: - db-store:/var/lib/mysql - ./logs:/var/log/mysql - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf

    environment: - MYSQL_DATABASE=${DB_NAME} - MYSQL_USER=${DB_USER} - MYSQL_PASSWORD=${DB_PASS} - MYSQL_ROOT_PASSWORD=${DB_PASS} - TZ=${TZ} volumes: db-store: driver: local docker-compose.yml
  71. docker-compose.ymlͷิ଍ • db-store: σʔλϘϦϡʔϜίϯς
 (໊લ෇͖ϘϦϡʔϜ΍τοϓϨϕϧϘϦϡʔϜͱ ΋ݺͿ) • ޡͬͯσʔλΛ࡟আ͠ʹ͍͘ͷͰ͓͢͢Ί

  72. [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_bin default-time-zone = SYSTEM

    log_timestamps = SYSTEM default-authentication-plugin = mysql_native_password log-error = /var/log/mysql/mysql-error.log slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 5.0 log_queries_not_using_indexes = 0 general_log = 1 general_log_file = /var/log/mysql/mysql-query.log [mysql] default-character-set = utf8mb4 [client] default-character-set = utf8mb4 docker/mysql/my.cnf
  73. docker/mysql/my.cnfͷิ଍ • จࣈίʔυͷઃఆ • λΠϜκʔϯͷઃఆ • ௨ৗϩάɺεϩʔΫΤϦϩάɺΤϥʔϩάઃఆ • σϑΥϧτೝূϓϥάΠϯͷมߋ •

    ৽͍͠઀ଓϓϥάΠϯ͕αϙʔτ͞Ε͍ͯͳ͍ͨΊ
  74. ͓ΘΓ