無料で使うDocker PaaS 〜Heroku〜

Herokuで無料でDockerイメージを動かしてみます。

とはいっても、Herokuの場合安心設計で、勝手に課金されることはなさそうです。
はじめ無料で、必要に応じて課金するスタンス。
実際、ドメイン名の変更したりしなければ、クレジットカードすらもいらないです。
ただ大きな制約として、一日最低6時間sleepする、ということです。
起動に時間がかかるアプリケーションや、頻繁にアクセスがあるようなサービスは向かないですね。

事前にherokuコマンドのインストール、heroku loginは済ましておいてください。

まずHeroku上にアプリケーションを作成します。

$ heroku apps:create uphy-test
Creating ⬢ uphy-test... done
https://uphy-test.herokuapp.com/ | https://git.heroku.com/uphy-test.git

試しに以下の内容のnginxのDockerfileを作成。

FROM nginx:latest
CMD sed -e "/ listen / s/80/$PORT/" -i /etc/nginx/conf.d/default.conf && \
    nginx -g "daemon off;"

sedの理由 nginxのイメージそのままでは動かない。
HerokuはHerokuの指定したポートでしかサービスを動かせないらしい。
その指定したポートというのは、環境変数PORTで渡されます。
そこでnginxの設定ファイルを書き換えて、$PORTで待ち受けるようにしましょう。
こんなやり方でいいのか・・・

gitリポジトリ初期化。

$ git init
$ heroku git:remote -a uphy-test
set git remote heroku to https://git.heroku.com/uphy-test.git

ちなみに、heroku git:remoteは、herokuのgitリポジトリをgit remote addしてくれるみたいね。楽ちん!
準備ができたので公開します。

$ heroku container:push web
=== Building for web  (/Users/uphy/Documents/DockerPaaS比較/heroku/app/Dockerfile)
Sending build context to Docker daemon  44.03kB
Step 1/1 : FROM nginx:latest
 ---> cc1b61406712
Successfully built cc1b61406712
Successfully tagged registry.heroku.com/uphy-test/web:latest
=== Pushing for web  (/Users/uphy/Documents/DockerPaaS比較/heroku/app/Dockerfile)
The push refers to a repository [registry.heroku.com/uphy-test/web
7d530616ebc2: Pushed
db07381cb585: Pushed
a2ae92ffcd29: Pushed
latest: digest: sha256:d3d260288da84e03e16f7e735c88f9da6406d3be6ce0f2e7c084ee513b722204 size: 948

確認。

$ curl https://uphy-test.herokuapp.com/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
- 略 -
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

docker imageのpushがかなり重いので、できたらHeroku上でビルドしてそのまま公開したい。要調査。