Prisma和Postgres FetchError:对http:// localhost:4466 / management的请求失败

时间:2019-07-26 07:54:06

标签: postgresql docker docker-compose prisma

我试图与Prisma合作。我已经尝试了有关GitHub问题和StackOverflow的可用解决方案。

我已经使用以下命令安装了Prisma

npm install -g prisma

我在系统中安装了docker和docker-compose。

以下是我的docker-compose文件

version: '3'
services:
 prisma:
   image: prismagraphql/prisma:1.34
   restart: always
   ports:
    - "4466:4466"
   environment:
      PRISMA_CONFIG: |
       port: 4466
       # uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security
       # managementApiSecret: my-secret
       databases:
         default:
           connector: postgres
           host: host.docker.internal
           database: db
           schema: public
           user: prisma
           password: prisma
           ssl: false
           rawAccess: true
           port: '5432'
           migrations: true
  postgres:
   image: postgres:11
   restart: always
   environment:
     POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
   volumes:
     - postgres:/var/lib/postgresql/data
volumes:
  postgres: ~

所以,现在我尝试使用以下命令启动容器

docker-compose up -d

现在,我使用以下命令检查容器是否运行正常。

docker-compose ps

以下是输出

Name                        Command                         State   Ports         
------------------------------------------------------------------------------------------
graphql-prisma_postgres_1   docker-entrypoint.sh postgres   Up      5432/tcp              
graphql-prisma_prisma_1     /bin/sh -c /app/start.sh        Up      0.0.0.0:4466->4466/tcp

现在,如果我尝试执行以下操作

prisma deploy
docker-compose ps

以下是输出

environment FetchError: request to http://localhost:4466/management failed, reason: connect ECONNREFUSED 127.0.0.1:4466
environment     at ClientRequest.<anonymous> (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/node-fetch/lib/index.js:1393:11)
environment     at ClientRequest.emit (events.js:200:13)
environment     at ClientRequest.EventEmitter.emit (domain.js:471:20)
environment     at Socket.socketErrorListener (_http_client.js:402:9)
environment     at Socket.emit (events.js:200:13)
environment     at Socket.EventEmitter.emit (domain.js:471:20)
environment     at emitErrorNT (internal/streams/destroy.js:91:8)
environment     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
environment     at processTicksAndRejections (internal/process/task_queues.js:84:9) +0ms
environment FetchError: request to http://localhost:4466/management failed, reason: connect ECONNREFUSED 127.0.0.1:4466
environment     at ClientRequest.<anonymous> (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/node-fetch/lib/index.js:1393:11)
environment     at ClientRequest.emit (events.js:200:13)
environment     at ClientRequest.EventEmitter.emit (domain.js:471:20)
environment     at Socket.socketErrorListener (_http_client.js:402:9)
environment     at Socket.emit (events.js:200:13)
environment     at Socket.EventEmitter.emit (domain.js:471:20)
environment     at emitErrorNT (internal/streams/destroy.js:91:8)
environment     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
environment     at processTicksAndRejections (internal/process/task_queues.js:84:9) +3ms
command Failed to fetch server version +0ms
command TypeError: Cannot read property 'replace' of null +0ms
environment FetchError: request to http://localhost:4466/management failed, reason: connect ECONNREFUSED 127.0.0.1:4466
environment     at ClientRequest.<anonymous> (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/node-fetch/lib/index.js:1393:11)
environment     at ClientRequest.emit (events.js:200:13)
environment     at ClientRequest.EventEmitter.emit (domain.js:471:20)
environment     at Socket.socketErrorListener (_http_client.js:402:9)
environment     at Socket.emit (events.js:200:13)
environment     at Socket.EventEmitter.emit (domain.js:471:20)
environment     at emitErrorNT (internal/streams/destroy.js:91:8)
environment     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
environment     at processTicksAndRejections (internal/process/task_queues.js:84:9) +4ms
environment FetchError: request to http://localhost:4466/management failed, reason: connect ECONNREFUSED 127.0.0.1:4466
environment     at ClientRequest.<anonymous> (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/node-fetch/lib/index.js:1393:11)
environment     at ClientRequest.emit (events.js:200:13)
environment     at ClientRequest.EventEmitter.emit (domain.js:471:20)
environment     at Socket.socketErrorListener (_http_client.js:402:9)
environment     at Socket.emit (events.js:200:13)
environment     at Socket.EventEmitter.emit (domain.js:471:20)
environment     at emitErrorNT (internal/streams/destroy.js:91:8)
environment     at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
environment     at processTicksAndRejections (internal/process/task_queues.js:84:9) +1ms
Error: Could not connect to server at http://localhost:4466. Please check if your server is running.
at Deploy.<anonymous> (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_mod 
ules/prisma-cli-core/src/commands/deploy/deploy.ts:137:13)
at step (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/prisma-cli-core/dist/commands/deploy/deploy.js:45:23)
at Object.next (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_modules/prisma-cli-core/dist/commands/deploy/deploy.js:26:53)
at fulfilled (/home/tomonso/.nvm/versions/node/v12.4.0/lib/node_modules/prisma/node_mod 
ules/prisma-cli-core/dist/commands/deploy/deploy.js:17:58)
   at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:25136) [DEP0066] DeprecationWarning: 
OutgoingMessage.prototype._headers is deprecated
 util timed out +0ms
  output Exiting with code: 0 +0ms

1 个答案:

答案 0 :(得分:0)

首先,如果您使用docker logs来检查prisma日志,则会发现很多错误。这里有几个问题:

  1. host.docker.internal仅适用于Mac的docker / Windows的docker。

    如果以上是您的真实情况,那么您想使用host machine's 5432 port访问postgres's 5342 port。如果是这样,则需要在- "5432:5432"中指定端口映射postgres service

    如果您仅在Linux上工作,那么host.docker.internal甚至无法识别。

    实际上,无论您使用的是Windows / mac / linux,都只需将host: host.docker.internal替换为host: postgresprisma container就可以直接访问postgres container,而无需主机干预,您可以参考this,在该页面上搜索User-defined bridges provide automatic DNS resolution between containers以获得更多帮助。

  2. prisma service要设置db数据库时,postgres service甚至还没有启动,您需要定义启动顺序以确保postgres service准备就绪在prisma service之前,您可以参考Control startup and shutdown order in Compose

    但是,如果您寻找它的入口点,那么可以看到下一个:

    bash-4.4# cat start.sh
    #!/bin/bash
    set -e
    /app/prerun_hook.sh
    /app/bin/prisma-local
    

    在这里,/app/prerun_hook.sh是使用此图像进行某些操作的用户的钩子,因此您只需在此文件中添加一些服务启动保证代码即可确保db服务已经启动。

最后,接下来是一个可行的解决方案:

docker-compose.yaml:

version: '3'
services:
  prisma:
    build: .
    restart: always
    ports:
      - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: postgres
            host: postgres
            database: db
            schema: public
            user: prisma
            password: prisma
            ssl: false
            rawAccess: true
            port: '5432'
            migrations: true
    depends_on:
      - "postgres"
  postgres:
    image: postgres:11
    restart: always
     environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
   postgres: ~

Dockerfile:

FROM prismagraphql/prisma:1.34
COPY ./prerun_hook.sh /app/prerun_hook.sh
RUN chmod +x /app/prerun_hook.sh && \
    apk add postgresql-client

prerun_hook.sh:

#!/bin/sh
set -e

until PGPASSWORD="prisma" psql -h postgres -U "prisma" -c '\q'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"

然后,如果检查docker日志,您将看到下一个,这意味着现在可以:

Postgres is up - executing command
No log level set, defaulting to INFO.
[INFO] {} - Starting...
[INFO] {} - Start completed.
[INFO] Initializing workers...
[INFO] Obtaining exclusive agent lock...
[INFO] Obtaining exclusive agent lock... Successful.
[INFO] Successfully started 1 workers.
[INFO] Deployment worker initialization complete.
Server running on :4466