多段階ビルドを備えたシンプルな Golang Dockerfile (イメージ サイズの ~ を削減)

Barbara Streisand
リリース: 2024-10-01 11:12:30
オリジナル
330 人が閲覧しました

この docker ファイルを使用すると、golang docker イメージを ~1 GB から ~40 MB に削減し、イメージをより速く再構築できます

Simple Golang Dockerfile with multi staged builds (reduces ~ of the image size)

# Stage 1: Build the Go application
FROM golang:1.23-alpine AS builder

# Set the working directory inside the container
WORKDIR /app

# Copy the Go module files
COPY go.mod go.sum ./

# Download Go module dependencies
RUN go mod download

# Copy the rest of the application code
COPY . .

# Build the Go application
RUN go build -o main cmd/server/main.go

# Stage 2: Minimal image for running the app
FROM alpine:latest as runner

# Set environment variables (optional)
ENV GO_ENV=production

# Create a directory for the application
WORKDIR /app

# Copy the binary from the builder stage
COPY --from=builder /app/main .

# Expose the port on which the application runs (if applicable)
EXPOSE 8080

# Command to run the application
CMD ["./main"]
ログイン後にコピー

以上が多段階ビルドを備えたシンプルな Golang Dockerfile (イメージ サイズの ~ を削減)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!