docker 容器中部署 Go 服务时,请求 https 文件时抛错

错误提示:

Get https://res.ddkt365.com/ddktRes/imageRes/wx_headimg/0f1d9e55913c22bcaf7cca9b38048d29.jpeg: x509: certificate signed by unknown authority -> http.Get
panic: runtime error: invalid memory address or nil pointer dereference

 

 

 

是 CA 证书的问题,解决方案就是 拷贝 alpine 镜像中的证书

 

附上 dockerfile 代码

# 基础镜像是 docker 仓库的 busybox
FROM alpine as certs
RUN apk update && apk add ca-certificates
# 从别的镜像中 copy 证书
FROM busybox
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
# 作者签名
LABEL author="szliugx@gmail.com"
# 工作目录
WORKDIR /root

# 添加文件
ADD ./bin/ddkt-poster-go ./ddkt-poster-go
ADD ./bin/conf.toml ./conf.toml
ADD ./fonts ./fonts
# 暴露出端口号
#EXPOSE 8080
# 运行容器执行时的口令
ENTRYPOINT ["./ddkt-poster-go", "-conf", "conf.toml"]

  

 

 

 

liugx@MacBook-Pro  ~/work/personal/golang/ddkt-poster   liugx_code_youhua ●  docker build -t ddkt-poster-go .
Sending build context to Docker daemon  78.08MB
Step 1/10 : FROM alpine as certs
latest: Pulling from library/alpine
aad63a933944: Pull complete 
Digest: sha256:b276d875eeed9c7d3f1cfa7edb06b22ed22b14219a7d67c52c56612330348239
Status: Downloaded newer image for alpine:latest
 ---> a187dde48cd2
Step 2/10 : RUN apk update && apk add ca-certificates
 ---> Running in 1a290f0b3176
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
v3.11.5-5-gfa48ebadee [http://dl-cdn.alpinelinux.org/alpine/v3.11/main]
v3.11.5-1-ge59ae1cbad [http://dl-cdn.alpinelinux.org/alpine/v3.11/community]
OK: 11265 distinct packages available
(1/1) Installing ca-certificates (20191127-r1)
Executing busybox-1.31.1-r9.trigger
Executing ca-certificates-20191127-r1.trigger
OK: 6 MiB in 15 packages
Removing intermediate container 1a290f0b3176
 ---> 409d89df1e27
Step 3/10 : FROM busybox
 ---> 83aa35aa1c79
Step 4/10 : COPY --from=certs /etc/ssl/certs /etc/ssl/certs
 ---> f3d288b6f60c
Step 5/10 : LABEL author="szliugx@gmail.com"
 ---> Running in 81fa23a198e9
Removing intermediate container 81fa23a198e9
 ---> 474ca6db3d19
Step 6/10 : WORKDIR /root
 ---> Running in 670487d3f2a5
Removing intermediate container 670487d3f2a5
 ---> ebbc723821b0
Step 7/10 : ADD ./bin/ddkt-poster-go ./ddkt-poster-go
 ---> 89d7a0e23eca
Step 8/10 : ADD ./bin/conf.toml ./conf.toml
 ---> 4ba50a3e49f8
Step 9/10 : ADD ./fonts ./fonts
 ---> 672cb62f608a
Step 10/10 : ENTRYPOINT ["./ddkt-poster-go", "-conf", "conf.toml"]
 ---> Running in 86afb270dab0
Removing intermediate container 86afb270dab0
 ---> da2ec7a93580
Successfully built da2ec7a93580
Successfully tagged ddkt-poster-go:latest
 liugx@MacBook-Pro  ~/work/personal/golang/ddkt-poster   liugx_code_youhua ●  
执行过程

 

引用链接:

https://stackoverflow.com/questions/45388934/how-do-i-make-an-https-call-in-a-busybox-docker-container-running-go

 

posted @ 2020-03-26 22:44  liugx  阅读(1255)  评论(0编辑  收藏  举报