【问题标题】:Github Action for updating a docker container on GCE用于在 GCE 上更新 docker 容器的 Github Action
【发布时间】:2021-06-05 12:36:24
【问题描述】:

我目前正在尝试创建一个 Github Action,它会自动更新我的一个 GCE 实例上正在运行的容器。我正在使用the template from this repo:

# Copyright 2020 Google, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Build and Deploy to Google Compute Engine

on:
  push:
    branches:
      - master

env:
  PROJECT_ID: ${{ secrets.GCE_PROJECT }}
  GCE_INSTANCE: instance-name
  GCE_INSTANCE_ZONE: us-east1-d

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

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

      # Setup gcloud CLI
      - uses: google-github-actions/setup-gcloud@master
        with:
          version: '290.0.1'
          service_account_key: ${{ secrets.GCE_SA_KEY }}
          project_id: ${{ secrets.GCE_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/$GCE_INSTANCE-image:$GITHUB_SHA" .
      # Push the Docker image to Google Container Registry
      - name: Publish
        run: |-
          docker push "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"
      - name: Deploy
        run: |-
          gcloud compute instances update-container "$GCE_INSTANCE" \
            --zone "$GCE_INSTANCE_ZONE" \
            --container-image "gcr.io/$PROJECT_ID/$GCE_INSTANCE-image:$GITHUB_SHA"

到目前为止,我更新应用程序的方法是从我的 github 存储库中手动提取更改并运行 docker-compose up --build。我正在使用 docker-compose,因为我的应用程序也在使用 Postgres DB。

这是我的Dockerfile

FROM node:14
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD [ "npm", "run", "prod" ]

还有我的docker-compose.yaml(带有虚拟值):

version: "3"
services:
  app:
    container_name: cname
    restart: always
    build: .
    depends_on:
      - postgres
  postgres:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pw
      POSTGRES_DB: db
    ports:
      - '5432:5432'
    volumes:
      - db-volume:/var/lib/postgresql/data

volumes:
  db-volume:

应用程序本身运行正常。在 master 上提交后,会创建一个 docker 映像并将其存储在 GCP 容器注册表中,但是如何将这个容器与 Postgres 容器一起运行?

【问题讨论】:

  • how do I run this container together with the Postgres Container 是什么意思?如果我理解正确,您似乎没有为他们定义网络。
  • @Saeed 我正在使用上面的 docker-compose 文件来启动我的应用程序所需的两个容器。我不明白如何使用 Github Actions 和 gcloud 来实现相同的目标。 Github 操作模板尚未使用 docker-compose 文件,这意味着 postgres 的容器永远不会启动/更新。

标签: docker google-cloud-platform docker-compose google-compute-engine github-actions


【解决方案1】:

您有 2 个解决方案。

  • 要么使用 Docker Compose
  • 或者您将 GCE 与容器映像一起使用

使用 Docker Compose,您无需使用 GCE 功能,只需在启动时运行一个命令。如果你想这样做,你可以定义一个startup script。我的建议是将其存储在 Google Cloud Storage 上,并在您的 startup script url 中引用它(不要忘记在您的计算引擎上授予正确的角色和范围以使其访问文件)。

然后,每次部署新版本时,使用最新的图像标签(使用 GIT_SHA 值)更新(使用 github 操作)GCS 中的启动脚本,然后reset your GCE 重新启动它并重播启动脚本(从而加载最新的图像)


因为使用 docker compose 不是 GCE 的原生特性(以及维护它、修补它等等的事实),我不推荐这个。

就性能和可扩展性而言,将Cloud SQL 用于您的 postgres 数据库并在 GCE 上仅部署您的 Node 容器可能会更好。像这样,您将能够开箱即用地使用具有容器图像功能的 GCE。


但这个解决方案还不完美。您只有 1 个 VM,您必须对其进行监控、更新、修补……最终的解决方案是使用全新的 autopilot mode for GKE 来托管您的 Node 容器,或者如果您的 Node 容器只响应 HTTP,则使用 Cloud Run请求。

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2021-11-22
    • 2021-11-09
    • 1970-01-01
    • 2019-04-24
    • 2020-11-12
    • 2021-09-17
    • 1970-01-01
    • 2021-02-13
    相关资源
    最近更新 更多