-2

我想用固定版本的micro和 go 依赖项构建一个 docker 映像。我打算这样做dep

git checkout git@github.com:micro/micro.git
dep ensure
git add Gopkg.toml
git add Gopkg.lock

# Build micro
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go 

# Build docker image
...

所以,我的问题是它是构建一致的微型docker 镜像的最佳解决方案吗?

4

2 回答 2

1

Dockerfile 的一个示例可以是:

FROM golang:1.9-alpine3.6 as builder

# Install package manager
RUN apk add --no-cache --virtual .go-dependencies git curl \
  && curl https://glide.sh/get | sh

# Copy files from context
WORKDIR /go/src/github.com/foo/bar
COPY . .

# Install project dependencies, test and build
RUN glide install \
  && go test ./... \
  && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o ./entry ./main.go ./plugins.go

# Build final image with binary
FROM alpine:3.6
RUN apk add --update ca-certificates && \
    rm -rf /var/cache/apk/* /tmp/*
WORKDIR /
COPY --from=builder /go/src/github.com/foo/bar/entry .
ENTRYPOINT [ "/entry" ]

glide.yaml 看起来像这样:

package: .
import:
- package: github.com/micro/go-micro
  version: ^0.3.0
  subpackages:
  - client
  - server
- package: github.com/micro/go-plugins
  version: ^0.6.1
  subpackages:
  - wrapper/trace/opentracing
  - broker/nats
  - transport/nats
- package: github.com/opentracing/opentracing-go
  version: ^1
- package: github.com/openzipkin/zipkin-go-opentracing
  version: ^0.3
testImport:
- package: github.com/golang/mock
  subpackages:
  - gomock
- package: github.com/smartystreets/goconvey
  subpackages:
  - convey
于 2018-01-30T13:27:17.163 回答
0

就我而言,dep看起来很棒而且足够快,而且它是 go 中的官方依赖管理器,所以我认为这是一个正确的选择。

于 2018-02-06T04:45:08.377 回答