dsiv4041 2019-05-10 08:16
浏览 178
已采纳

如何正确配置Dockerfile以在Google Cloud Run上运行?

I'm trying to run a Go app using Docker on Google Cloud Run but I'm getting this error:

Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

I fixed my port to be 8080 as stated in the docs but I think my Dockerfile is incorrect. Does anyone know what I'm missing?

FROM golang:1.12-alpine

RUN apk upgrade -U \
  && apk add \
  ca-certificates \
  git \
  libva-intel-driver \
  make \
  && rm -rf /var/cache/*

ENV GOOS linux
ENV GOARCH amd64
ENV CGO_ENABLED=0
ENV GOFLAGS "-ldflags=-w -ldflags=-s"
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
RUN echo $PATH
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
RUN go get -u github.com/cespare/reflex
# RUN reflex -h 
# Setup modules after reflex install
ENV GO111MODULE=on \
  GOFLAGS="$GOFLAGS -mod=vendor"

WORKDIR /go/src/bitbucket.org/team/app/

COPY . .

CMD [ "go", "run", "cmd/main.go" ]
  • 写回答

2条回答 默认 最新

  • douzi2785 2019-05-10 16:45
    关注

    Dockerfiles don't make your application listen on a specific port number.

    The EXPOSE directive in Dockerfile is purely a documentation and doesn't do anything functional.

    You have 2 options for a Go app:

    1. Just refactor your code to read the PORT env variable: os.Getenv("PORT") and use it on the HTTP server address you’re starting:

      port := os.Getenv("PORT")
      http.ListenAndServe(":"+port)
      
    2. Create a -port flag and read it during the entrypoint of your app in the Dockerfile:

      e.g. if you can make go run main.go -port=8080 work, change your dockerfile to:

      exec go run main.go -port=$PORT
      

    These will get you what you want.

    Ideally you should not use go run inside a container. Just do:

    RUN go build -o /bin/my-app ./my/pkg
    ENTRYPOINT /bin/my-app
    

    to compile a Go program and use it directly. Otherwise, every time Cloud Run starts your container, you would be re-compiling it from scratch, which is not fast, this will increase your cold start times.


    Aside from these you seem to have a lot of inconsistencies in your dockerfile. You set a lot of Go env vars like GOOS GOARCH but you don't actually go build your app (go run is an on-the-fly compilation and doesn't take the linker flags in GOFLAGS into account I believe). Look at sample Go dockerfiles to have a better idea on how to write idiomatic Go dockerfiles.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵