使用Cloud Registry - gcr.io來儲存您的docker image

Google提供每一個Cloud專案一個專屬的gcr.io registry作為docker image的存放地,透過gcr.io可以無限存放您的image,並且有提供簡單的查詢介面供使用者檢視所儲存的image與相關tag… 近一步使用將可透過與其他服務的串連,達到更多樣化的應用~

從使用流程了解gcr.io

在使用前,我們先來檢視gcr.io的使用流程

Step 1 - 幫你的image製作tag

docker tag [your-image] gcr.io/[your-project-id]/[your-image-name]

Step 2 - Push image到gcr.io

gcloud docker - push gcr.io/[your-project-id]/[your-image-name]

下面實際使用一個Dockerfile來作為範例,存放到gcr.io上...

首先,透過node.js的express套件來建立專案... [參考: https://expressjs.com/en/starter/installing.html]

$ mkdir myproject && cd myproject && express -e web
 warning: option `--ejs' has been renamed to `--view=ejs'
  create : web
  create : web/package.json
  create : web/app.js
  create : web/public
  create : web/public/javascripts
│ │ ├── destroy@1.0.4
  create : web/public/images
  create : web/public/stylesheets
  create : web/public/stylesheets/style.css
  create : web/routes
  create : web/routes/index.js
  create : web/routes/users.js
  create : web/views
  create : web/views/index.ejs
  create : web/views/error.ejs
  create : web/bin
  create : web/bin/www

  install dependencies:
    $ cd web && npm install

  run the app:
    $ DEBUG=web:* npm start

接下來我們在myproject下面建立Dockerfile檔案

From node

ADD web /app
WORKDIR /app
RUN npm install
CMD ["npm","start"]

然後開始build這個Dockerfile,過程中docker會將Dockerfile中每一行建置成image中的一個layer,如果一切沒問題,則會在系統中新增一個”myweb”的image...

$ docker build -t myweb .
Sending build context to Docker daemon  16.9 kB
Step 1 : FROM node
latest: Pulling from library/node
ad74af05f5a2: Pull complete
2b032b8bbe8b: Pull complete
a9a5b35f6ead: Pull complete
49fb60496f63: Pull complete
3894583bc464: Pull complete
321f9e1d7334: Pull complete
cdf9ffbab3ff: Pull complete
a76a49ec92c8: Pull complete
Digest: sha256:0adc410aa5e45b817bc8f29d5845c4941ad03b63e9584bc7f8654562e60424cf
Status: Downloaded newer image for node:latest
---> 60bea5b86079
Step 2 : ADD web /app
---> c2db2b5f5fd9
Removing intermediate container 287af85ca8f5
Step 3 : WORKDIR /app
---> Running in 415faf475d84
---> 0ff5af512bc8
Removing intermediate container 415faf475d84
Step 4 : RUN npm install
---> Running in af9958080502
npm info it worked if it ends with ok
npm info using npm@5.3.0
...
npm info lifecycle undefined~postshrinkwrap: undefined
added 63 packages in 14.524s
npm info ok
---> b002146466c3
Removing intermediate container af9958080502
Step 5 : CMD npm start
---> Running in ebb282a74539
---> 19057465c4c6
Removing intermediate container ebb282a74539
Successfully built 19057465c4c6

我們可以透過”docker images”來查詢新的image是否建置完成...

假設一切無誤,接著,我們可以透過上面所說的兩個步驟來將image存放至gcr.io…

$ docker tag myweb gcr.io/mitac-simonsu-2017/myweb
$ gcloud docker -- push gcr.io/mitac-simonsu-2017/myweb
The push refers to a repository [gcr.io/mitac-simonsu-2017/myweb]
5953068074de: Pushing [==================================================>] 4.235 MB
4a296490096d: Pushed
…(skip)
2c40c66f7667: Pushed
latest: digest: sha256:394c8ef825ad3a1aa...39b27ae61e8cd38d500cf size: 2425

當push完成,我們可以檢視一下目前image的狀況...

首先確認一下是否image已經上傳...

image list 指令

gcloud container images list

執行範例

$ gcloud container images list
NAME
gcr.io/mitac-simonsu-2017/demo
gcr.io/mitac-simonsu-2017/demoweb
gcr.io/mitac-simonsu-2017/jenkins-slave
gcr.io/mitac-simonsu-2017/mytest
gcr.io/mitac-simonsu-2017/myweb
gcr.io/mitac-simonsu-2017/peihsinsu
gcr.io/mitac-simonsu-2017/web2
Only listing images in gcr.io/mitac-simonsu-2017. Use --repository to list images in other repositories.

如果上傳無誤,則可以透過list-tags來檢視該image下是否有其他tag存在

list-tags指令

gloud container images list-tags gcr.io/[project-id]/[image-name]

執行範例

$ gcloud container images list-tags gcr.io/mitac-simonsu-2017/myweb
DIGEST        TAGS    TIMESTAMP
394c8ef825ad  latest  2017-09-03T19:43:57

要使用該image的話,如果是GKE環境,則可以直接使用,如果是透過docker或外部環境,則可以透過gcloud來認證docker,讓docker具備gcr.io的存取權限。

docker login -u oauth2accesstoken \
 -p "$(gcloud auth application-default print-access-token)" \
 https://gcr.io


參考

使用gcloud認證docker指令: https://cloud.google.com/container-registry/docs/advanced-authentication

這個網誌中的熱門文章

Bash判斷參數是否存在

使用 minikube 輕鬆上手 kubernetes