LoginSignup
0
0

More than 1 year has passed since last update.

Docker の実験(その9:イメージを Container Registry に登録する)

Last updated at Posted at 2023-02-18

その8:Dockerfile のオプションのつづき。

コンテナ作成時にタグをつけることができる。

$ docker build -t demo:v1 .

もし、タグ付けを忘れた場合は、であとからタグをつけることができる。

$ docker tag <container id> demo:v1

また、タグでエイリアスを作成することができる。

ここで、Google Cloud 上の Docker vm (Container Optimized-os)で、まずは、タグ付けをする。
(参考)
https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling

$ docker tag ubuntu asia.gcr.io/<project-id>/ubuntu-image                                                                                                                            
$ docker images
REPOSITORY                                     TAG       IMAGE ID       CREATED       SIZE
<none>                                         <none>    5e443cc82858   4 hours ago   175MB
<none>                                         <none>    ede4075dc036   5 hours ago   175MB
nginx                                          latest    3f8a00f137a0   9 days ago    142MB
asia.gcr.io/<project-id>/ubuntu-image   latest    58db3edaf2be   3 weeks ago   77.8MB
ubuntu                                         latest    58db3edaf2be   3 weeks ago   77.8MB

では、このイメージを、Artifact Registry にタグ名を指定してプッシュする。

$ docker image push asia.gcr.io/<project-id>/ubuntu-image                                                                                                                      
Using default tag: latest
The push refers to repository [asia.gcr.io/<project-id>/ubuntu-image]
c5ff2d88f679: Preparing
unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication

権限がなくて、ダメなようだ。

(参考)
https://cloud.google.com/artifact-registry/docs/docker/troubleshoot
By default Compute Engine, Cloud Run, and Google Kubernetes Engine have read permissions for repositories in the same project. If you are using a third-party tool in these environments to push images, you must grant the Artifact Registry Writer role to the runtime service account.

当該インスタンスでは、以下の Service account が使われている。

<number>-compute@developer.gserviceaccount.com

いずれにしろ、gcloud コマンドが使えないとダメなようだ。

(参考)
https://cloud.google.com/sdk/docs/install#deb

ちなみに、パッケージをインストールする際に、toolbox というコマンドをかます必要がある。

(参考)
https://cloud.google.com/container-optimized-os/docs/how-to/toolbox

apt-get コマンドは、直接実行することができない。
一度、toolbox コマンドを投入する必要がある。

$ toolbox
root@docker-test:~# which gcloud
/usr/bin/gcloud

ちゃんと、gcloud コマンドがあった。。
今回は、toolbox コマンドは必要ない。

以下の手順を実行することが必要です。
(参考)
https://cloud.google.com/container-optimized-os/docs/how-to/run-container-instance
https://daddytrevia.com/gcp/Google_Container_Registry/ (こちらが該当する)

$ gcloud auth print-access-token
<文字列>
$ docker login -u oauth2accesstoken https://asia.gcr.io
Password:<上記 access-token key をペースト>
WARNING! Your password will be stored unencrypted in /home/<USERNAME>/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

では、Artifact registry にイメージをプッシュしてみる。

$ docker image push asia.gcr.io/<project-id>/ubuntu-image                                                                                                                      
Using default tag: latest
The push refers to repository [asia.gcr.io/<project-id>/ubuntu-image]
c5ff2d88f679: Layer already exists
latest: digest: sha256:41c1003bfccce22a81a49062ddb088ea6478eabea1457430e6235828298593e6 size: 529

成功している。
コンソールは、Container Registry を開くと、ubuntu-image Docker イメージが存在していた。

pull も実行可能である。

$ docker image pull asia.gcr.io/<project-id>/ubuntu-image

この場合、既にイメージが存在しているので、updagte された旨が表示されるが、docker rmi コマンドで削除しておけば、ダウンロードすることが見て取れる。

その10:コンテナのレイヤにつづく。

0
0
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
0
0