Prisma无法从另一个Docker容器连接localhost:4466

时间:2019-11-05 02:42:43

标签: javascript node.js docker docker-compose prisma-graphql

从昨天开始,我正尝试将以下内容一起进行dockerize,因为我使用了docker-compose 其中包含以下定义:

docker.compose.yml

version: '3'
networks:
  network:
    driver: bridge
services:
  server:
    container_name: truckpeserver
    restart: always
    build: .
    networks:
      - network
    links:
      - redis
    ports:
      - '3000:3000'
  redis:
    container_name: "redisserver"
    image: redis:latest
    networks:
      - network
    restart: always
    command: ["redis-server", "--bind", "redis", "--port", "6379"]
  prisma:
    image: prismagraphql/prisma:1.34
    restart: always
    ports:
      - '4466:4466'
    networks:
      - network
    environment:
      PRISMA_ENDPOINT: http://prisma:4466/
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: mysql
            host: mysql
            port: 3306
            user: root
            password: prisma
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: prisma
    volumes:
      - mysql:/var/lib/mysql
volumes:
  mysql: ~

和用于运行我的graphql服务器的dockerfile如下:

# install node.js
FROM node:10.15.3

# create necessary directories and
# permissions
RUN mkdir -p /home/node/truckpeserver/node_modules && chown -R node:node /home/node/truckpeserver

# switch to working directory
WORKDIR /home/node/truckpeserver

# copy package.json files in directory
COPY package*.json ./

# check and switch to node user.
USER node

# install node_modules.
RUN npm install

# remove duplicate packages
RUN npm dedupe

# copy project files.
COPY --chown=node:node . .

# build server
RUN npm run build

# start server
CMD ["npm", "start"]

问题:

除棱镜外,一切正常。当我每个执行代码的查询棱镜时,我都收到如下错误:

错误

truckpeserver | FetchError: request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466
truckpeserver |     at ClientRequest.<anonymous> (/home/node/truckpeserver/node_modules/cross-fetch/node_modules/node-fetch/lib/index.js:1393:11)
truckpeserver |     at ClientRequest.emit (events.js:189:13)
truckpeserver |     at ClientRequest.EventEmitter.emit (domain.js:441:20)
truckpeserver |     at Socket.socketErrorListener (_http_client.js:392:9)
truckpeserver |     at Socket.emit (events.js:189:13)
truckpeserver |     at Socket.EventEmitter.emit (domain.js:441:20)
truckpeserver |     at emitErrorNT (internal/streams/destroy.js:82:8)
truckpeserver |     at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
truckpeserver |     at process._tickCallback (internal/process/next_tick.js:63:19)

我已经尝试过的东西。

  1. 链接
  2. 创建网络:桥梁

一切都失败了,prisma无法正常工作,请帮忙

1 个答案:

答案 0 :(得分:1)

删除将与网络相关的任何其他配置链接为docker-compose会解决这个问题。在服务之间的通信中,您可以使用容器名称来进行从一个容器到另一个容器的某些调用。

现在该错误来自您的truckpeserver容器,

truckpeserver | FetchError: request to http://localhost:4466/ failed, reason: connect ECONNREFUSED 127.0.0.1:4466

您需要将localhost更改为prisma:4466。当您在某个容器中使用本地主机时,它将指向该容器的本地主机,则无法使用本地主机访问其他容器。

从错误日志来看,您似乎正在使用交叉获取?然后您可以尝试

fetch('//prisma:4466')
  .then(res => {
    if (res.status >= 400) {
      throw new Error("Bad response from server");
    }
    return res.json();
  })
....