【问题标题】:Deploy to Heroku Error cannot execute binary file Exec format error部署到 Heroku 错误无法执行二进制文件 Exec 格式错误
【发布时间】:2020-12-04 08:10:02
【问题描述】:

我是 Golang 的新手,正在尝试将一个简单的 rest api 部署到 Heroku。

在我构建代码并在本地运行 heroku 之后,代码在本地运行。但是,当我将代码推送到 Heroku 时,该代码不起作用。在我的浏览器中,我看到了 heroku 的应用程序错误

我使用 heroku logs --tail 得到以下错误日志

错误代码是“无法执行二进制文件:执行格式错误”。我做了一个谷歌搜索,从我的阅读中,它说编译版本不兼容。如果是这样,我应该怎么做才能让正确的二进制文件在heroku上运行?谢谢

2020-08-14T11:48:37.863717+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T11:48:37.909304+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T11:48:37.946300+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-14T11:48:59.000000+00:00 app[api]: Build succeeded
2020-08-14T11:52:47.670135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=go-hendry.herokuapp.com request_id=06309354-6516-4520-8800-5fa12b8b421b fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T11:52:49.872040+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=go-hendry.herokuapp.com request_id=b8f0a6a6-06cc-4bef-a346-21b91ce024d2 fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T12:03:31.503635+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-14T12:03:32.679883+00:00 heroku[web.1]: Starting process with command `bin/golang-gin-poc`
2020-08-14T12:03:35.223469+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T12:03:35.263368+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T12:03:35.306058+00:00 heroku[web.1]: State changed from starting to crashed

以下是我的 server.go 代码,以供参考

package main

import (
    "io"
    "net/http"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/myhendry/goapi/controller"
    "github.com/myhendry/goapi/service"
)

var (
    videoService    service.VideoService       = service.New()
    videoController controller.VideoController = controller.New(videoService)
)

func setupLogOutput() {
    f, _ := os.Create("gin.log")
    gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}

func main() {
    setupLogOutput()

    server := gin.New()
    server.Use(gin.Logger())

    apiRoutes := server.Group("/api")
    {
        apiRoutes.GET("/videos", func(ctx *gin.Context) {
            ctx.JSON(200, videoController.FindAll())
        })

        apiRoutes.POST("/videos", func(ctx *gin.Context) {
            err := videoController.Save(ctx)
            if err != nil {
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
            } else {
                ctx.JSON(http.StatusOK, gin.H{"message": "Video Input is Valid!!"})
            }

        })
    }

    port := os.Getenv("PORT")

    if port == "" {
        port = "5000"
    }

    server.Run(":" + port)
}

【问题讨论】:

  • 您在什么平台上构建二进制文件?如果是 OSX,则需要针对 linux 进行交叉编译。或者更好的是,让 heroku 自己完成构建步骤,不要推送二进制文件。
  • 是的。我正在使用 OSX。我有一个 Procfile 网站:bin/golang-gin-poc。那是在heroku上构建的吗?还是我需要做点别的?谢谢
  • 我不知道。 procfile的内容是什么?
  • 网页:bin/golang-gin-poc
  • 那么你只是将你的二进制文件推送到heroku。您需要构建一个交叉编译的二进制文件,或者让 heroku 从源代码构建。我确定他们的文档有详细信息。

标签: go


【解决方案1】:

在终端本地开发机Mac OS上编译

env GOOS=linux GOARCH=amd64 go build -o bin/golang-gin-poc -v .

然后推送到 Heroku

我仍在寻找在 heroku 上进行后期构建的解决方案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多