首先建议直接放弃alpine: 关于docker:如何安装Go in alpine linux | 码农家园 (codenong.com)
1 准备基础镜像Dockerfile 根据golang:1.16 的debian系统自己制作
FROM golang:1.16
#维护人员
MAINTAINER Lvv <123@qq.com>
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive
RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
#更新alpine,并安装curl bash bash-doc bash-completion
RUN apt-get update \
&& apt-get install net-tools \
&& apt-get clean
docker build -t golang:1.16-debian .
运行下测试时区:
[root@docker-cs3 go]# docker run -it --rm golang:1.16-debian /bin/sh
# date
Fri Oct 8 22:12:19 CST 2021
# netstat -tunl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
推送到自己的harbor仓库:
docker login -u xxx -p xxx myhub.mydocker.com
[root@docker-cs3 go]# docker tag golang:1.16-debian myhub.mydocker.com/base/debian-golang:1.16
[root@docker-cs3 go]# docker push myhub.mydocker.com/base/debian-golang:1.16
2 使用基础镜像编写golang应用的Dockerfile
FROM myhub.mydocker.com/base/debian-golang:1.16
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
GIN_MODE=release
WORKDIR /opt/microservice
COPY . .
RUN go build .
EXPOSE 8090
ENTRYPOINT ["./gin-k8s"]
构建:
docker build -t gin-k8s:0.1 .
测试:
[root@docker-cs3 gin-k8s]# docker run -it --rm -p 8090:8090 gin-k8s:0.1 /bin/bash
数据库连接成功 0x25e4f98
使用日志中间件
创建目录成功
/opt/microservice/logs/access.log
常备命令: 有时候build失败,根据失败前的镜像id /bin/bash进去查看镜像情况:
docker run -it --rm f1a3b860ed6e /bin/bash