Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8913

GCPのすゝめ【Cloud Run編】

$
0
0

概要

Cloud Runを使う際の手順とまとめです。

公式ドキュメント
Cloud Run
Cloud Build

サンプルレポジトリー
https://github.com/koffe0522/cloudrun-express

準備

Docker for MAC
Docker for Windows
Google Cloud SDK

Google Cloud Platform での設定

※既存のprojectプロジェクトを使用する場合は以下コマンドのみ

$ gcloud config set project <PROJECT_ID>

projectの作成

projectの作成方法は、以下の2種類
- Cloud Consoleから作成
- gcloudコマンドライン ツールで作成

公式ドキュメント
プロジェクトの作成と管理

ディレクトリー構成

.├── .env
├── .dockerignore
├── Dockerfile
├── README.md
├── cloud-build.yml
├── docker-compose.yml
├── index.js
├── package.json
└── yarn.lock

アプリケーションの作成

※nodeプロジェクトが存在している場合は不要

$ mkdir<project name>
$ cd<project name>
$ yarn init -y$ yarn add express
# 開発用$ yarn add -D nodemon
index.js
constexpress=require('express');constapp=express();app.get('/',(req,res)=>{res.send(`Hello World!`);});constport=process.env.PORT||8080;app.listen(port,()=>{console.log('Hello world listening on port',port);});

以下を追加

package.json
{..."scripts":{"start":"node index.js","dev":"PORT=3000 nodemon index.js"},...}

ローカル開発環境設定

docker-compose.yml
version:"3.6"services:app:image:node:10env_file:.envtty:trueports:-"3000:3000"volumes:-.:/appworking_dir:/appcommand:yarn run dev

起動してみる

$ docker-compose up -d

http://localhost:3000/

停止する

$ docker-compose down

Buildの準備

Dockerfile
FROM node:10WORKDIR /srcCOPY package*.json ./RUN yarn install\
--prefer-offline\
--frozen-lockfile\
--non-interractive\
--production=falseCOPY . .CMD ["yarn", "run", "start"]
.dockerignore
node_modules
npm-debug.log
cloud-build.yml
steps:-name:gcr.io/cloud-builders/dockerargs:["build","-f","Dockerfile","-t","gcr.io/<PROJECT_ID>/<imagename好きな名前>",# ①".",]images:# ①と同じ-gcr.io/<PROJECT_ID>/<image name 好きな名前>

Cloud Build & Deploy

Cloud Buildの実行

$ gcloud builds submit --project<PROJECT_ID> --config=cloud-build.yml

...
 Would you like to enable and retry (this will take a few minutes)? 
(y/N)?  y
$ gcloud beta run deploy <cloud-run-name> --region us-central1 --project<PROJECT_ID> --image<cloud-build.ymlのimages名> --platform managed

...
you like to enable and retry (this will take a few minutes)? (y/N)?  y
...
Allow unauthenticated invocations to [cloud-run-name] (y/N)?  y

Deploying container to Cloud Run service [cloud-run-name] in project [PROJECT_ID] region [us-central1]
✓ Deploying new service... Done.                                                                                                                                       
  ✓ Creating Revision... Deploying Revision.                                                                                                                           
  ✓ Routing traffic...                                                                                                                                                 
  ✓ Setting IAM Policy...                                                                                                                                              
Done.                                                                                                                                                                  
Service [cloud-run-name] revision [cloud-run-name-00000-xxx] has been deployed and is serving 100 percent of traffic at <URL> Routing traffic... 

表示されたURLにアクセスする

備考

  • Cloud Run では8080ポートでないとエラーとなる

Viewing all articles
Browse latest Browse all 8913

Trending Articles