AmazonLinux2のDocker Imageにnginxをインストールする

ベースイメージの選択

最終目標として、AWS ECS(以下ECS)でRailsアプリケーションを動作させたいと思っています。ECSで利用するイメージのベースはなにがよいかわかっていないので、とりあえずAmazonLinux2を利用します。一旦テストのためにローカルでDockerfileを作成してみます。

AmazonLinux2のイメージはdockerhubにありました。
https://hub.docker.com/_/amazonlinux

tagはlatest2とすれば大丈夫のようです。

nginxのインストール

Dockerfileを記述していきます

1
2
3
FROM amazonlinux:latest
RUN yum update -y
RUN yum install -y nginx

docker image buildを実行します

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ docker image build -f Dockerfile -t al2_nginx:latest .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM amazonlinux:latest
---> 7f335821efb5
Step 2/3 : RUN yum update -y
---> Running in 1d974faaaefa
Loaded plugins: ovl, priorities
Resolving Dependencies
--> Running transaction check
---> Package glib2.x86_64 0:2.56.1-4.amzn2 will be updated
(中略)
FROM amazonlinux:latest

Complete!
Removing intermediate container 1d974faaaefa
---> b36e0bbfc4cf
Step 3/3 : RUN yum install -y nginx
---> Running in f244f466cb1b
Loaded plugins: ovl, priorities
No package nginx available.
Error: Nothing to do
The command '/bin/sh -c yum install -y nginx' returned a non-zero code: 1

nginxのパッケージがないようなので調べてみると、 amazon-linux-extras install nginx1.12でインストールが可能とのこと。Dockerfileを編集します。

1
2
3
FROM amazonlinux:latest
RUN yum update -y
RUN amazon-linux-extras install nginx1.12

そして、再度ビルドします

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ docker image build -f Dockerfile -t al2_nginx:latest .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM amazonlinux:latest
---> 7f335821efb5
Step 2/3 : RUN yum update -y
---> Using cache
---> b36e0bbfc4cf
Step 3/3 : RUN amazon-linux-extras install nginx1.12
---> Running in de67234984e9
NOTE: The livepatch extra is in public preview, not meant for production use

Topic nginx1.12 has end-of-support date of 2019-09-20
Loaded plugins: ovl, priorities
Cleaning repos: amzn2-core amzn2extra-nginx1.12
(中略)
* Extra topic has reached end of support.
Removing intermediate container de67234984e9
---> 97bc3952d1a8
Successfully built 97bc3952d1a8
Successfully tagged al2_nginx:latest

依存関係によって82ものパッケージがインストールされますが、無事nginxはインストールできたようです。

ただ気になるメッセージがありました。

1
2
3
NOTE: The livepatch extra is in public preview, not meant for production use

Topic nginx1.12 has end-of-support date of 2019-09-20

livepatch extraというのは本番用ではないということと、nginx1.12のサポートがすでにを終わっているということでした。

nginxのバージョン変更

nginxのバージョンをあげるにはどういう風に指定すればいいのかを調べてみると、以下のページが見つかりました。

https://dev.classmethod.jp/articles/al2-nginx1-new-extras-topic/

記事に書いてある内容ですと、nginx1と指定するとよさそうです。
再度Dockerfileを更新します。

1
2
3
FROM amazonlinux:latest
RUN yum update -y
RUN amazon-linux-extras install nginx1

再度buildします。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ docker image build -f Dockerfile -t al2_nginx:latest .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM amazonlinux:latest
---> 7f335821efb5
Step 2/3 : RUN yum update -y
---> Using cache
---> b36e0bbfc4cf
Step 3/3 : RUN amazon-linux-extras install nginx1
---> Running in a7280e4e4c82
NOTE: The livepatch extra is in public preview, not meant for production use

Loaded plugins: ovl, priorities
Cleaning repos: amzn2-core amzn2extra-nginx1
6 metadata files removed
2 sqlite files removed
0 metadata files removed
Loaded plugins: ovl, priorities
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.amzn2.0.1 will be installed

今度は1.16.1がインストールできました(2020年6月現在)

まとめ

  • AmazonLinux2のDockerImageはDockerhubから取得できる。
  • AmazonLinux2にnginxをインストールする場合は、amazon-linux-extrasコマンドを利用する
  • トピックをnginx1とすることで1系の最新バージョンがインストールできる
  • extrasリポジトリで配布されるパッケージは、トピック変更が発生するため、定期的なOSパッチ更新などのタイミングで利用中のトピックのサポート状況を確認する。Dockerfileのメンテナンスを行う。

次にRubyのインストールとRailsアプリケーションのデプロイを行いたいと思います。