---
title: Cloud Native Buildpacks Tutorial - 5.6. ┗ heroku/buildpacks:18 BuilderでPHPアプリのOCIイメージを作成
tags: ["Cloud Native Buildpacks Tutorial", "Cloud Native Buildpacks", "Heroku", "Series"]
categories: ["Dev", "Infrastructure", "CloudNativeBuildpacks", "Heroku"]
date: 2020-04-28T10:40:45Z
updated: 2020-05-11T03:35:12Z
---

`heroku/buildpacks:18`でPHPのアプリケーションのOCIイメージを作成します。

BuilderとStackを予めpullしておいてください。

```
docker pull heroku/buildpacks:18
docker pull heroku/pack:18
```

次のコマンドで"Hello World"アプリケーションを作成します。

```
mkdir hello-php
cd hello-php
cat <<'EOF' > index.php
<?php
echo 'Hello World!';
EOF

cat <<'EOF' > Procfile
web: vendor/bin/heroku-php-nginx
EOF
```

`pack build`コマンドでイメージを作成します。

```
pack build making/pack-php --no-pull --builder heroku/buildpacks:18
```

> BuilderとStackを事前にpullしていない場合は`--no-pull`を外せば、ビルド時にBuilderとStackもpullしますが、ビルドが遅くなります。

次のようなログが出力されます。

```
===> DETECTING
[detector] heroku/php      0.1.2
[detector] heroku/procfile 0.5
===> ANALYZING
[analyzer] Warning: Image "index.docker.io/making/pack-php:latest" not found
===> RESTORING
===> BUILDING
[builder] 
[builder]  !     WARNING: No 'composer.json' found!
[builder]  !     
[builder]  !     Your project only contains an 'index.php', no 'composer.json'.
[builder]  !     
[builder]  !     Using 'index.php' to declare app type as PHP is deprecated and
[builder]  !     may lead to unexpected behavior.
[builder]  !     
[builder]  !     Please consider updating your codebase to utilize Composer and
[builder]  !     modern dependency management in order to benefit from the latest
[builder]  !     PHP runtimes and improved application performance, as well as
[builder]  !     control over the PHP versions and extensions available.
[builder]  !     
[builder]  !     For an introduction to dependency management with Composer and
[builder]  !     how to get the most out of PHP on Heroku, refer to the docs at
[builder]  !     https://getcomposer.org/doc/00-intro.md and
[builder]  !     https://devcenter.heroku.com/articles/getting-started-with-php
[builder] 
[builder] -----> Bootstrapping...
[builder] -----> Installing platform packages...
[builder]        NOTICE: No runtime required in composer.lock; using PHP ^7.0.0
[builder]        - apache (2.4.41)
[builder]        - nginx (1.16.1)
[builder]        - php (7.4.4)
[builder] -----> Installing dependencies...
[builder]        Composer version 1.10.1 2020-03-13 20:34:27
[builder] -----> Preparing runtime environment...
[builder] -----> Checking for additional extensions to install...
[builder] -----> Discovering process types
[builder]        Procfile declares types     -> web
===> EXPORTING
[exporter] Adding layer 'launcher'
[exporter] Adding layer 'heroku/php:profile'
[exporter] Adding 1/1 app layer(s)
[exporter] Adding layer 'config'
[exporter] *** Images (30181cd95c4e):
[exporter]       index.docker.io/making/pack-php:latest
[exporter] Adding cache layer 'heroku/php:shim'
Successfully built image making/pack-php
```

> `pack build`時に`--publish`オプションをつけると、Docker Registryでのpushを行います。事前に`docker login`が必要です。

作成したイメージを`docker run`で起動します。

```
docker run --rm -e PORT=8080 -p 8080:8080 making/pack-php
```

アプリケーションにアクセスします。

```
$ curl localhost:8080 -w '\n'
Hello World!
```

Docker Imageのサイズを確認します。

```
$ docker images | grep making/pack-php
making/pack-php                      latest              30181cd95c4e        40 years ago        566MB
```

`pack inspect-image`でイメージを解析します。

```
$ pack inspect-image making/pack-php
Inspecting image: making/pack-php

REMOTE:
(not present)

LOCAL:

Stack: heroku-18

Base Image:
  Reference: 60f9e03398de7d6a5268be95224246f05eed9f1eecb1fe7605753261f2bd871a
  Top Layer: sha256:ceefbd5b67bc32a9d87867c8c8d375675246e9db9b470cb95133b4e93429b113

Run Images:
  heroku/pack:18

Buildpacks:
  ID                     VERSION
  heroku/php             0.1.2
  heroku/procfile        0.5

Processes:
  TYPE                 SHELL        COMMAND                            ARGS
  web (default)        bash         vendor/bin/heroku-php-nginx    
```

おわったらDocker Imageを削除します。

```
docker rmi making/pack-php
```
