首页 > 解决方案 > 使用 Dockerfile 从部署到 App Engine 的 Ghost 连接到 Google Cloud SQL

问题描述

我正在按照本教程将 Ghost 部署到 Google App Engine https://cloud.google.com/community/tutorials/ghost-on-app-engine-part-1-deploying

但是,将 Ghost 安装为 NPM 模块的方法已被弃用。

本教程介绍了一种使用 Dockerfile 安装 Ghost 的方法。https://vanlatum.dev/ghost-appengine/

我正在尝试利用此 Dockerfile 将 Ghost 部署到 Google App Engine,并连接到我的 Google Cloud SQL 数据库。

但是我遇到了问题:

ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
[2019-10-03 21:10:46] ERROR connect ENOENT /cloudsql/ghost

connect ENOENT /cloudsql/ghost

"Unknown database error"

Error ID:
    500

Error Code:
    ENOENT

----------------------------------------

DatabaseError: connect ENOENT /cloudsql/ghost
    at DatabaseError.KnexMigrateError (/var/lib/ghost/versions/2.31.1/node_modules/knex-migrator/lib/errors.js:7:26)

在第一个教程中,它提到需要在启动 ghost 之前运行迁移以防止此问题。所以我尝试在我的 Dockerfile 中添加这一行

RUN npm install knex-migrator --no-save
RUN NODE_ENV=production node_modules/knex-migrator init --mgpath node_modules/ghost

但后来我收到以下错误:

/bin/sh: 1: node_modules/knex-migrator: Permission denied
The command '/bin/sh -c NODE_ENV=production node_modules/knex-migrator init --mgpath node_modules/ghost' returned a non-zero code: 126

如何配置我的 Dockerfile 以在运行 Ghost 之前迁移数据库以确保它可以连接到 Cloud SQL 数据库?

文件:

Dockerfile

FROM ghost

COPY config.production.json /var/lib/ghost/config.production.json
WORKDIR /var/lib/ghost
COPY credentials.json /var/lib/ghost/credentials.json
RUN npm install ghost-gcs --no-save
WORKDIR /var/lib/ghost/content/adapters/storage/ghost-gcs/
ADD https://raw.githubusercontent.com/thomas-vl/ghost-gcs/master/export.js index.js

WORKDIR /var/lib/ghost

config.production.json

{
  "url": "https://redactedurl.appspot.com",
  "fileStorage": false,
  "mail": {},
  "database": {
    "client": "mysql",
    "connection": {
      "socketPath": "/cloudsql/ghost",
      "user": "redacted",
      "password": "redacted",
      "database": "ghost",
      "charset": "utf8"
    },
    "debug": false
  },
  "server": {
    "host": "0.0.0.0",
    "port": "2368"
  },
  "paths": {
    "contentPath": "content/"
  },
  "logging": {
    "level": "info",
    "rotation": {
      "enabled": true
    },
    "transports": ["file", "stdout"]
  },
  "storage": {
    "active": "ghost-gcs",
    "ghost-gcs": {
      "key": "credentials.json",
      "bucket": "redactedurl"
    }
  }
}

应用程序.yaml

runtime: custom
service: blog
env: flex
manual_scaling:
  instances: 1
env_variables:
  MYSQL_USER: redacted
  MYSQL_PASSWORD: redacted
  MYSQL_DATABASE: ghost
  INSTANCE_CONNECTION_NAME: redacted:us-central1:ghost
beta_settings:
  cloud_sql_instances: redacted:us-central1:ghost
skip_files:
  - ^(.*/)?#.*#$
  - ^(.*/)?.*~$
  - ^(.*/)?.*\.py[co]$
  - ^(.*/)?.*/RCS/.*$
  - ^(.*/)?\..*$
  - ^(.*/)?.*\.ts$
  - ^(.*/)?config\.development\.json$

``

标签: node.jsdockergoogle-app-enginegoogle-cloud-sqlghost-blog

解决方案


根据从 App Engine页面连接,您需要将路径更新为/cloudsql/INSTANCE_CONNECTION_NAME(so /cloudsql/redacted:us-central1:ghost)。


推荐阅读