登录
首页 >  Golang >  Go问答

在 Heroku 上使用 Docker 镜像部署包含 Go 应用和 Vue.js 的应用程序

来源:stackoverflow

时间:2024-03-22 11:34:12 356浏览 收藏

本文介绍了在 Heroku 上部署包含 Go 后端和 Vue.js 前端的应用程序的过程。部署时遇到错误,显示“没有运行的网络进程”,无法访问应用程序。原因是应用程序未从环境变量中读取动态端口,导致 Heroku 无法转发连接。本文提供了解决方案,建议在应用程序启动时读取环境变量中的端口值并侦听传入连接。通过将数据库连接和端口字符串更新为从环境变量中读取,并修复 Axios 中的 baseurl 设置,最终解决了该问题,应用程序可以在 Heroku 上成功运行。

问题内容

我正在尝试使用 docker 映像在 heroku 上部署应用程序 https://github.com/valasek/timesheet。 应用程序有一个 go 后端 (negroni/gorilla) 和前端 vue.js/vuetify.js,并使用 postgresql 持久性。

我陷入部署困境。我不知道如何调试,如何显示命令行输出,什么失败了......以及任何帮助,高度赞赏。

相关 dockerfile - https://github.com/valasek/timesheet/blob/master/dockerfile

以下是我正在执行的步骤:

> docker build --rm -f "Dockerfile" -t timesheet:latest .
Successfully tagged timesheet:latest
...

> heroku container:push timesheet:latest --app timesheet-cloud
...
The push refers to repository [registry.heroku.com/timesheet-cloud/timesheet]
...
Your image has been successfully pushed. You can now release it with the 'container:release' command.

> heroku container:release timesheet --app timesheet-cloud
Releasing images timesheet to timesheet-cloud... done

> heroku ps -a timesheet-cloud
Free dyno hours quota remaining this month: 971h 8m (97%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping

No dynos on ⬢ timesheet-cloud

> heroku logs --app timesheet-cloud
2019-02-15T08:33:49.373221+00:00 app[api]: Deployed timesheet (709022e100f9) by user @gmail.com
2019-02-15T08:33:49.373221+00:00 app[api]: Release v20 created by user @gmail.com
2019-02-15T08:34:43.901070+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=timesheet-cloud.herokuapp.com request_id=4c21eb79-5344-4d40-b341-8977128c873f fwd="195.250.152.42" dyno= connect= service= status=503 bytes= protocol=https
2019-02-15T08:34:44.842322+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=timesheet-cloud.herokuapp.com request_id=b66caaee-880a-46a0-918b-e778a49334f4 fwd="195.250.152.42" dyno= connect= service= status=503 bytes= protocol=https
2019-02-15T08:34:54.865321+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=timesheet-cloud.herokuapp.com request_id=d5af6aa7-0279-4f0e-a4cc-0f9e8682ec2f fwd="195.250.152.42" dyno= connect= service= status=503 bytes= protocol=https
2019-02-15T08:34:55.158317+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=timesheet-cloud.herokuapp.com request_id=2d69f4b5-9015-48de-9314-c493703818d1 fwd="195.250.152.42" dyno= connect= service= status=503 bytes= protocol=https

解决方案


这很可能发生,因为 heroku 没有检测到您的应用程序的任何端点(默认绑定在端口 3000 上)。

heroku 为您的应用程序分配一个动态端口,并将端口值放在名为 $port 的环境变量中。

您的应用必须使用 heroku 在此变量中指定的值并侦听其上的传入连接,因为这是 heroku 转发连接的位置。

应用程序启动时必须读取该变量: https://help.heroku.com/PPBPA231/how-do-i-use-the-port-environment-variable-in-container-based-apps

您可以使用 heroku run printenv 检查应用程序上的当前环境变量

(我看到你正在使用 viper,它应该处理 env 变量。但是他们在文档中说,当来自 env 时,它区分大小写,也许这可以解释为什么它不适合你:https://github.com/spf13/viper#working-with-environment-variables

谢谢@jonathan muller。解决了! gui 正在 https://timesheet-cloud.herokuapp.com 上运行。

  1. 在 heroku db 连接和 port 字符串中应从环境变量中读取

更新为文件 timesheet.yaml

  1. axios 中的 baseurl 设置为 '',因此 axios 使用相对 api url,这是可行的。已在文件 axiosSettings.js 中修复。

在命令 heroku run printenv 返回之前:

panic: dial tcp 127.0.0.1:5432: connect: connection refused

现在我得到:

Running printenv on ⬢ timesheet-cloud... up, run.1962 (Free)
Feb 15 16:22:34.186 [INFO] config file /timesheet.yaml
Feb 15 16:22:34.197 [INFO] connecting to DB postgres://user:[email protected]:5432/dbname
Feb 15 16:22:34.246 [INFO] connected to DB postgres://user:[email protected]:5432/dbname

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>