8
4

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.

GCR(Google Container Registry)にDockerイメージをpushする

Last updated at Posted at 2019-10-31

前提

なにがしかのDockerイメージを作成済みであること。

手順

1. Google Cloud SDKをダウンロード

コチラからSDKをダウンロード→解凍。

tar -xzvf Downloads/google-cloud-sdk-245.0.0-darwin-x86_64.tar.gz -C $HOME
x google-cloud-sdk/
x google-cloud-sdk/properties
x google-cloud-sdk/lib/
...
(果てしなく続くので以下省略)

>ls -la ~/
total 999
drwxr-xr-x+ 38 username  staff   1216 10 31 16:09 .
drwxr-xr-x   5 root          admin    160 10 20 10:45 ..
...
drwxr-xr-x@ 21 username  staff    672  5  4 04:53 google-cloud-sdk

2. インストール

❯ ./google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)? 

ぐーぐる先生の支援を求められるのでyesかnoかお好きな回答を。

(続き)
Your current Cloud SDK version is: 245.0.0
The latest available version is: 269.0.0

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   Components                                                  │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤
│      Status      │                         Name                         │            ID            │   Size   │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤
│ Update Available │ BigQuery Command Line Tool                           │ bq                       │  < 1 MiB │
│ Update Available │ Cloud SDK Core Libraries                             │ core                     │ 12.2 MiB │
│ Update Available │ Cloud Storage Command Line Tool                      │ gsutil                   │  3.6 MiB │
│ Not Installed    │ App Engine Go Extensions                             │ app-engine-go            │  4.8 MiB │
│ Not Installed    │ Cloud Bigtable Command Line Tool                     │ cbt                      │  7.3 MiB │
│ Not Installed    │ Cloud Bigtable Emulator                              │ bigtable                 │  6.6 MiB │
│ Not Installed    │ Cloud Datalab Command Line Tool                      │ datalab                  │  < 1 MiB │
│ Not Installed    │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │ 18.4 MiB │
│ Not Installed    │ Cloud Firestore Emulator                             │ cloud-firestore-emulator │ 37.2 MiB │
│ Not Installed    │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │ 34.9 MiB │
│ Not Installed    │ Cloud SQL Proxy                                      │ cloud_sql_proxy          │  3.7 MiB │
│ Not Installed    │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │ 14.5 MiB │
│ Not Installed    │ Google Cloud Build Local Builder                     │ cloud-build-local        │  5.9 MiB │
│ Not Installed    │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │  1.8 MiB │
│ Not Installed    │ gcloud Alpha Commands                                │ alpha                    │  < 1 MiB │
│ Not Installed    │ gcloud Beta Commands                                 │ beta                     │  < 1 MiB │
│ Not Installed    │ gcloud app Java Extensions                           │ app-engine-java          │ 85.9 MiB │
│ Not Installed    │ gcloud app PHP Extensions                            │ app-engine-php           │ 21.9 MiB │
│ Not Installed    │ gcloud app Python Extensions                         │ app-engine-python        │  6.0 MiB │
│ Not Installed    │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras │ 28.5 MiB │
│ Not Installed    │ kubectl                                              │ kubectl                  │  < 1 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘
To install or remove components at your current SDK version [245.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [269.0.0], run:
  $ gcloud components update



To take a quick anonymous survey, run:
  $ gcloud alpha survey


Modify profile to update your $PATH and enable shell command 
completion?

Do you want to continue (Y/n)?

gcloud を使うにはシェルの設定を読み込まなければならないがこのまま続ける?と聞かれる。
このプロセスを終了して新しいプロセスを開く場合はno、プロセスを維持する場合はyes。

(続き)
The Google Cloud SDK installer will now prompt you to update an rc 
file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use 
[/Users/ko-kamenashi/.bash_profile]:  
Backing up [/Users/ko-kamenashi/.bash_profile] to [/Users/ko-kamenashi/.bash_profile.backup].
[/Users/ko-kamenashi/.bash_profile] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts



~ took 2m2s 
❯ source .bash_profile # プロセスを維持した場合は `rc` を手動で再読み込み

はい、インストールできたか確認しましょう。

~ 
❯ gcloud version
Google Cloud SDK 245.0.0
bq 2.0.43
core 2019.05.03
gsutil 4.38

OK牧場です。

2. 認証

GCRにpushするには予め認証を通しておく必要があります。ヘルパーを使って認証しちゃいましょう。

❯ gcloud components install docker-credential-gcr


Your current Cloud SDK version is: 245.0.0
Installing components from version: 245.0.0

┌──────────────────────────────────────────────────────────────────────────┐
│                   These components will be installed.                    │
├──────────────────────────────────────────────────────┬─────────┬─────────┤
│                         Name                         │ Version │   Size  │
├──────────────────────────────────────────────────────┼─────────┼─────────┤
│ Google Container Registry's Docker credential helper │         │         │
│ Google Container Registry's Docker credential helper │   1.5.0 │ 1.8 MiB │
└──────────────────────────────────────────────────────┴─────────┴─────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  Y

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Container Registry's Docker creden... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Container Registry's Docker creden... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.                                                                                                                                                                                                                        

Update done!


~ 
❯ gcloud auth configure-docker
The following settings will be added to your Docker config file 
located at [/Users/ko-kamenashi/.docker/config.json]:
 {
  "credHelpers": {
    "gcr.io": "gcloud", 
    "us.gcr.io": "gcloud", 
    "eu.gcr.io": "gcloud", 
    "asia.gcr.io": "gcloud", 
    "staging-k8s.gcr.io": "gcloud", 
    "marketplace.gcr.io": "gcloud"
  }
}

Do you want to continue (Y/n)?  Y

Docker configuration file updated.

~ 
❯ gcloud auth configure-docker
The following settings will be added to your Docker config file 
located at [/Users/ko-kamenashi/.docker/config.json]:
 {
  "credHelpers": {
    "gcr.io": "gcloud", 
    "us.gcr.io": "gcloud", 
    "eu.gcr.io": "gcloud", 
    "asia.gcr.io": "gcloud", 
    "staging-k8s.gcr.io": "gcloud", 
    "marketplace.gcr.io": "gcloud"
  }
}

Do you want to continue (Y/n)?  Y

Docker configuration file updated.

~ 
❯ gcloud auth login
Your browser has been opened to visit: # ここで自動的にWebブラウザが開くので画面から操作する

    https://accounts.google.com/o/oauth2/auth?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&prompt=select_account&response_type=code&client_id=32555940559.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&access_type=offline


WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
  gcloud auth application-default --help

You are now logged in as [ko-kamenashi@dac.co.jp].
Your current project is [None].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

~ 
❯ 

はい、認証できました。
あとついでにpush先のGCPプロジェクトIDも設定しておきましょう。

❯ gcloud config set project [PROJECT_ID]
Updated property [core/project].

~ 
❯

ようやく準備OKとなりました。

3. push用のDockerイメージを作成

ここからDockerの操作です。用意しておいたイメージからGCRへpushするためのイメージを作成する必要があります。
イメージ名に「リージョン」と「プロジェクトID」を付記します。

リージョンは、

  • gcr.io は米国でイメージをホスト(今後は場所が変更される可能性あり)
  • us.gcr.io は米国でイメージをホスト(その場所は、gcr.io によってホストされるイメージからは独立したストレージ バケット)
  • eu.gcr.io は、欧州連合でイメージをホスト
  • asia.gcr.io は、アジアでイメージをホスト

の4つですが、まぁアジアでよいでしょう。以下のようなコマンドとなります。

❯ docker image tag 作成済みのイメージ名:バージョン asia.gcr.io/GCPプロジェクトID/任意のイメージ名

~ 
❯

4. push

いよいよpushです!

❯ docker image push asia.gcr.io/GCPプロジェクトID/さっき作ったイメージ名
The push refers to repository [asia.gcr.io/PROJECT_ID/IMAGE_NAME]
abc123cde456: Pushed 
...
latest: digest: sha256:abcdefg12345467abcdefg12345467... size: 1574

~ 
❯

Success!!

5. 確認

GCPコンソールで確認しましょう。

スクリーンショット 2019-10-31 17.56.31.png

無事にpushされてます。イエイ。

以上

ありがとうございました。

参照URL

公式ドキュメントの通りに進めていけば無問題です。

8
4
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
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?