Google Container RegistryにHello, WorldなDockerイメージを置いてみる
Google Container Registryを初めて触ってみます。
Dockerコンテナイメージ作成
以下のような適当なDockerfile
を作成します。
FROM ubuntu:20.04 RUN echo "Hello, World!" > /hello.txt
コンテナイメージをビルドします。Dockerfile
のあるディレクトリで以下のコマンドでビルドできます。
$ docker build -t hello-world .
作成したコンテナイメージは、docker images
コマンドで見えるようになります。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 21deb0595efa 4 minutes ago 72.9MB
試しにコンテナイメージを実行してみます。
$ docker run -it --rm hello-world bash root@3b8150776095:/# cat /hello.txt Hello, World!
Google Container Registryにpush
Google Container Registryにpushするために、タグを付けます。新しいタグの名前は gcr.io/PROJECT_ID/NAME というフォーマットです。PROJECT_IDはGoogle Cloud PlatformのプロジェクトID、NAMEはコンテナイメージの名前です。
$ docker tag hello-world gcr.io/xxxxxxxx/hello-world
docker images
コマンドで確認できます。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE gcr.io/xxxxxxxx/hello-world latest 21deb0595efa 6 minutes ago 72.9MB hello-world latest 21deb0595efa 6 minutes ago 72.9MB
このコンテナイメージをGoogle Container Registryにアップロードします。
$ gcloud docker -- push gcr.io/xxxxxxxx/hello-world
$ gcloud container images list NAME gcr.io/xxxxxxxx/hello-world
Google Container Registryからpull
gcloudコマンドを同じプロジェクトが使えるように設定してあれば、別の環境からもコンテナイメージにアクセスできることが確認できます。
$ gcloud docker -- run -it --rm gcr.io/xxxxxxxx/hello-world bash -c "cat /hello.txt" Hello, World!
追記 2021/04/22
AWSでも同じことをしました。