Skip to content

Instantly share code, notes, and snippets.

@tsonglew
Created October 15, 2020 06:14
Show Gist options
  • Save tsonglew/3ff8a8b3741f233424d815300c923358 to your computer and use it in GitHub Desktop.
Save tsonglew/3ff8a8b3741f233424d815300c923358 to your computer and use it in GitHub Desktop.
Go 项目 Dockerfile 模板
FROM golang:alpine AS builder
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
GOPROXY=https://goproxy.cn
# Move to working directory /build
WORKDIR /build
# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download
# Copy the code into the container
COPY . .
# Build the application
RUN go build -o main .
# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist
# Copy binary from build to main folder
RUN cp /build/main .
# Build a small image
FROM alpine
RUN apk update
RUN apk add tzdata
ENV ZONEINFO /usr/share/timezone
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
RUN date
COPY --from=builder /dist/main /
EXPOSE 8000 8080
# Command to run
ENTRYPOINT ["/main"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment