Rails+Webスクレイピング+DockerのアプリをEC2で立てようとしたら、デフォルト設定だとt2.small(2GB)とかが必要になりそう

※情弱なのでデフォルトのメモリ設定をしてしまってますが、もしかしたらDockerのメモリ割り当てをうまくやればもっと小さいサイズでいけるかもしれません。

# 概要
dockerでWebサイト（Rails + Webスクレイピング）をAWSのEC2で立てたいと思っていたんですが、自分しか使わないような遊びサイトだったので、できるだけ安い金額で立てられたらと思っていたんですが、見事に撃沈した話です。

# 結論
デフォルト設定(Docker、Rails)だとt2.smallを使わないと、`bundle install`の`nokogiri`を入れるときにメモリエラーになります。

※[この記事](https://qiita.com/fkooo/items/bb921218313123c14358)のようにDockerを動かすだけならt2.nanoでも動きます。
※t2.smallならスクレイピング([docker-selenium](https://github.com/SeleniumHQ/docker-selenium))も動きました

# 詳細
## どんな環境？
Docker: 19.03.6-ce
docker-compose: 1.25.5
ruby:2.5.3 (Dockerhubの`ruby:2.5.3`のイメージを使用)
Rails: 5.2.3

AWSにDockerを入れるのには[こちら](https://qiita.com/yumatsud/items/33bc22f7d8f640a286f4)を参考にさせてもらいました。

## どんなことが起きたの？
t2.nanoとt2.microのdocker環境内で`bundle install`をしたら以下エラーが起きました。
（どうでもいいですが、ローカル環境で動いていたDockerfileで`bundle install`したときに以下エラーが起き、まだイメージが出来上がる前だったので、わざわざDockerfileの`bundle install`やdocker-compose.ymlのコマンドから`bundle install`やそれに依存した`rails s`をコメントアウトしてdocker-compose.ymlで`tail -f Gemfile`をしてコンテナ延命した状態で以下のように独立して`bundle install`を実行しました。docker-composeに慣れて`docker run`が打てない人間の悲しい性です）

```sh
# bundle install
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from http://rubygems.org/............
Fetching rake 12.3.3
Installing rake 12.3.3
Fetching concurrent-ruby 1.1.5
Installing concurrent-ruby 1.1.5
Fetching i18n 1.6.0
Installing i18n 1.6.0
Fetching minitest 5.11.3
Installing minitest 5.11.3
Fetching thread_safe 0.3.6
Installing thread_safe 0.3.6
Fetching tzinfo 1.2.5
Installing tzinfo 1.2.5
Fetching activesupport 5.2.3
Installing activesupport 5.2.3
Fetching builder 3.2.3
Installing builder 3.2.3
Fetching erubi 1.8.0
Installing erubi 1.8.0
Using mini_portile2 2.4.0
Fetching nokogiri 1.10.3
Installing nokogiri 1.10.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /usr/local/bundle/gems/nokogiri-1.10.3/ext/nokogiri
/usr/local/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.5.0 -r
./siteconf20200523-12-t3a56p.rb extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
Using mini_portile version 2.4.0
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.9
with the following patches applied:
        - 0001-Revert-Do-not-URI-escape-in-server-side-includes.patch
        - 0002-Remove-script-macro-support.patch
        - 0003-Update-entities-to-remove-handling-of-ssi.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.9.tar.gz into
tmp/x86_64-pc-linux-gnu/ports/libxml2/2.9.9... OK
Running git apply with
/usr/local/bundle/gems/nokogiri-1.10.3/patches/libxml2/0001-Revert-Do-not-URI-escape-in-server-side-includes.patch...
OK
Running git apply with
/usr/local/bundle/gems/nokogiri-1.10.3/patches/libxml2/0002-Remove-script-macro-support.patch...
OK
Running git apply with
/usr/local/bundle/gems/nokogiri-1.10.3/patches/libxml2/0003-Update-entities-to-remove-handling-of-ssi.patch...
OK
Running 'configure' for libxml2 2.9.9... OK
Running 'compile' for libxml2 2.9.9... OK
Running 'install' for libxml2 2.9.9... OK
Activating libxml2 2.9.9 (from
/usr/local/bundle/gems/nokogiri-1.10.3/ports/x86_64-pc-linux-gnu/libxml2/2.9.9)...
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxslt-1.1.33
with the following patches applied:
        - 0001-Fix-security-framework-bypass.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install
************************************************************************
Extracting libxslt-1.1.33.tar.gz into
tmp/x86_64-pc-linux-gnu/ports/libxslt/1.1.33... OK
Running git apply with
/usr/local/bundle/gems/nokogiri-1.10.3/patches/libxslt/0001-Fix-security-framework-bypass.patch...
OK
Running 'configure' for libxslt 1.1.33... OK
Running 'compile' for libxslt 1.1.33... OK
Running 'install' for libxslt 1.1.33... OK
Activating libxslt 1.1.33 (from
/usr/local/bundle/gems/nokogiri-1.10.3/ports/x86_64-pc-linux-gnu/libxslt/1.1.33)...
checking for -llzma... yes
checking for xmlParseDoc() in libxml/parser.h... yes
checking for xsltParseStylesheetDoc() in libxslt/xslt.h... yes
checking for exsltFuncRegister() in libexslt/exslt.h... yes
checking for xmlHasFeature()... yes
checking for xmlFirstElementChild()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetParserStructuredErrors()... yes
checking for xmlRelaxNGSetValidStructuredErrors()... yes
checking for xmlSchemaSetValidStructuredErrors()... yes
checking for xmlSchemaSetParserStructuredErrors()... yes
creating Makefile

current directory: /usr/local/bundle/gems/nokogiri-1.10.3/ext/nokogiri
make "DESTDIR=" clean
current directory: /usr/local/bundle/gems/nokogiri-1.10.3/ext/nokogiri
make "DESTDIR="
make failedCannot allocate memory - make "DESTDIR="

Gem files will remain installed in /usr/local/bundle/gems/nokogiri-1.10.3
for inspection.
Results logged to
/usr/local/bundle/extensions/x86_64-linux/2.5.0/nokogiri-1.10.3/gem_make.out

An error occurred while installing nokogiri (1.10.3), and Bundler
cannot continue.
Make sure that `gem install nokogiri -v '1.10.3' --source
'http://rubygems.org/'` succeeds before bundling.

In Gemfile:
  rails was resolved to 5.2.3, which depends on
    actioncable was resolved to 5.2.3, which depends on
      actionpack was resolved to 5.2.3, which depends on
        actionview was resolved to 5.2.3, which depends on
          rails-dom-testing was resolved to 2.0.3, which depends on
            nokogiri
```

長すぎて何がエラーかわからないので、見ろと言われたログを見てみます

```
# cat /usr/local/bundle/extensions/x86_64-linux/2.5.0/nokogiri-1.10.3/gem_make.out
current directory: /usr/local/bundle/gems/nokogiri-1.10.3/ext/nokogiri
/usr/local/bin/ruby -I /usr/local/lib/ruby/site_ruby/2.5.0 -r ./siteconf20200523-53-7krva6.rb extconf.rb
extconf failedCannot allocate memory - /usr/local/bin/ruby
```
ここでメモリ不足だとわかりましたorz
そりゃ500MBでdockerだもんな

# まとめ
月2000円は痛いんですが、仕方ないかぁといった感じです。
誰か同じ状況の人やAWSに関しての意思決定で同じ問題意識を持った人の一助になれば嬉しいです。