作者 陈志颖

chore:优化镜像打包

1 <component name="ProjectRunConfigurationManager"> 1 <component name="ProjectRunConfigurationManager">
2 - <configuration default="false" name="Docker for Mac" type="docker-deploy" factoryName="dockerfile" server-name="Docker for Mac"> 2 + <configuration default="false" name="Docker for Mac" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
3 <deployment type="dockerfile"> 3 <deployment type="dockerfile">
4 <settings> 4 <settings>
5 - <option name="imageTag" value="" /> 5 + <option name="imageTag" value="latest" />
6 <option name="containerName" value="allied-creation-cooperation" /> 6 <option name="containerName" value="allied-creation-cooperation" />
  7 + <option name="envVars">
  8 + <list>
  9 + <DockerEnvVarImpl>
  10 + <option name="name" value="POSTGRESQL_HOST" />
  11 + <option name="value" value="192.168.100.27" />
  12 + </DockerEnvVarImpl>
  13 + </list>
  14 + </option>
  15 + <option name="portBindings">
  16 + <list>
  17 + <DockerPortBindingImpl>
  18 + <option name="containerPort" value="8082" />
  19 + <option name="hostPort" value="8082" />
  20 + </DockerPortBindingImpl>
  21 + </list>
  22 + </option>
7 <option name="sourceFilePath" value="Dockerfile" /> 23 <option name="sourceFilePath" value="Dockerfile" />
8 </settings> 24 </settings>
9 </deployment> 25 </deployment>
1 -FROM golang:latest 1 +#FROM golang:latest
  2 +#MAINTAINER 陈志颖 <steve.d.chan@qq.com>
  3 +#ENV APP_DIR $GOPATH/src/allied-creation-cooperation
  4 +#RUN mkdir -p $APP_DIR
  5 +#WORKDIR $APP_DIR/
  6 +#COPY ./pkg pkg
  7 +#COPY ./config config
  8 +#COPY ./go.mod go.mod
  9 +#COPY ./go.sum go.sum
  10 +#COPY ./main.go main.go
  11 +#RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"]
  12 +#ENV GO111MODULE on
  13 +#ENV GOPROXY https://goproxy.io
  14 +#RUN ["go","mod","tidy"]
  15 +#RUN ["ls"]
  16 +#RUN ["go","build"]
  17 +#ENTRYPOINT ["./allied-creation-cooperation"]
  18 +
  19 +#############################
  20 +## STEP 1 build executable binary
  21 +#############################
  22 +FROM golang:latest AS builder
2 MAINTAINER 陈志颖 <steve.d.chan@qq.com> 23 MAINTAINER 陈志颖 <steve.d.chan@qq.com>
3 -ENV APP_DIR $GOPATH/src/allied-creation-cooperation  
4 -RUN mkdir -p $APP_DIR  
5 -WORKDIR $APP_DIR/  
6 -COPY ./pkg pkg  
7 -COPY ./config config  
8 -COPY ./go.mod go.mod  
9 -COPY ./go.sum go.sum  
10 -COPY ./main.go main.go 24 +RUN mkdir /app
  25 +WORKDIR /app
  26 +COPY . /app
11 RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"] 27 RUN ["ln","-sf","/usr/share/zoneinfo/Asia/Shanghai","/etc/localtime"]
12 -ENV GO111MODULE on  
13 -ENV GOPROXY https://goproxy.io  
14 -RUN ["go","mod","tidy"]  
15 -RUN ["ls"]  
16 -RUN ["go","build"]  
17 -ENTRYPOINT ["./allied-creation-cooperation"]  
  28 +RUN GO111MODULE="on" GOPROXY="https://goproxy.cn" CGO_ENABLED=0 GOOS=linux go build -o allied-creation-cooperation
  29 +
  30 +#############################
  31 +## STEP 2 build a small image
  32 +############################
  33 +FROM scratch as final
  34 +COPY --from=builder /app/allied-creation-cooperation /app/allied-creation-cooperation
  35 +ENTRYPOINT ["./app/allied-creation-cooperation"]