Home  >  Article  >  Backend Development  >  Simple Golang Dockerfile with multi staged builds (reduces ~ of the image size)

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-01 11:12:30332browse

Using this docker file you will reduce your golang docker image from ~1GB to ~40MB and you will rebuild your image faster

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

The above is the detailed content of Simple Golang Dockerfile with multi staged builds (reduces ~ of the image size). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Espresso; it&#s Go timeNext article:None