首页 > 解决方案 > Vuejs 到 Github Actions 中的 docker 容器

问题描述

我正在尝试找出如何最好地将我的 Vuejs 应用程序容器化,然后将其上传到 Google Container Registry。

我已经破解并削减了一些东西,但我认为这是不对的,我的容器被推送到 GCR 但它不起作用。

# My GitHub action file to build the container

name: Build and Push container to GCR

on:
  push:
    branches: [ master ]

env:
  PROJECT_ID: ${{ secrets.GKE_PROJECT }}
  DEPLOYMENT_NAME: my-deployment
  IMAGE: my-app-image

jobs:
  setup-build-publish:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@master

    # Setup gcloud CLI
    - uses: GoogleCloudPlatform/github-actions/setup-gcloud@0.1.3
      with:
        service_account_key: ${{ secrets.GKE_SA_KEY }}
        project_id: ${{ secrets.GKE_PROJECT }}

    # Configure Docker to use the gcloud command-line tool as a credential
    # helper for authentication
    - run: |-
        gcloud --quiet auth configure-docker

    # Build the Docker image
    - name: Build
      run: |-
        docker build \
          --tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \
          --build-arg GITHUB_SHA="$GITHUB_SHA" \
          --build-arg GITHUB_REF="$GITHUB_REF" \
          .

    # Push the Docker image to Google Container Registry
    - name: Publish
      run: |-
        docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA"

我的 Dockerfile

FROM node:lts-alpine

# make the 'app' folder the current working directory
WORKDIR /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./

# install project dependencies
RUN npm install

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .

# build app for production with minification
RUN npm run build

然后我部署此容器以在 GCP 上运行,但出现错误

Cloud Run 错误:容器无法启动。无法启动并监听 PORT 环境变量定义的端口

我通常部署到 AppEngine,但我正在学习如何使用容器,但我并没有完全理解我所缺少的东西。

标签: dockergithub-actionsgoogle-cloud-run

解决方案


您收到的错误是因为您没有在代码中侦听传入的 HTTP 请求,或者您正在侦听错误端口上的传入请求。

正如您在Cloud Run 容器运行时$PORT中所记录的那样,您的容器必须在由 Cloud Run 定义并在环境变量中提供的端口上侦听传入的 HTTP 请求。

如果这失败了,健康检查将失败,它会切换到错误状态,流量将不会被路由到正确的端口。

我会为 Node.js 发布一个示例,我可以看到您没有指定与端口相关的任何内容:

const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log('Hello world listening on port', port);
});

推荐阅读