go官方提供了各种版本的镜像,在Easypack中根据官方的Alpine镜像也创建go语言的镜像,因为go 1.13开始GOPROXY 默认为 https://proxy.golang.org,将GOPROXY缺省设定为goproxy.cn,以方便更简单地使用go镜像进行构建。
官方镜像分如下三类:
golang:
golang:-alpine
golang:-windowsservercore
镜像tag:golang:
下载镜像:docker pull liumiaocn/golang:1.13.5-alpine3.11
这里以Prometheus提供的go语言的示例应用(为Prometheus提供可供抓取的数据)为例进行说明如何使用Docker镜像进行go语言应用的编译。
执行命令:git clone https://github.com/prometheus/client_golang.git
当然也可以直接使用自己手头已有的go语言的应用示例进行编译。
启动Docker并将待编译的工程进行卷的映射
执行命令示例:docker run --rm -it -v `pwd`/client_golang:/project liumiaocn/golang:1.13.5-alpine3.11 sh
liumiaocn:golang liumiao$ docker run --rm -it -v `pwd`/client_golang:/go/src liumiaocn/golang:1.13.5-alpine3.11 sh
/go # ls
bin src
/go # cd src
/go/src # ls
CHANGELOG.md LICENSE Makefile.common VERSION go.mod
CONTRIBUTING.md MAINTAINERS.md NOTICE api go.sum
Dockerfile Makefile README.md examples prometheus
/go/src #
此示例工程下有如下go语言的应用,我们对其进行编译
/go/src # cd examples/random/
/go/src/examples/random # ls
main.go
/go/src/examples/random #
/go/src/examples/random # go get -d
go: downloading github.com/prometheus/common v0.7.0
go: downloading github.com/prometheus/client_model v0.1.0
go: downloading github.com/prometheus/procfs v0.0.8
go: downloading golang.org/x/sys v0.0.0-20191220142924-d4481acd189f
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/golang/protobuf v1.3.2
go: extracting github.com/prometheus/common v0.7.0
go: extracting github.com/prometheus/client_model v0.1.0
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
go: extracting github.com/cespare/xxhash/v2 v2.1.1
go: extracting github.com/golang/protobuf v1.3.2
go: extracting github.com/beorn7/perks v1.0.1
go: extracting github.com/prometheus/procfs v0.0.8
go: extracting github.com/matttproud/golang_protobuf_extensions v1.0.1
go: extracting golang.org/x/sys v0.0.0-20191220142924-d4481acd189f
/go/src/examples/random #
/go/src/examples/random # go build
go: finding github.com/prometheus/client_model v0.1.0
go: finding github.com/prometheus/common v0.7.0
go: finding github.com/beorn7/perks v1.0.1
go: finding github.com/cespare/xxhash/v2 v2.1.1
go: finding github.com/golang/protobuf v1.3.2
go: finding github.com/prometheus/procfs v0.0.8
go: finding github.com/matttproud/golang_protobuf_extensions v1.0.1
/go/src/examples/random #
/go/src/examples/random # ls
main.go random
/go/src/examples/random #
可以看到已经构建了一个名为random的可执行文件, 至此已经确认完毕。确认main.go就会发现实际上此应用就是在指定端口的/metrics的URL上提供一些随机的指标数据,比如这里在8080端口启动此应用。
/go/src/examples/random # ./random -listen-address=:8080 &
/go/src/examples/random # ps
PID USER TIME COMMAND
1 root 0:00 sh
203 root 0:00 ./random -listen-address=:8080
211 root 0:00 ps
/go/src/examples/random #
由于此镜像也是在官方Alpine镜像基础上作出的,缺省未安装curl,此处为了验证手动添加一下
/go/src/examples/random # apk add curl
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
(1/3) Installing nghttp2-libs (1.40.0-r0)
(2/3) Installing libcurl (7.67.0-r0)
(3/3) Installing curl (7.67.0-r0)
Executing busybox-1.31.1-r8.trigger
OK: 7 MiB in 18 packages
/go/src/examples/random #
动作确认
/go/src/examples/random # curl http://localhost:8080/metrics 2>/dev/null |head -n 15
# HELP go_build_info Build information about the main Go module.
# TYPE go_build_info gauge
go_build_info{checksum="",path="github.com/prometheus/client_golang",version="(devel)"} 1
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 10
/go/src/examples/random #
所以可以看出构建出来的应用是可以正常动作的。