14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IBM Cloud の Cloud Foundry 上で Docker Container を動かすために Container Registry を合わせて使ってみた

Last updated at Posted at 2019-03-27

事前準備

IBM Cloud CLI をインストールした後、以下のコマンド実行で Container Registry 用のプラグインをインストールします。

ibmcloud plugin install container-registry

Container Registry で自分のネームスペースを作成

CLI でログインします

$ ibmcloud login

API endpoint: https://cloud.ibm.com
Authenticating...
OK

Targeted account IBM (xxxxxxx) <-> xxxxxxx

Targeted resource group Default

Select a region (or press enter to skip):
1. au-syd
2. jp-tok
3. eu-de
4. eu-gb
5. us-south
6. us-east
Enter a number> 2
Targeted region jp-tok
               
API endpoint:      https://cloud.ibm.com   
Region:            jp-tok   
User:              xxxxx@example.com   
Account:           XXX (xxxxxxx) <-> xxxxxxx   
Resource group:    Default   
CF API endpoint:      
Org:                  
Space:                

Tip: If you are managing Cloud Foundry applications and services
- Use 'ibmcloud target --cf' to target Cloud Foundry org/space interactively, or use 'ibmcloud target --cf-api ENDPOINT -o ORG -s SPACE' to target the org/space.
- Use 'ibmcloud cf' if you want to run the Cloud Foundry CLI with current IBM Cloud CLI context.

Container Registry のリージョンを東京に設定します。

$ ibmcloud cr region-set ap-north

The region is set to 'ap-north', the registry is 'jp.icr.io'.

OK

Container Registry で自分のネームスペースを作成します。

$ ibmcloud cr namespace-add khayama

Adding namespace 'khayama'...

Successfully added namespace 'khayama'

OK

ネームスペースが作成されたことは、以下の UI 画面 ( https://cloud.ibm.com/kubernetes/registry/main/namespaces ) からも確認できます。
Kobito.KWpfxF.png

テストコンテナイメージを Container Registry にプッシュ

コマンドを実行しているマシンで Docker のデーモンを稼働させた状態で、Container Registry にログインします。

$ ibmcloud cr login

Logging in to 'jp.icr.io'...
Logged in to 'jp.icr.io'.

IBM Cloud Container Registry is adopting new icr.io domain names to align with the rebranding of IBM Cloud for a better user experience. The existing bluemix.net domain names are deprecated, but you can continue to use them for the time being, as an unsupported date will be announced later. For more information about registry domain names, see https://cloud.ibm.com/docs/services/Registry?topic=registry-registry_overview#registry_regions_local

OK

Docker デーモンが稼働していない場合のエラー

Logging in to 'jp.icr.io'...
FAILED
Failed to 'docker login' to 'jp.icr.io' with error: WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Cloud Foundry で動かすためのテストコンテナイメージをダウンロードします。

$ docker pull cloudfoundry/test-app

Using default tag: latest
latest: Pulling from cloudfoundry/test-app
a3ed95caeb02: Pull complete 
300273678d06: Pull complete 
bb84e9e1b665: Pull complete 
Digest: sha256:6d2e265936c77c8955c253dde084ee0f8ec25a78e76b6631222f5ef0e9047b72
Status: Downloaded newer image for cloudfoundry/test-app:latest

イメージにタグ付けをし直します。

$ docker tag cloudfoundry/test-app jp.icr.io/khayama/cf-test-app:v1

IBM Cloud の Container Registry 東京リージョンにアップロードします。

$ docker push jp.icr.io/khayama/cf-test-app:v1

The push refers to repository [jp.icr.io/khayama/cf-test-app]
0147abe5feb0: Pushed 
5f70bf18a086: Pushed 
5dbcf0efe4f2: Pushed 
v1: digest: sha256:c32f35d771bee7f0ccfea0dac66572014e7bf636ed5c0b0a34bc335eb21ffe5f size: 1362

Container Registry 東京リージョンのイメージリスト内にあることを確認します。

$ ibmcloud cr image-list

Listing images...

REPOSITORY                      TAG   DIGEST         NAMESPACE   CREATED        SIZE     SECURITY STATUS   
jp.icr.io/khayama/cf-test-app   v1    c32f35d771be   khayama     2 years ago    8.1 MB   No Issues   
jp.icr.io/khayama/hello-world   v1    92c7f9c92844   khayama     2 months ago   977 B    No Issues   

OK

イメージがアップロードされたことは、以下の UI 画面 ( https://cloud.ibm.com/kubernetes/registry/main/private ) からも確認できます。
Kobito.63WNub.png

Container Registry アクセス認証用の IAM API key を発行

以下のコマンドで作成し、出力された json 内に apikey が保存されます。

$ ibmcloud iam api-key-create khayama-cr -d "for container registry access" --file crKey.json

Creating API key khayama-cr as xxxxx@example.com...
OK
API key khayama-cr was created
Successfully save API key information to crKey.json

以下のコマンドで、発行された apikey を変数として格納します。(あとで使います)

export $( echo $(cat crKey.json) | jq -r 'keys[] as $k | "export \($k)=\(.[$k])"')
export CF_DOCKER_PASSWORD=$apikey

IBM Cloud の Cloud Foundry 上で Docker Container を動かせるかの確認

最寄りの Cloud Foundry リージョンをターゲットに指定します。

$ ibmcloud target -r au-syd

Switched to region au-syd

API endpoint:      https://cloud.ibm.com   
Region:            au-syd   
User:              xxxxx@example.com   
Account:           XXXXX (xxxxx) <-> xxxxx   
Resource group:    Default   
CF API endpoint:      
Org:                  
Space:                

Tip: If you are managing Cloud Foundry applications and services
- Use 'ibmcloud target --cf' to target Cloud Foundry org/space interactively, or use 'ibmcloud target --cf-api ENDPOINT -o ORG -s SPACE' to target the org/space.
- Use 'ibmcloud cf' if you want to run the Cloud Foundry CLI with current IBM Cloud CLI context.
$ ibmcloud target --cf

Targeted Cloud Foundry (https://api.au-syd.bluemix.net)

Targeted org xxxxx

Targeted space dev

API endpoint:      https://cloud.ibm.com   
Region:            au-syd   
User:              xxxxx@example.com   
Account:           xxxxx (xxxxx) <-> xxxxx   
Resource group:    Default   
CF API endpoint:   https://api.au-syd.bluemix.net (API version: 2.106.0)   
Org:               XXXXX   
Space:             dev   

Cloud Foundry 上で Docker Container を動かす機能が有効化されていることを確認します。
diego_docker=enabled
これで「Cloud Foundry で Docker を動かす」こと自体は、問題ない(きっとサポートされている)はず...!
(ドキュメントにも Cloud Foundry で Docker を動かすコマンドが掲載されているので...)

$ ibmcloud cf feature-flags

Invoking 'cf feature-flags'...

Retrieving status of all flagged features as xxxxx@example.com...

features                               state
user_org_creation                      disabled
private_domain_creation                enabled
app_bits_upload                        enabled
app_scaling                            enabled
route_creation                         enabled
service_instance_creation              enabled
diego_docker                           enabled
set_roles_by_username                  enabled
unset_roles_by_username                enabled
task_creation                          enabled
env_var_visibility                     enabled
space_scoped_private_broker_creation   enabled
space_developer_env_var_visibility     enabled
service_instance_sharing               disabled

Container Registry から Cloud Foundry にデプロイ

以下のコマンド実行で、すぐに立ち上がります。
起動に 10 秒もかかっていないイメージです。
Container Registry の認証がうまく動くかがポイントです。

$ ibmcloud cf push khayama-cf-test-app --docker-image jp.icr.io/khayama/cf-test-app:v1 --docker-username iamapikey

Invoking 'cf push khayama-cf-test-app --docker-image jp.icr.io/khayama/cf-test-app:v1 --docker-username iamapikey'...

Using docker repository password from environment variable CF_DOCKER_PASSWORD.
Pushing app khayama-cf-test-app to org xxxxx / space dev as xxxxx@example.com...
Getting app info...
Creating app with these attributes...
+ name:              khayama-cf-test-app
+ docker image:      jp.icr.io/khayama/cf-test-app:v1
+ docker username:   iamapikey
  routes:
+   khayama-cf-test-app.au-syd.mybluemix.net

Creating app khayama-cf-test-app...
Mapping routes...

Staging app and tracing logs...
   Cell c2557d8d-aa23-4d21-be92-3baf62299ad2 creating container for instance f8effe27-b4fc-403e-9180-28348ac9f3b6
   Cell c2557d8d-aa23-4d21-be92-3baf62299ad2 successfully created container for instance f8effe27-b4fc-403e-9180-28348ac9f3b6
   Staging...
   Staging process started ...
   Staging process finished
   Exit status 0
   Staging Complete
   Cell c2557d8d-aa23-4d21-be92-3baf62299ad2 stopping instance f8effe27-b4fc-403e-9180-28348ac9f3b6
   Cell c2557d8d-aa23-4d21-be92-3baf62299ad2 destroying container for instance f8effe27-b4fc-403e-9180-28348ac9f3b6
   Cell c2557d8d-aa23-4d21-be92-3baf62299ad2 successfully destroyed container for instance f8effe27-b4fc-403e-9180-28348ac9f3b6

Waiting for app to start...

name:              khayama-cf-test-app
requested state:   started
routes:            khayama-cf-test-app.au-syd.mybluemix.net
last uploaded:     Thu 28 Mar 00:49:59 JST 2019
stack:             
docker image:      jp.icr.io/khayama/cf-test-app:v1

type:            web
instances:       1/1
memory usage:    1024M
start command:   /test-app 
     state     since                  cpu    memory        disk          details
# 0   running   2019-03-27T15:50:16Z   1.1%   12.2M of 1G   22.4M of 1G   

Web 公開されているテストアプリが確認できました!
Kobito.GsNpnW.png

感想

今回は、IAM API key を発行して、Container Registry へのアクセス認証を自動化するところがポイントでした。
Web公開用のコンテナイメージも多くあるので、ぱっと公開したい場合は、これが一番お手軽な方法なのかもしれませんね。

参考

14
10
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
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?