1 构建golang开发环境
FROM centos:centos7.6.1810
USER root
RUN yum install -y wget && cd /usr/local/src && wget https://studygolang.com/dl/golang/go1.16.3.linux-amd64.tar.gz && tar -zxvf go1.16.3.linux-amd64.tar.gz -C /usr/local/
ENV GOROOT=/usr/local/go
ENV GOPATH=/home/gopath
ENV GOBIN=/home/gopath/bin
ENV GOPROXY=https://goproxy.cn,direct
ENV GO111MODULE=on
ENV PATH=$PATH:$GOROOT/bin
ENV PATH=$GOPATH/bin/:$PATH
WORKDIR /my_work_dir
ENV CGO_ENABLED=0
ENV GOOS=linux
2 构建编译二进制产出的golang镜像
2.1 构建镜像
FROM centos:centos7.6.1810
USER root
RUN yum install -y wget && cd /usr/local/src && wget https://studygolang.com/dl/golang/go1.16.3.linux-amd64.tar.gz && tar -zxvf go1.16.3.linux-amd64.tar.gz -C /usr/local/
ENV GOROOT=/usr/local/go
ENV GOPATH=/home/gopath
ENV GOBIN=/home/gopath/bin
ENV GOPROXY=https://goproxy.cn,direct
ENV GO111MODULE=on
ENV PATH=$PATH:$GOROOT/bin
ENV PATH=$GOPATH/bin/:$PATH
WORKDIR /my_work_dir
ENV CGO_ENABLED=0
ENV GOOS=linux
CMD ["go", "build", "-o", "app", "./"]
2.2 用法
适合场景有:
- 无golang环境,但有docker
- 需要交叉编译其它平台的二进制
若你构建的镜像名是go16build:v1
, 那么在当前代码库下执行
#可设置环境变量CGO_ENABLED实现动态链接编译 docker run -it --rm -e CGO_ENABLED=1 -v $(pwd):/my_work_dir go16build:v1
docker run -it --rm -v $(pwd):/my_work_dir go16build:v1
若未构建镜像,可使用我已经构建好的zzc932/go16dev:v1
镜像
# zzc932/go16dev:v1镜像内默认的workdir是/paddleflow
docker run -it --rm -v $(pwd):/paddleflow zzc932/go16dev:v1
标签:bin,golang,编译,构建,ENV,linux,PATH,local
From: https://www.cnblogs.com/nishiliu/p/16870998.html