将 Vue.js/Go 容器部署到 heroku 不起作用

时间:2021-07-18 11:23:01

标签: docker go nginx heroku deployment

我正在尝试部署在前端使用 Vuejs 的项目(在 localhost:8080 中运行并硬编码以将 ws 和 http 请求发送到 localhost:5000。它还使用 vue 路由器来处理路由)和一个 go websocket 服务器(在 localhost:5000)作为后端。我曾尝试部署 git 存储库,但 heroku 希望存储库采用一种语言,因此我目前正在使用多阶段 docker 构建,因为我已将其划分为前端和后端目录。

Dockerfile

FROM golang:1.16-alpine as go-build
ADD . /build
WORKDIR /build/vc-server
COPY vc-server/*.go ./
COPY vc-server/go.mod ./
COPY vc-server/go.sum ./
RUN CGO_ENABLED=0 GOOS=linux go build

FROM node:14-alpine AS vue-build
WORKDIR /build/vc-frontend
COPY vc-frontend/. ./
COPY vc-frontend/package*.json ./
RUN npm install
RUN npm run build

FROM nginx:1.16.0-alpine
COPY --from=vue-build /build/vc-frontend/dist /usr/share/nginx/html
COPY --from=go-build /build/vc-server/vc-server ./
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d
COPY entrypoint.sh .
RUN ["chmod", "+x", "entrypoint.sh"]
ENTRYPOINT ["./entrypoint.sh"]

entrypoint.sh - 使用 nginx 服务前端 dist 并运行 go 二进制文件。

#!/bin/sh
./vc-server & 
sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/nginx.conf && nginx -g 'daemon off;'

nginx.conf

server {
    listen $PORT;
    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }

如果我尝试构建它并使用命令 docker run -it -e 'PORT=80' -p 8080:80 -p 5000:5000 --rm vc:latest 在本地运行它 那么应用程序在端口 8080 上工作正常。当我释放这个容器时,有一个应用程序错误,这就是日志显示的内容。

2021-07-18T11:12:47.572098+00:00 heroku[web.1]: Starting process with command `./entrypoint.sh`
2021-07-18T11:12:50.572311+00:00 app[web.1]: 2021/07/18 11:12:50 [warn] 7#7: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-07-18T11:12:50.572313+00:00 app[web.1]: nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2021-07-18T11:12:50.573959+00:00 app[web.1]: 2021/07/18 11:12:50 [emerg] 7#7: bind() to 0.0.0.0:80 failed (13: Permission denied)
2021-07-18T11:12:50.573960+00:00 app[web.1]: nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
2021-07-18T11:12:50.635213+00:00 heroku[web.1]: Process exited with status 1
2021-07-18T11:12:50.719062+00:00 heroku[web.1]: State changed from starting to crashed
2021-07-18T11:12:55.716826+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=vc-test.herokuapp.com request_id=9ece7adc-3caa-4ca0-873b-5bbd567d9759 fwd="94.205.20.95" dyno= connect= service= status=503 bytes= protocol=https
2021-07-18T11:12:56.461615+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=vc-test.herokuapp.com request_id=35e06cac-eef8-40a1-8094-93599a692a33 fwd="94.205.20.95" dyno= connect= service= status=503 bytes= protocol=https

我该如何解决这个问题,或者有没有更好的方法来部署这样的项目?我已经使用类似技术浏览了大量 github 存储库,但找不到任何有用的东西。

0 个答案:

没有答案