見出し画像

Cloud Run × HTML × Nginx デプロイ



概要

静的ページをDockerでNginx環境を構築し、Cloud Runを使用してサーバーレスな構成でデプロイする手順をまとめました。


手順

次のようなディレクトリ構造で作っていきます。

% tree -L 2
.
├── Dockerfile
├── dist
│   ├── asset # css, js とかあれば
│   └── index.html
└── nginx.conf

nginx.confの作成

Cloud RunのデフォルトのPORTが8080なのでそれに合わせます。

>nginx.conf

user nginx;

worker_processes auto;

pid /var/run/nginx.pid;

events {
  worker_connections 2048;
  multi_accept on;

  use epoll;
}

http {
  charset UTF-8;
  server_tokens off;
  include /etc/nginx/mime.types;
  default_type text/plain;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  server {
    listen 8080;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.html;
    charset utf-8;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
  }
}

Dockerfileの作成

>Dockerfile

FROM nginx:latest
COPY ./dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]


Artifact registryにイメージを提出

gcpにログイン

gcloud auth login

レポジトリ作成

gcloud artifacts repositories create $(DIR_NAME) --repository-format=docker --location=asia-northeast1

Dockerfileがあるディレクトリから次のコマンドを実行します

gcloud builds submit --region=asia-east1 --tag asia-east1-docker.pkg.dev/$(PROJECT_NAME)/$(DIR_NAME)/$(IMAGE_NAME):latest

Cloud Runにデプロイ

gcloud run deploy $(SERVICE_NAME) --image asia-east1-docker.pkg.dev/$(PROJECT_NAME)/$(DIR_NAME)/$(IMAGE_NAME):latest --region asia-east1 --platform managed --allow-unauthenticated


この記事が気に入ったらサポートをしてみませんか?