Maison  >  Article  >  développement back-end  >  Dockerfile Golang simple avec des constructions en plusieurs étapes (réduit ~ de la taille de l'image)

Dockerfile Golang simple avec des constructions en plusieurs étapes (réduit ~ de la taille de l'image)

Barbara Streisand
Barbara Streisandoriginal
2024-10-01 11:12:30332parcourir

En utilisant ce fichier Docker, vous réduirez votre image Docker Golang de ~1 Go à ~40 Mo et vous reconstruirez votre image plus rapidement

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"]

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Espresso; c'est l'heure d'y allerArticle suivant:Aucun