在多阶段构建中在docker容器中使用golang

I want to use multi-stage build and I want that at the end I will have Golang inside the running container. When I run the container and do go version I get error “unknown go”

# build stage
FROM golang:1.11.2-alpine3.8 AS builder-env



ENV CGO_ENABLED=0
ENV GOOS=linux


ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep

RUN mkdir -p $GOPATH/src/github/mtp/myproj
WORKDIR  $GOPATH/src/github/mtp/myproj


COPY Gopkg.toml Gopkg.lock ./


RUN dep ensure --vendor-only

COPY . ./



RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /myproj .


FROM alpine:latest
RUN mkdir /data

COPY --from=builder-env myproj ./

I can do something like this to make it work, but is it a good options ?

FROM alpine:latest
RUN mkdir /data
RUN update-ca-certificates && \
    apk add go 

COPY --from=builder-env myproj ./

You don't need the go executable to run a compiled program, just the resulting binary. If you add in the missing CMD line to your Dockerfile

CMD ["./myproj"]

I'd expect it would work fine.