>  기사  >  백엔드 개발  >  다단계 빌드가 포함된 간단한 Golang Dockerfile(이미지 크기 감소)

다단계 빌드가 포함된 간단한 Golang Dockerfile(이미지 크기 감소)

Barbara Streisand
Barbara Streisand원래의
2024-10-01 11:12:30332검색

이 docker 파일을 사용하면 golang docker 이미지가 ~1GB에서 ~40MB로 줄어들고 이미지를 더 빠르게 다시 빌드할 수 있습니다

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.