3
3

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 1 year has passed since last update.

Google Cloud Runを使ってみる

Last updated at Posted at 2022-11-10

Cloud Runを使ってみます。

全体の流れ

おおよその流れは以下の通り。

  • ローカルでイメージをビルド
  • GCP(artifact registry)にイメージをpush
  • Cloud Runで実行

前提

  • mac
  • dockder, gcloudインストール済み
  • Google Cloudにプロジェクト(hoge-dev-test)作成済み

準備

ログイン

まずはGoogle Cloudにログイン。

gcloud auth login

資格証明ヘルパー?の登録(不要かも)

gcloudとdockerが連携できるよう認証(登録)する。

gcloud auth configure-docker asia-northeast1-docker.pkg.dev

上記のコマンドを実行すると/User/hoge/.docker/config.jsonに、credHelpersとして登録される。

artifact registryの作成

以前はImageのpush先としてはContainer Resistryが一般的だったけど、今はArtifact Registryが登場してそっちが推奨となってるみたいなのでそちらを利用してみます。

gcloud artifacts repositories create hogerepo --location=asia-northeast1 --repository-format=docker

Artifact Registryにはイメージのチェックとかの機能もあるみたい。
久しぶりに上記を実行したらバーミションエラー。Artifact Registry管理者権限を追加したらOKでした。。。。

上記作業はGoogle CloudのコンソールからGUIで行うことも可能です。

ローカルでイメージをビルド

普通にnginxのイメージのまんまでもいいのですが、簡単にカスタムビルドしてみます。

作業場の作成

cd 
mkdir cloudrun-test
cd cloudrun-test

touch Dockerfile default.conf index.html

Dockerfile

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/
COPY ./index.html /var/www/html/

default.conf

default.conf
server {
    listen 80;
    charset UTF-8;

    location / {
        root /var/www/html;
        index index.html;
    }
}

index.html

index.html
Hello Cloud Run!

イメージをビルドとテスト(ローカル)

docker build -t my_ngnix:v1 .
docker run --rm --name my_www -p 8888:80 -d my_nginx:v1

Cloud Run用のイメージのビルドとpush

build

docker build -t asia-northeast1-docker.pkg.dev/hoge-dev-test/hogerepo/my_ngnix:v1 .

push

docker push asia-northeast1-docker.pkg.dev/hoge-dev-test/hogerepo/my_ngnix:v1

Cloud Runの設定と実行

ここからは基本GUI。

サービスの生成

Cloud Runのページにアクセスし、「サービスの作成」をクリック。

Kobito.biWPoq.png

イメージの選択

コンテナイメージのURLの選択を押し、先程pushしたイメージを選択。

Kobito.irNbrt.png

必要な設定を行う

認証の有無

未認証の呼び出しを許可を選択。

Kobito.WUN2c5.png

ポートの設定

nginxのリッスンポートとして指定したポート番号を設定。

Kobito.y2D0fh.png

作成(デプロイ)

作成をクリックすると10秒くらいでデプロイが完了します。

Kobito.4k66nT.png

動作確認

URLにアクセス。

Kobito.zG4c8a.png

メモ

  • http/https, gRPC以外でのアクセス(例えばDBとか)は無理っぽい。。。
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?