KeystoneJS docker-compose无法连接mongo db

时间:2019-12-20 12:32:19

标签: mongodb docker docker-compose keystonejs

我正在尝试在服务器上部署keystoneJS应用。在此之前,我想在本地进行测试。

我在本地启动了mongoDB。然后在这里使用文档https://www.keystonejs.com/guides/deployment中的Dockerfile,我无法运行它。我收到以下错误:

✖ Connecting to database
Error: Server selection timed out after 30000 ms
    at /home/node/node_modules/@keystonejs/utils/dist/utils.cjs.prod.js:54:26
    at async executeDefaultServer (/home/node/node_modules/@keystonejs/keystone/bin/utils.js:109:3) {
  errors: {
    MongooseAdapter: MongoTimeoutError: Server selection timed out after 30000 ms
        at Timeout._onTimeout (/home/node/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9)
        at listOnTimeout (internal/timers.js:531:17)
        at processTimers (internal/timers.js:475:7) {
      name: 'MongoTimeoutError',
      reason: [MongoNetworkError],
      [Symbol(mongoErrorContextSymbol)]: {}
    }
  }
}
error Command failed with exit code 1.

我用Google搜索似乎不了解本地mongodb://localhost:27017

然后我决定使用docker-compose:

这是 docker-compose.yml


version: '3'
services:
  app:
    container_name: my-admin
    restart: always
    build: .
    volumes:
        - .:/mycode
    environment:
        - MONGO_URI=mongodb://mongo:27017
    ports:
    - "80:3030"
    links:
        - mongo
  mongo:
        image: mongo:latest
        restart: always
        ports:
            - "27017:27017"

run docker-compose up遇到相同的错误。

也尝试过:

const keystone = new Keystone({
  name: PROJECT_NAME,
  adapter: new Adapter({mongoUri: "mongodb://mongo:27017/myapp"}),
  onConnect: initialiseData,
});

有帮助吗?谢谢!

编辑

这是 Dockerfile

# https://docs.docker.com/samples/library/node/
ARG NODE_VERSION=12.10.0
# https://github.com/Yelp/dumb-init/releases
ARG DUMB_INIT_VERSION=1.2.2

# Build container
FROM node:${NODE_VERSION}-alpine AS build
ARG DUMB_INIT_VERSION

WORKDIR /home/node

RUN apk add --no-cache build-base python2 yarn && \
    wget -O dumb-init -q https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64 && \
    chmod +x dumb-init
ADD . /home/node
RUN yarn install && yarn build && yarn cache clean

# Runtime container
FROM node:${NODE_VERSION}-alpine

WORKDIR /home/node

COPY --from=build /home/node /home/node

EXPOSE 3000
CMD ["./dumb-init", "yarn", "start"]

0 个答案:

没有答案