利用docker自动编译golang项目
目录
如何利用docker自动编译golang项目?利用docker自动编译golang可执行文件,以便快速部署拉起。
利用Dockerfile创建自定义的golang-docker镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Dockerfile References: https://docs.docker.com/engine/reference/builder/ # Start from the latest golang base image FROM golang:latest # Add Maintainer Info LABEL maintainer="cppla <i@cpp.la>" # Set the Current Working Directory inside the container WORKDIR /app # Copy go mod and sum files COPY go.mod go.sum ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . # Build the Go app RUN go build -o main . # Expose port 8080 to the outside world EXPOSE 8080 # Command to run the executable CMD ["./main"] |
一键构建并测试运行golang-docker
1 2 3 4 5 6 |
# 构建docker images docker build -t go-docker . # 查看images docker image ls # 运行容器 docker run -d -p 8080:8080 go-docker |
利用Dockerfile创建带日志的golang-docker镜像
该操作会持久化golang-docker 产生的日志数据到物理机器硬盘上,适合生产环境中做数据落地
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Start from the latest golang base image FROM golang:latest # Add Maintainer Info LABEL maintainer="cppla <i@cpp.la>" # Set the Current Working Directory inside the container WORKDIR /app # Build Args ARG LOG_DIR=/app/logs # Create Log Directory RUN mkdir -p ${LOG_DIR} # Environment Variables ENV LOG_FILE_LOCATION=${LOG_DIR}/app.log # Copy go mod and sum files COPY go.mod go.sum ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . # Build the Go app RUN go build -o main . # This container exposes port 8080 to the outside world EXPOSE 8080 # Declare volumes to mount VOLUME [${LOG_DIR}] # Run the binary program produced by `go install` CMD ["./main"] |
【重点】利用Dockerfile多阶段创建优化的golang-docker镜像
上述dockerfile打包的golang-docker镜像非常大,体积高达约800M,非常不便于我们做集群弹性拉起。然而我们仅仅需要编译产生golang项目的二进制可执行文件放在alpine容器上运行即可,即:无需保留任何golang环境,Dockerfile只负责产生二进制文件容器运行(体积大小仅仅为数十兆)。
1 2 3 4 5 |
root@aws3:/# docker images REPOSITORY TAG IMAGE ID CREATED SIZE iptools latest cb2e34531b72 3 months ago 21.8MB alpine latest 055936d39205 4 months ago 5.53MB golang latest 7ced090ee82e 4 months ago 774MB |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# Start from the latest golang base image FROM golang:latest as builder # Add Maintainer Info LABEL maintainer="xxx <i@cpp.la>" # Set the Current Working Directory inside the container WORKDIR /app # Copy go mod and sum files COPY go.mod go.sum ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . # Build the Go app RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . ######## Start a new stage from scratch ####### FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ # Copy the Pre-built binary file from the previous stage COPY --from=builder /app/main . # Expose port 8080 to the outside world EXPOSE 8080 # Command to run the executable CMD ["./main"] |
附录一:工程中实际用的go-Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# onekey docker for golang project, by: cpp.la FROM golang:latest as builder MAINTAINER cppla <i@cpp.la> ADD dsp $GOPATH/src/dsp WORKDIR $GOPATH/src/dsp RUN go get -d -v ./... RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/dsp . FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root COPY --from=builder /go/bin/dsp . EXPOSE 8080 CMD ["./dsp"] |
附录二:工程中实际用的go-Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# golang builder for iptools, test: curl 3.0.3.0 FROM golang:latest as builder MAINTAINER cppla <i@cpp.la> ADD iptools $GOPATH/src/iptools WORKDIR $GOPATH/src/iptools RUN go get -d -v ./... RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/iptools . # binary run FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root COPY --from=builder /go/bin/iptools . COPY --from=builder /go/src/iptools/static ./static COPY --from=builder /go/src/iptools/templates ./templates EXPOSE 8080 CMD ["./iptools"] |
Refer: www.callicoder.com/docker-golang-image-container-example.
Note: 20190925.
Author: https://cpp.la