Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

今こそ深堀りする、PHPのDockerイメージ / understand_php_docker

今こそ深堀りする、PHPのDockerイメージ / understand_php_docker

PHP Conference Japan 2021の発表資料です。
https://phpcon.php.gr.jp/2021/

トーク中で使用したCakePHPの環境構築
https://github.com/rukiadia/php-sample-docker

スライド中のURL一覧
・PHP公式イメージのタグ一覧
https://github.com/docker-library/docs/blob/master/php/README.md#supported-tags-and-respective-dockerfile-links
・「PHPのレジェンドシステムをEC2からKubernetesに移行する話 その5 〜PHPアプリケーションをコンテナ化しよう〜」
※alpineを見送った事例として紹介させていただいたURL
https://creators-note.chatwork.com/entry/2021/01/13/144022
・「CakePHPのインストール」
https://book.cakephp.org/4/ja/installation.html
・intl拡張モジュールのインストール要件
https://www.php.net/manual/ja/intl.requirements.php
・拡張モジュールの一覧/分類
https://www.php.net/manual/ja/extensions.php
・Dockerfileのコードリーディングに使用したイメージ
https://github.com/docker-library/php/blob/8d98db4cebcdf5ff776e2d0b9bf4a5b482f4e657/8.0/bullseye/cli/Dockerfile

TwitterID:@rukiadia

October 02, 2021
Tweet

More Decks by TwitterID:@rukiadia

Other Decks in Programming

Transcript

  1. ܰࣗ͘ݾ঺հ • ࢁాɹ঵ʢ΍·ͩɹ͠ΐ͏ʣ • Twitter : @rukiadia • PHP Conferenceʹొஃͤͯ͞΋Β͏ͷ͸ೋճ໨

    • ίωώτגࣜձࣾͰόοΫΤϯυΤϯδχΞΛ͍ͯ͠·͢ɻ • ʮCakePHPʯʮTypeScript x React ʯΛॻ͘ࣄ͕ଟ͍Ͱ͢ɻ
  2. 8FCαʔόʔ͕ͳ͍ͱࠔΔ w ʰWFSTJPOBQBDIFʱͷΠϝʔδΛબ୒͢Δɻ w ྫɿBQBDIFCVMMTFZFɺBQBDIF w ΋ͪΖΜɺBQBDIFΛޙ͔ΒೖΕΔ͜ͱ΋ՄೳͰ͢ɻ w ʮͰ΋ɺࢲ͕࢖͍͍ͨͷ͸OHJOYͳΜͰ͕͢ɾɾʯ w

    ެ͕ࣜఏڙ͍ͯ͠ΔϓϦηοτ͸ͳ͍ɻʢͳΜͰ΍ɾɾʣ w OHJOYͷίϯςφΛ࡞ͬͯɺQIQΛಈ͔͍ͯ͠ΔίϯςφʹϦΫΤε τΛඈ͹͢ߏ੒ʹ͢Δͷ͕ͭͷखɻ˞ޙड़
  3. ࠓճ૝ఆ͢Δ؀ڥ w 1)1ܥ˞ࠓճ͸%FCJBOܥͷΠϝʔδΛ࢖༻͠·͢ w $BLF1)1Yܥ w 8FMDPNFϖʔδ͕։͚Δͱ͜Ζ·ͰਐΊΔɻ w QIQGQN OHJOY

    w OHJOYͳͷ͸ʰ*46$0/ʱͱ͍͏ΠϕϯτͰ৮Δ͖͔͚͕ͬ૿͔͑ͨ ΒͳΜͱͳ͘Ͱ͢ɻ w ˞಺༰͸(JU)VCͷϦϙδτϦͰڞ༗͠·͢
  4. EPDLFSDPNQPTFZNMͷ಺༰ version: ‘3’ services: nginx: image: nginx:1.21 ports: - 80:80

    volumes: - ./source:/usr/share/nginx/html - ./nginx/default.conf:/etc/nginx/conf.d/default.conf php-fpm: build: ./php-fpm volumes: - ./source:/var/www/html
  5. QIQGQN OHJOYͷઃఆ # pass the PHP scripts to FastCGI server

    listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} location ~ \.php$ { root /var/www/html/; fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } OHJOY΁ͷϦΫΤετ࣌ 1)1ͷίϯςφͱ௨৴͢Δ
  6. ·ͣ͸$BLF1)1ͷཁ݅Λ֬ೝ w ϑϨʔϜϫʔΫʹ͸جຊతʹγεςϜཁ͕݅͋ΔͷͰɺ$BLF1)1ͷཁ ݅ΛݟͯΈ·͢ɻ w )551αʔόʔʢ"QBDIFɺNPE@SFXSJUF͕ਪ঑͞ΕΔʣ w 1)1 w NCTUSJOH1)1֦ுɺJOUM1)1֦ுɺTJNQMFYNM1)1֦ு

    w ࢖༻͢ΔσʔλϕʔεʹԠͨ͡1%0֦ுϞδϡʔϧ w ࢀߟIUUQTCPPLDBLFQIQPSHKBJOTUBMMBUJPOIUNM ࠓճͷτʔΫͰ஫໨͍ͨ͠ͷ͸ ͜͜ͷ෦෼ʂ
  7. $BLF1)1ʹඞཁͳ֦ுϞδϡʔϧ w $BLF1)1͸ҎԼͷ1)1֦ுϞδϡʔϧΛඞཁͱ͠·͢ɻ w NCTUSJOH1)1֦ு w ϚϧνόΠτରԠͷจࣈྻؔ਺Λఏڙ w JOUM1)1֦ு w

    ࠃࡍԽରԠ༻ͷػೳʢλΠϜκʔϯ΍೔෇ܗࣜͳͲʣΛఏڙ w TJNQMFYNM1)1֦ு w 9.-ΛΦϒδΣΫτͱͯ͠ѻ͏ػೳ w 1%0֦ுϞδϡʔϧ w 1%0ʢ1)1%BUB0CKFDUʣͷ֦ுϞδϡʔϧɻ1)1͔ΒσʔλϕʔεʹΞΫηε͢ΔΠϯλʔϑΣΠεΛఏڙ w ˞࢖༻͢ΔσʔλϕʔεʹԠͨ͡υϥΠό͕ඞཁ
  8. ೖ͍ͬͯΔϞδϡʔϧͷ֬ೝ # php -m [PHP Modules] Core ctype curl date

    dom fileinfo filter ftp … # php -m | grep mbstring Mbstring QIQNͰҰཡදࣔͰ͖Δɻ ಛఆͷϞδϡʔϧ͚ͩ֬ೝ͚ͨ͠Ε͹ɺ HSFQΛซ༻͢Δͷָ͕ɻ
  9. 1)1༻Πϝʔδɾͦͷ FROM php:8.0-fpm # add extension module RUN docker-php-ext-install pdo_mysql

    intl # docker-php-ext-install͸ެࣜͷPHPΠϝʔδ͕ఏڙ͍ͯ͠Δʮ֦ுϞδϡʔϧૢ࡞πʔϧʯ # ެࣜΠϝʔδΛ࢖͏ϝϦοτͷ1ͭ ֦ுϞδϡʔϧΛΠϯετʔϧ
  10. 1)1༻Πϝʔδɾͦͷ FROM php:8.0-fpm RUN apt update RUN apt install -y

    libicu-dev # add extension module RUN docker-php-ext-install pdo_mysql intl ֦ுϞδϡʔϧͷϏϧυʹ࢖༻͢Δ ύοέʔδͷ௥Ճ
  11. 1)1༻Πϝʔδɾ׬੒ܗ FROM php:8.0-fpm RUN apt update RUN apt install -y

    git zip unzip libicu-dev # add extension module RUN docker-php-ext-install pdo_mysql intl # install composer COPY --from=composer:2.0.12 /usr/bin/composer /usr/bin/composer $BLF1)1ຊମͷऔಘ͸ DPNQPTFSʹ೚ͤΔɻ ඞཁͳ΋ͷΛผ్௥Ճɻ
  12. ิ଍OHJOYͷઃఆ location / { root /usr/share/nginx/html/my_app/webroot; index index.html index.htm index.php;

    } location ~ \.php$ { root /var/www/html/my_app/webroot; fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } $BLF1)1ͷެ։༻σΟϨΫτϦΛࢦఆ
  13. FROM debian:bullseye-slim # prevent Debian’s PHP packages from being installed

    RUN set -eux; \ { \ echo ‘Package: php*’; \ echo ‘Pin: release *’; \ echo ‘Pin-Priority: -1’; \ } > /etc/apt/preferences.d/no-debian-php # dependencies required for running “phpize” # (see persistent deps below) ENV PHPIZE_DEPS \ autoconf \ dpkg-dev \ file \ g++ \ gcc \ libc-dev \ make \ pkg-config \ re2c ࠷௿ݶͷ΋ͷ͔͠ೖͬͯͳ͍ ܰྔ൛ΠϝʔδΛϕʔεʹ࢖͏ ʮphp-*ʯͰ࢝·ΔύοέʔδΛ aptͰΠϯετʔϧ͠ͳ͍ઃఆ PHP֦ுϞδϡʔϧͷϏϧυʹ࢖༻ ͢Δphpize͕ґଘ͢ΔύοέʔδΛ వΊͯࢦఆ͍ͯ͠Δ
  14. # persistent / runtime deps RUN set -eux; \ apt-get

    update; \ apt-get install -y --no-install-recommends \ $PHPIZE_DEPS \ ca-certificates \ curl \ xz-utils \ ; \ rm -rf /var/lib/apt/lists/* ENV PHP_INI_DIR /usr/local/etc/php RUN set -eux; \ mkdir -p “$PHP_INI_DIR/conf.d”; \ [ ! -d /var/www/html ]; \ mkdir -p /var/www/html; \ chown www-data:www-data /var/www/html; \ chmod 777 /var/www/html લड़Ͱࢦఆͨ͠phpize༻ͷύοέʔδͱ PHPຊମͷΠϯετʔϧʹ࢖༻͢Δύοέʔδ /var/www/htmlσΟϨΫτϦͷॴ༗ऀ Λwww-dataʹઃఆ͠ɺapacheΛ࣮ߦ Մೳʹ͢Δɻ ※<version>-apacheܥͷΠϝʔδ͸͜ ͷޙʹapacheΛΠϯετʔϧ͢Δɻ iniϑΝΠϧͷஔ͖৔ॴΛ༻ҙ
  15. COPY docker-php-source /usr/local/bin/ RUN set -eux; \ \ savedAptMark=“$(apt-mark showmanual)“;

    \ apt-get update; \ apt-get install -y --no-install-recommends \ ${PHP_EXTRA_BUILD_DEPS:-} \ libargon2-dev \ libcurl4-openssl-dev \ libonig-dev \ libreadline-dev \ libsodium-dev \ libsqlite3-dev \ libssl-dev \ libxml2-dev \ zlib1g-dev \ PHPͷϏϧυʹ࢖͏ύοέʔδΛΠ ϯετʔϧͭͭ͠ɺʮapt-mark showmanualʯͰΠϯετʔϧͨ͠΋ ͷΛه࿥͍ͯ͠Δɻ PHPίʔυΛల։͢Δ γΣϧεΫϦϓτΛίϐʔ ※ݩͷεΫϦϓτ͸ϦϙδτϦʹ
  16. ./configure \ --build="$gnuArch" \ --with-config-file-path="$PHP_INI_DIR" \ --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \ \ #

    make sure invalid --configure-flags are fatal errors instead of just warnings --enable-option-checking=fatal \ \ # https://github.com/docker-library/php/issues/439 --with-mhash \ \ # https://github.com/docker-library/php/issues/822 --with-pic \ \ # --enable-ftp is included here because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236) --enable-ftp \ # --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195) --enable-mbstring \ ※ ௕͗͢ΔͷͰҰ෦ൈਮ PHPຊମʹؚ·Ε͍ͯΔʮconfigureʯͱ͍͏γΣϧεΫϦϓτΛ࣮ߦɻ Πϯετʔϧʹඞཁͳ΋ͷΛνΣοΫͭͭ͠ɺMakefileΛੜ੒
  17. make -j "$(nproc)"; \ find -type f -name '*.a' -delete;

    \ make install; \ find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; \ make clean; \ \ # https://github.com/docker-library/php/issues/692 (copy default example "php.ini" files somewhere easily discoverable) cp -v php.ini-* "$PHP_INI_DIR/"; \ \ cd /; \ docker-php-source delete; \ PHP͔ΒॳظͷiniϑΝΠϧʢphp.ini.production ͳͲʣΛίϐʔͭͭ͠ɺϏϧυΛऴ͑ͯෆཁ ʹͳͬͨPHPͷιʔείʔυΛফ͢ɻ લͷ޻ఔͰੜ੒ͨ͠makefileΛ࣮ߦͱ૟আ
  18. # reset apt-mark's "manual" list so that "purge --auto-remove" will

    remove all build dependencies apt-mark auto '.*' > /dev/null; \ [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \ find /usr/local -type f -executable -exec ldd '{}' ';' \ | awk '/=>/ { print $(NF-1) }' \ | sort -u \ | xargs -r dpkg-query --search \ | cut -d: -f1 \ | sort -u \ | xargs -r apt-mark manual \ ; \ apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \ rm -rf /var/lib/apt/lists/*; \ Ϗϧυʹ࢖͍ͬͯͨύοέʔδ͸͜ͷλΠϛϯάͰඞཁͳ͘ͳ ΔͷͰফ͢ɻʮapt-mark showmanualʯͰΠϯετʔϧͨ͠΋ͷΛ ه࿥͍ͯͨ͠ͷ͸ɺ͜ͷ࣌ͷͨΊɻ
  19. %PDLFSpMFΛಡΉࡍͷࢀߟϦϯΫ • PHP֦ுϞδϡʔϧͷҰཡ • https://www.php.net/manual/ja/extensions.alphabetical.php • PECL֦ுϞδϡʔϧΛPHPʹ૊ΈࠐΉ • https://www.php.net/manual/ja/install.pecl.static.php •

    PhpizeͰPECL֦ுϞδϡʔϧΛಡΈࠐΉ • https://www.php.net/manual/ja/install.pecl.phpize.php • ୊ࡐͱͨ͠Dockerfileʢdebian-bullseyeʣ • https://github.com/docker-library/php/blob/8d98db4cebcdf5ff776e2d0b9bf4a5b482f4e657/8.0/ bullseye/cli/Dockerfile