Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

ゆうきゃん

July 18, 2019
Tweet

More Decks by ゆうきゃん

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • 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
  2. Laravel؀ڥΛߏங͢Δํ๏ • ௚Πϯετʔϧ • XAMPP, MAMP • Laravel Valet •

    Cloud9, IBM Cloud • Vagrant (VirtualBox, VMware) • Homestead • Docker • Laradock
  3. ϋΠύʔόΠβܕԾ૝Ϛγϯ • ESXi, Hyper-V, Xen౳ • ͢΂ͯͷOSΛԾ૝Խ؀ڥͰಈ࡞ • ϋʔυ΢ΣΞΛ௚઀੍ޚՄೳ •

    Ծ૝Ϛγϯͷ଎౓௿ԼΛ࠷খݶʹ཈͑ΒΕΔ • طଘͷϗετOSΛͦͷ··ར༻Ͱ͖ͳ͍ • ઐ༻ͷ෺ཧαʔόʔΛ༻ҙ͢Δඞཁ͕͋Δ
  4. ίϯςφܕԾ૝Խ • VirtualBox౳ͷ׬શԾ૝ԽΛߦ͏ϋΠύʔόΠβܕ΍ϗετܕʹൺ΂ͯɺ • σΟεΫ༰ྔ͸গͳ͍ • Ծ૝؀ڥͷ࡞੒΍ىಈ͸଎͘ • ੑೳྼԽ͕΄ͱΜͲͳ͍ •

    OSͱҟͳΔγεςϜΛίϯςφͰىಈͤ͞Δ͜ͱ͸Ͱ͖ͳ͍ • ΧʔωϧΛશͯͷίϯςφͰڞ༗͢ΔͨΊɺΧʔωϧʹର͢Δมߋૢ࡞ Λݸผʹߦ͏͜ͱ͕Ͱ͖ͳ͍
  5. Docker Πϝʔδ • ಡΈࠐΈઐ༻ͷςϯϓϨʔτ • ΠϝʔδϨΠϠ(૚)ͷੵΈॏͶ • ϑΝΠϧγεςϜͱΞϓϦέʔγϣϯ΍ϛυϧ΢ΣΞɺ࣮ߦ࣌ʹ ඞཁͱ͢Δύϥϝʔλ͔Βߏ੒ •

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

    ϨδετϦͱͯ͠ Docker Hub Λఏڙ • ϓϥΠϕʔτͳ Docker ϨδετϦΛࣗલͰߏஙͨ͠Γɺ AWS(ECR), GCP(Container Registry)ͳͲͷαʔϏε΋࢖͑ ·͢
  7. Dockerͷίϯϙʔωϯτ·ͱΊ • ΞϓϦέʔγϣϯΛอ࣋͢Δ Docker ΠϝʔδΛ ߏஙͰ͖Δɻ • ͜ΕΒ Docker ΠϝʔδͰΞϓϦέʔγϣϯΛ࣮

    ߦ͢Δ Docker ίϯςφΛ࡞੒Ͱ͖Δɻ • ͜ΕΒ Docker ΠϝʔδΛ Docker Hub ΍ࣗ෼ͷ ϨδετϦͰڞ༗Ͱ͖Δɻ
  8. 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. ...
  9. 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
  10. ΦϓγϣϯͷڍಈΛ֬ೝ͢Δ $ docker run -i ubuntu /bin/bash $ docker run

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

    $ docker rmi <IMAGE ID> # コンテナ、イメージ全て削除 $ docker system prune -af # コンテナ、イメージ、ボリューム全て削除 $ docker system prune -af --volumes
  12. ࠷ऴతͳσΟϨΫτϦߏ੒ . ├── 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
  13. Laravel5.8ͷΠϯετʔϧཁ݅ • PHP >= 7.1.3 • BCMath PHP֦ு • Ctype

    PHP֦ு • JSON PHP֦ு • Mbstring PHP֦ு • OpenSSL PHP֦ு • PDO PHP֦ு • Tokenizer PHP֦ு • XML PHP֦ு
  14. 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
  15. docker-compose.ymlͷิ଍ • version: docker-composeͷઃఆϑΝΠϧͷόʔδϣϯ(ࠓ͸3.7͕࠷ ৽) • ֤αʔϏε͸image·ͨ͸buildΛࢦఆ͢Δ • context: DockerϑΝΠϧ΁ͷ૬ରύε

    • args: DockerϑΝΠϧʹ౉͢Ҿ਺ • volumes: ϘϦϡʔϜͱͯ͠DockerίϯςφʹϚ΢ϯτ • working_dir: ࡞ۀ৔ॴ(ίϯςφʹೖͬͨͱ͖ͷ৔ॴʹͳΔ) • environment: ؀ڥม਺Λઃఆ
  16. 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
  17. 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ͷαΠζ
  18. docker/php/Dockerfile • FROM ͰϕʔεΠϝʔδΛࢦఆ • LABEL Ͱ࡞ऀΛઃఆ • ARG Ͱ

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

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

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

    docker-php-ext-install
 LaravelͰඞཁͳPHP֦ுϥΠϒϥϦΛΠϯε τʔϧ • ComposerͷΠϯετʔϧˍߴ଎Խઃఆ
  22. 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
  23. Ϗϧυ • docker-compose up ͸ docker-compose.yml ʹఆٛͨ͠αʔϏεΛىಈ͠ ·͢ɻ • -d

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