LoginSignup
2
5

More than 1 year has passed since last update.

Amazon Linux 2のコンテナイメージをDocke上で動かす

Last updated at Posted at 2020-04-28

はじめに

Amazon Linux AMIのサポート期間終了が2020年12月31日までと発表されました。今後はAmazon Linux 2での運用が必要となってきます。そこで、dockerhubで配布されているAmazon Linux 2のコンテナイメージを使って検証環境を構築してみます。

参考情報

Dockerfile & コンテナイメージ 成果物

作成したDockerfileとコンテナイメージはそれぞれ以下にあります。

試した環境

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.4
BuildVersion:	19E287
$ docker --version
Docker version 19.03.8, build afacb8b

作成したDockerfileのビルドやコンテナ実行はMac OS上で行いました。
dockerhubのコンテナイメージの説明にこのように書かれているので、他の環境でも動作すると思います。

Where can I run Amazon Linux container images?

You can run Amazon Linux container images in any Docker based environment. Examples include, your laptop, in AWS EC2 instances, and ECS clusters.

Amazon Linux 2 on Docker にインストールするアプリケーション

Dockerfile内の環境変数でバージョン指定をしています。
Dockerfileを作成した時点の最新版を指定しています。コンテナを作って壊してを繰り返す使い方でなければコンテナ実行後にrbenvやpyenvを実行して必要なバージョンをインストールすることもできます。

  • xpanes:4.1.1
  • rbenv
  • Ruby:2.7.1
  • pyenv
  • Python:3.8.2
  • tfenv
  • Terraform:0.12.24
  • Ansible:2.9.7
  • aws-cli:v2

Dockerfileは https://github.com/revsystem/Dockerfiles/tree/master/AmazonLinux2 に置いています。

Dockerイメージのビルドとコンテナ実行

Dockerfileを手元でビルドする場合。

Dockerイメージのビルド

git clone https://github.com/revsystem/Dockerfiles.git
cd Dockerfiles/AmazonLinux2
docker image build -t amzn2-awscliv2 .

コンテナ実行

docker container run -it amzn2-awscliv2 /bin/bash --login

ローカル環境のディレクトリをコンテナ上にマッピングする

~/.ssh~/.awsをコンテナにマッピングする場合。

docker container run -it -v ~/.aws:/home/ec2-user/.aws -v ~/.ssh:/home/ec2-user/.ssh -v $(pwd):/home/ec2-user/aws amzn2-awscliv2 /bin/bash --login

dockerhub上のイメージを使ってコンテナ実行

手元でビルドするとPythonやRubyのビルドに時間がかかるため、そのまま使うだけであればこちらのほうが手軽です。

docker container run -it revsystem/amzn2 /bin/bash --login

Linuxユーザーとsudo

コンテナを実行すると ec2-user というユーザでログインします。 root 権限が必要な場合は sudo コマンドが使えます。ec2-userのパスワードはec2-userです。

[ec2-user@59716cc68639 ~]$ sudo su -

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for ec2-user: 
-bash-4.2# 
2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5