LoginSignup
2
3

More than 5 years have passed since last update.

IBM Containers のリポジトリにDockerイメージを登録する 

Last updated at Posted at 2016-12-15

概要

Bluemix 用 ibmnode イメージ入門 のドキュメントを読んだだけでは、出来なかったので、調べた事を含めて、カスタム・イメージのコンテナが起動するまでを確認した記録です。

コンテナ・イメージのリスト

IBM Container を作成する元になるイメージのリストは、以下のコマンドで見る事ができます。 最初のステップとして、このリストにカスタム・イメージを追加します。

root@ubuntu-xenial:~/ibmnode/hellonode# cf ic images
REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
registry.ng.bluemix.net/ibm_wa_agent          latest              42090290077d        11 weeks ago        418.8 MB
registry.ng.bluemix.net/ibm-backup-restore    latest              4b84e2510ebe        2 weeks ago         205.4 MB
registry.ng.bluemix.net/ibm-integration-bus   latest              d8b7c6d7c9bc        10 days ago         681 MB
registry.ng.bluemix.net/ibm-node-strong-pm    latest              e63d1d2a3244        2 weeks ago         264.1 MB
registry.ng.bluemix.net/ibmliberty            javaee7             190892c21ed5        12 weeks ago        313.5 MB
registry.ng.bluemix.net/ibmliberty            latest              190892c21ed5        12 weeks ago        313.5 MB
registry.ng.bluemix.net/ibmliberty            microProfile        3b74ce22b3ac        12 weeks ago        237.9 MB
registry.ng.bluemix.net/ibmliberty            webProfile6         2546c4b2a22f        12 weeks ago        266.9 MB
registry.ng.bluemix.net/ibmliberty            webProfile7         d06fb99cd165        12 weeks ago        276.2 MB
registry.ng.bluemix.net/ibmnode               latest              337f3f2a2e8f        2 weeks ago         197.1 MB
registry.ng.bluemix.net/ibmnode               v4                  337f3f2a2e8f        2 weeks ago         197.1 MB
registry.ng.bluemix.net/ibmnode               v1.1                e5776452ee8f        2 weeks ago         186.6 MB
registry.ng.bluemix.net/ibmnode               v1.2                8a5cfcc8251c        2 weeks ago         192.8 MB

カスタム・コンテナ・イメージの作り方

Bluemix 用 ibmnode イメージ入門 の記事で提供されているサンプルコードの hellonode.zip を利用してカスタムイメージを作ってみます。

事前準備

サンプルコードをダウンロードします。

root@ubuntu-xenial:~# wget ftp://public.dhe.ibm.com/cloud/bluemix/containers/hellonode.zip

次に、unzipで展開します。もしunzip が入っていなければ apt-get install unzip でインストールします。

root@ubuntu-xenial:~# unzip hellonode.zip 
Archive:  hellonode.zip
   creating: hellonode/app/
  inflating: hellonode/app/app.js    
  inflating: hellonode/Dockerfile   

hellonode.zip を展開すると以下の様なファイルとディレクトリが入っています。

root@ubuntu-xenial:~/ibmnode/hellonode# ls -al
total 16
drwxr-xr-x 3 root   root   4096 Dec 14 07:34 .
drwxrwxr-x 3 ubuntu ubuntu 4096 Dec 14 07:34 ..
drwxr-xr-x 3 root   root   4096 Dec 14 07:56 app
-rw-r--r-- 1 root   root    146 Jan 14  2016 Dockerfile

カスタム・イメージの作成

ここから、カスタムコンテナに、詰め込むファイルのセットを作っていきます。最初に、このNode.jsのアプリケーションに必要なファイルを確保するための初期化を行います。展開したフォルダの app に移動して、以下のコマンドを実行して、デフォルト値でも良いので Enter を押して進めていきます。

root@ubuntu-xenial:~/ibmnode/hellonode/app# npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (app) 
version: (1.0.0) 
description: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/ubuntu/ibmnode/hellonode/app/package.json:

{
  "name": "app",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"
  },
  "devDependencies": {},
  "description": ""
}


Is this ok? (yes) yes

次にNode.jsで開発された Webアプリを動作させるためのフレームワーク Express をインストールします。

root@ubuntu-xenial:~/ibmnode/hellonode/app# npm install express -save
app@1.0.0 /home/ubuntu/ibmnode/hellonode/app
└─┬ express@4.14.0 
  ├─┬ accepts@1.3.3 
  │ ├─┬ mime-types@2.1.13 
  │ │ └── mime-db@1.25.0 
  │ └── negotiator@0.6.1 
<以下省略>

この中で、node_modules というフォルダーに express や 依存関係のあるファイルが収められています。

root@ubuntu-xenial:~/ibmnode/hellonode/app# ls -al
total 20
drwxr-xr-x  3 root root 4096 Dec 15 01:03 .
drwxr-xr-x  3 root root 4096 Dec 14 07:34 ..
-rw-r--r--  1 root root  229 Jan 14  2016 app.js
drwxr-xr-x 45 root root 4096 Dec 15 01:03 node_modules
-rw-r--r--  1 root root  272 Dec 15 01:03 package.json

ここから、コンテナのイメージをビルドします。イメージの作成に docker コマンドを利用するので、ローカルの環境に、docker をインストールしておく必要があります。 Ubuntu であれば、以下のコマンドでインストールする事ができます。

# apt-get install docker.io

docker コマンドが動作する様になれば、早速、自分のPCの仮想マシン上で、Docker のコンテナイメージを作成します。 Dockerfile のある場所で、以下のコマンドを実行します。

root@ubuntu-xenial:~/ibmnode/hellonode# docker build -t mynode:node1 .
Sending build context to Docker daemon 1.264 MB
Step 1 : FROM registry.ng.bluemix.net/ibmnode
latest: Pulling from ibmnode
ba76e97bb96c: Already exists 
<中略>
 ---> ceb56b38b813
Removing intermediate container 388a4b7cd2be
Successfully built ceb56b38b813

Bluemix上の プライベート・リポジトリへ登録

プラペートのリポジトリ名に必要なキーワードを次のコマンドで取得します。

root@ubuntu-xenial:~/ibmnode# cf ic namespace get
takara_node

取得したnamespaceのキーワードを使って、プライペートのリポジトリ名のタグを付与します。mynode:node1 は、ビルドしたイメージです。そして、registry.ng.bluemix.net/takara_node がプライペートリポジトリの名前です。 そして、mynode は、格納するコンテナイメージの名前です。

root@ubuntu-xenial:~/ibmnode/hellonode# docker tag mynode:node1 registry.ng.bluemix.net/takara_node/mynode

次に、タグをつけたイメージを Bluemix のプラペートリポジトリに転送(push)します。

root@ubuntu-xenial:~/ibmnode/hellonode# docker push registry.ng.bluemix.net/takara_node/mynode
The push refers to a repository [registry.ng.bluemix.net/takara_node/mynode]
dac89f73e68d: Pushed 
<中略>
b257bb4b4aea: Pushed 
latest: digest: sha256:8b28d9c2d757011272919ee3acb52747f6a220271a878c67f7549396572f3f65 size: 2621

本当にプライベート・リポジトリに格納されたのか確認します。 以下の様にtakara_nodeに相当するキーワードを含むエントリーがあれば登録完了です。

root@ubuntu-xenial:~/ibmnode/hellonode# cf ic images
REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
<中略>
registry.ng.bluemix.net/takara_node/mynode    latest              ceb56b38b813        7 minutes ago       197.4 MB

Bluemix 上で コンテナを起動

ここからの動作は、すべてBluemix のポータル画面からでも同じ操作ができるのですが、せっかくなので、コマンドラインで進めていきます。 後は普通の IBM Container の起動方法と同じですが、自分の忘備録を兼ねているので、記録しておきます。 このポイントは、-p 3000 の部分で Dockerfileに記載したPORT番号以外のものが指定できない制約があります。 また、-p を付与しなければ、外部ネットワークからアクセスできません。

root@ubuntu-xenial:~# cf ic run -p 3000 --name mynode1 registry.ng.bluemix.net/takara_node/mynode:latest
77a95056-3f21-4de1-8f32-75a3a03714c1

起動後の確認は、ps オプションを利用します。 ps にオプションをつけなければ動作中のコンテナだけが表示され、-a を付けると 停止中のコンテナも表示されます。

root@ubuntu-xenial:~# cf ic ps
CONTAINER ID        IMAGE                                               COMMAND             CREATED             STATUS                 PORTS               NAMES
77a95056-3f2        registry.ng.bluemix.net/takara_node/mynode:latest   ""                  10 seconds ago      Running a second ago   3000/tcp            mynode1

このままでは、外部ネットワークからアクセスできないので、パブリックIPアドレスを取得して、コンテナへバインドします。

root@ubuntu-xenial:~# cf ic ip request
OK
The IP address "169.46.21.24" was obtained.

上で取得した パブリックIPアドレスをバインドします。

root@ubuntu-xenial:~# cf ic ip bind 169.46.21.24 mynode1
OK
The IP address was bound successfully.

これで外部から、コンテナのアプリを呼び出る様になりました。

imac:~ maho$ curl http://169.46.21.24:3000
Hello World!

参考資料

2
3
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
3