Skip to content

Instantly share code, notes, and snippets.

@utamori
Last active July 20, 2020 07:13
Show Gist options
  • Select an option

  • Save utamori/ef4cc71090b608d3c93433c47f1b9642 to your computer and use it in GitHub Desktop.

Select an option

Save utamori/ef4cc71090b608d3c93433c47f1b9642 to your computer and use it in GitHub Desktop.
CloudRunのサンプル
# ビルドアーティファクトを作成するために、公式のGolangイメージを使用する
# これは Debian をベースにしており、GOPATH を/goに設定します。
# https://hub.docker.com/_/golang
FROM golang:1.13 as builder
# appディレクトリを作成して移動します。
WORKDIR /app
# go modulesを使用してアプリケーションの依存関係を取得します。
# コンテナのビルドでダウンロードした依存関係を再利用できるようにします。
COPY go.* ./
RUN go mod download
# ローカルコードをコンテナイメージにコピーします。
COPY . ./
# バイナリをビルドします
# -mod=readonlyはコンテナビルドでの go.mod と go.sum の不変性を保証します。
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# 無駄のないプロダクション用コンテナには、alpineの公式画像を使用してください。
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates
# builder段階からproductionイメージにバイナリをコピーします。
COPY --from=builder /app/server /server
# コンテナ起動時にWebサービスを実行します。
CMD ["/server"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment