ホームページ  >  記事  >  バックエンド開発  >  多段階ビルドを備えたシンプルな Golang Dockerfile (イメージ サイズの ~ を削減)

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

Barbara Streisand
Barbara Streisandオリジナル
2024-10-01 11:12:30332ブラウズ

この 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 サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:エスプレッソ;さあ出発です次の記事:なし