无法使用 docker 创建 mongo 数据库

Cannot create a mongo database with docker

我在使用 docker-compose 命令创建 mongo 数据库时遇到问题。 Docker 桌面告诉我一切都已启动 运行ning 包括数据库,但我得到的只是标准 'admin, config, local' 而不是我想要创建的数据库。这是我的 docker-compose.yaml

version: '3'

services:

  app:
    build: ./
    entrypoint: ./.docker/entrypoint.sh
    ports:
    - 3000:3000
    volumes:
    - .:/home/node/app
    depends_on:
    - db

  db:
    image: mongo:4.4.4
    restart: always
    volumes:
      - ./.docker/dbdata:/data/db
      - ./.docker/mongo:/docker-entrypoint-initdb.d
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=root
      - MONGO_INITDB_DATABASE=nest
  
  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_SERVER=db
      - ME_CONFIG_MONGODB_AUTH_USERNAME=root
      - ME_CONFIG_MONGODB_AUTH_PASSWORD=root
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=root
    depends_on:
    - db

我的init.js里面.docker/mongo

db.routes.insertMany([
  {
    _id: "1",
    title: "Primeiro",
    startPosition: {lat: -15.82594, lng: -47.92923},
    endPosition: {lat: -15.82942, lng: -47.92765},
  },
  {
    _id: "2",
    title: "Segundo",
    startPosition: {lat: -15.82449, lng: -47.92756},
    endPosition: {lat: -15.82776, lng: -47.92621},
  },
  {
    _id: "3",
    title: "Terceiro",
    startPosition: {lat: -15.82331, lng: -47.92588},
    endPosition: {lat: -15.82758, lng: -47.92532},
  }
]);

和我的docker文件

FROM node:14.18.1-alpine

RUN apk add --no-cache bash

RUN npm install -g @nestjs/cli

USER node

WORKDIR /home/node/app


这是我从 docker 获得的 'error' 日志,当我 运行 带有 mongodb、nest app 和 mongo express 的嵌套容器时(实际上还有很多,但 SO keeps 出于某种原因认为它是垃圾邮件。

about to fork child process, waiting until server is ready for connections.


Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory

{"t":{"$date":"2022-06-01T19:39:15.542+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:39304","connectionId":2,"connectionCount":0}}


/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.js

{"t":{"$date":"2022-06-01T19:39:15.683+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:39310","connectionId":3,"connectionCount":1}}

{"t":{"$date":"2022-06-01T19:39:15.684+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn3","msg":"client metadata","attr":{"remote":"127.0.0.1:39310","client":"conn3","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.4"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"18.04"}}}}

{"t":{"$date":"2022-06-01T19:39:15.701+00:00"},"s":"I",  "c":"STORAGE",  "id":20320,   "ctx":"conn3","msg":"createCollection","attr":{"namespace":"nest.routes","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"f689868e-af6d-4ec6-b555-dcf520f24788"}},"options":{}}}

{"t":{"$date":"2022-06-01T19:39:15.761+00:00"},"s":"I",  "c":"INDEX",    "id":20345,   "ctx":"conn3","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"nest.routes","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}

uncaught exception: ReferenceError: colection is not defined :

@/docker-entrypoint-initdb.d/init.js:23:1

failed to load: /docker-entrypoint-initdb.d/init.js

exiting with code -3



这就是 运行ning docker-compose ps 显示的内容

NAME                       COMMAND                  SERVICE             STATUS              PORTS     
nest-api-app-1             "./.docker/entrypoin…"   app                 running             0.0.0.0:3000->3000/tcp
nest-api-db-1              "docker-entrypoint.s…"   db                  running             27017/tcp 
nest-api-mongo-express-1   "tini -- /docker-ent…"   mongo-express       running             0.0.0.0:8081->8081/tcp

this what my docker desktop shows

MongoDB 容器仅在不存在数据库时才创建数据库。您可能已经有了一个,这就是为什么没有创建新数据库并且您的初始化脚本不是 运行.

的原因

删除主机上./.docker/dbdata的内容。然后使用 docker-compose 启动容器,Mongo 应该会为您创建数据库。