はじめに
この記事はコンテナ勉強用として試したことまとめたものです。
今回はECRにイメージをプッシュを試してみました。
ECRとは
- EC2 Container Registoryの略
- AWSのマネージドなレジストリサービスである。
- Dockerのイメージ格納場所である。コンテナ定義のイメージを置く。
- リポジトリに保存するデータとインターネットに転送するデータ量に対して課金が発生。
- 公式ドキュメント
https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/what-is-ecr.html
サンプルアプリをクローン
-
以下リンクにPHPアプリが提供されているのでクローンして使用する。
https://github.com/aws-samples/ecs-demo-php-simple-app -
git cloneコマンド実行する。
$ git clone https://github.com/aws-samples/ecs-demo-php-simple-app.git
Cloning into 'ecs-demo-php-simple-app'...
remote: Enumerating objects: 75, done.
remote: Total 75 (delta 0), reused 0 (delta 0), pack-reused 75
Unpacking objects: 100% (75/75), done.
$
- ディレクトリ移動する。
$ ll
total 4
drwxrwxr-x 4 ec2-user ec2-user 4096 Aug 5 12:04 ecs-demo-php-simple-app
$ cd ecs-demo-php-simple-app/
Dockerイメージビルド
- dockerビルドを実行する。(docker buildコマンドでイメージを作成することが出来る)
$ sudo docker build -t amazon-ecs-test .
Sending build context to Docker daemon 370.7kB
Step 1/10 : FROM amazonlinux:2
2: Pulling from library/amazonlinux
72d97abdfae3: Pull complete
Digest: sha256:730fae68c6a180e8006443b0f090e56419da5ceb5d11250d96e627660ffbc674
Status: Downloaded newer image for amazonlinux:2
---> b94321659aca
Step 2/10 : RUN yum install -y curl httpd php && ln -s /usr/sbin/httpd /usr/sbin/apache2
---> Running in 623634f22d4e
Loaded plugins: ovl, priorities
Resolving Dependencies
--> Running transaction check
---> Package curl.x86_64 0:7.61.1-9.amzn2.0.1 will be updated
---> Package curl.x86_64 0:7.61.1-11.amzn2.0.2 will be an update
--> Processing Dependency: libcurl(x86-64) = 7.61.1-11.amzn2.0.2 for package: curl-7.61.1-11.amzn2.0.2.x86_64
---> Package httpd.x86_64 0:2.4.39-1.amzn2.0.1 will be installed
--> Processing Dependency: httpd-tools = 2.4.39-1.amzn2.0.1 for package: httpd-2.4.39-1.amzn2.0.1.x86_64
--> Processing Dependency: httpd-filesystem = 2.4.39-1.amzn2.0.1 for package: httpd-2.4.39-1.amzn2.0.1.x86_64
・・・(省略)・・・
Step 10/10 : CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
---> Running in 5e922dc9f8ae
Removing intermediate container 5e922dc9f8ae
---> f90240a94062
Successfully built f90240a94062
Successfully tagged amazon-ecs-test:latest
$
- Dcoker作成・起動する。
$ sudo docker run -d -p 80:80 --name amazon-ecs-test amazon-ecs-test:latest
5bc7308354dde742d3ae9c05e406193f4d3e33b3242b1e308496a8eb54d04863
$
- 接続確認する。
$ curl http://<IPアドレス>:80/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Simple PHP App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<style>body {margin-top: 40px; background-color: #333;}</style>
<link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet">
<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<div class="container">
<div class="hero-unit">
<h1>Simple PHP App</h1>
<h2>Congratulations</h2>
<p>Your PHP application is now running on a container in Amazon ECS.</p>
<p>The container is running PHP version 5.4.16.</p>
$
ECRリポジトリにDockerイメージPush
- 作成したイメージをECRにPushするため、コンソールからリポジトリを作成する。
(作成が完了するとPushコマンドが表示できます。)
- アップロード用のコマンドを確認する。
- ECRログインコマンドを実行する。
$ sudo $(aws ecr get-login --no-include-email --region ap-northeast-1)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$
- イメージにタグをつける。
$ sudo docker tag amazon-ecs-test:latest 111111111111.dkr.ecr.ap-northeast-1.amazonaws.com/amazon-ecs-test:latest
$
- プッシュコマンドを実行する。
$ sudo docker push 111111111111.dkr.ecr.ap-northeast-1.amazonaws.com/amazon-ecs-test:latest
The push refers to repository [111111111111.dkr.ecr.ap-northeast-1.amazonaws.com/amazon-ecs-test]
f26e4cb2e5cd: Pushed
99445024df25: Pushed
24ba07c56401: Pushed
f387c8b346c8: Pushed
latest: digest: sha256:a5baf5fa02aad35e4b732022d98e1ba02809933a2645e495f4ee0047f84eeae8 size: 1160
$
まとめ
- Dockerの扱いに苦労するのでまずはDockerに慣れていくことが必要...
- 次はECSでアプリケーションを配置してみたい!