docker打包golang应用

一、错误的打包方式

在本地环境编译,然后将可执行程序放入 alpine(docker.io/alpine:latest)

  1. 准备web程序

package main

import (
    "fmt"
    "net/http"
)

func main() {
    server := &http.Server{
        Addr: ":8888",
    }

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "hello world")
    })

    fmt.Println("server startup...")
    if err := server.ListenAndServe(); err != nil {
        fmt.Printf("server startup failed, err:%v\n", err)
    }
}

go build hello.go

  1. dockerfile

FROM       docker.io/alpine:latest
MAINTAINER demo <juest a demo>

#alpine内的包管理
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories
#安装bash
RUN apk add --update bash && rm -rf /var/cache/apk/*

RUN mkdir -p /data/go
COPY hello /data/go

EXPOSE 8888

ENTRYPOINT ["/data/go/hello"]
  1. 构建镜像

docker build -t demo/go-hello:1.0 -f dockerfile .
  1. 将在本地生成将demo/go-hello:1.o镜像

5.创建并运行容器

原因:编译的hello二进制程序不是存静态程序,还依赖一些库,但这些库在alpine镜像中找不到。

二、正确的打包流程

  1. 需要放入alpine镜像里运行的go程序,可以直接使用golang:alpine来编译,但我们在golang:alpine基础上再构建一个镜像,这个镜像中包含bash、GO111MODULE、GOPROXY等环境变量。

dockerfile

FROM docker.io/golang:alpine

RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories
RUN apk add --update bash && rm -rf /var/cache/apk/*

# 为我们的镜像设置必要的环境变量
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64\
    GOPROXY=https://goproxy.cn,direct
  1. 构建自己的go编译镜像

docker build -t go-build:1.0 -f dockerfile .
  1. 运行go-build:1.0 镜像,编译go项目代码:

#xxx为本地go代码路径
docker run -it --rm -v xxx:/data/go demo/go-build:1.0 /bin/bash
cd /data/go
go build hello.go

生成了hello可执行文件,且为纯静态的。

  1. 将编译得到的hello二进制打入alpine:latest

dockerfile2

FROM       docker.io/alpine:latest
MAINTAINER demo <juest a demo>

#alpine内的包管理
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories
#安装bash
RUN apk add --update bash && rm -rf /var/cache/apk/*

RUN mkdir -p /data/go
COPY hello /data/go

EXPOSE 8888

ENTRYPOINT ["/data/go/hello"]
  1. 打包

docker build -t demo/go-hello:1.0 -f dockerfile2 .
  1. 运行demo/go-hello:1.0

三、使用scratch构建镜像

scratch为空镜像,适合那些没有任何外部依赖的程序,刚好前面的hello程序没有任何依赖!

  1. dockerfile3

FROM      scratch
MAINTAINER demo <juest a demo>

COPY hello /

EXPOSE 8888

ENTRYPOINT ["/hello"]
  1. 构建

docker build -t demo/go-hello:2.0 -f dockerfile3 .

以scratch为基础构建出来的镜像是最小的

  1. 运行

四、参考以太坊的打包

目录结构

dockerfile

# Support setting various labels on the final image
ARG COMMIT=""
ARG VERSION=""
ARG BUILDNUM=""

# Build Geth in a stock Go builder container
FROM golang:alpine as builder

RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories
RUN apk add --no-cache gcc musl-dev linux-headers git

ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64\
    GOPROXY=https://goproxy.cn,direct

# Get dependencies - will also be cached if we won't change go.mod/go.sum
COPY go.mod /web/
COPY go.sum /web/
RUN cd /web && go mod download

ADD . /web
RUN cd /web && go build -o ./cmd/app main.go

# Pull Geth into a second stage deploy alpine container
FROM alpine:latest

RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main" > /etc/apk/repositories
RUN apk add --no-cache ca-certificates
COPY --from=builder /web/cmd/app /usr/local/bin/

EXPOSE 8080
ENTRYPOINT ["app"]

# Add some metadata labels to help programatic image consumption
ARG COMMIT=""
ARG VERSION=""
ARG BUILDNUM=""

LABEL commit="$COMMIT" version="$VERSION" buildnum="$BUILDNUM"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值