首页 > 后端开发 > Golang > 正文

具有多阶段构建的简单 Golang Dockerfile(减少了图像大小)

Barbara Streisand
发布: 2024-10-01 11:12:30
原创
332 人浏览过

使用此 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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!