gpt4 book ai didi

mongodb - 无法在Docker容器启动时创建Mongodb数据库

转载 作者:行者123 更新时间:2023-12-02 19:16:44 24 4
gpt4 key购买 nike

我试图用它和它自己的新用户中的集合初始化Mongodb数据库。
我已遵循以下SO链接,但无法从主机连接到数据库。
如果我打开Mongodb Shell,则无法找到新创建的数据库和用户。
场景1
Docker文件

FROM mongo:latest

ENV MONGO_INITDB_ROOT_USERNAME admin-user
ENV MONGO_INITDB_ROOT_PASSWORD admin-password
ENV MONGO_INITDB_DATABASE admin

ADD mongo-init.js /docker-entrypoint-initdb.d/
mongo-init.js
db.auth('admin-user', 'admin-password')

db = db.getSiblingDB('test-database')

db.createUser({
user: 'test-user',
pwd: 'test-password',
roles: [
{
role: 'readWrite',
db: 'test-database',
},
],
});

db.foo.insert({ x: 1, y: 1 })
我有linux ubuntu。我使用了以下命令:
docker image build -t image1:1.0 .
docker run -d -p 27017-27019:27017-27019 --name image1 mongo
对于上述情况, Neo Anderson答案非常有效。现在我有另一种情况:
方案2:
如果我尝试从容器外部与我的容器Mongodb连接,则无法连接。无法连接到 172.20.1.100:37019。尽管我将run命令也更改为 run -d -p 37017:27017-27019

最佳答案

问题似乎出在您的docker run命令中。
您正在构建镜像image1:1.0,但是正在使用mongo镜像运行容器。--name定义容器名称。 docker run命令的最后一个参数引用要使用的图像。
运行容器:

docker run -d -p 27017-27019:27017-27019 --name my-awesome-container image1:1.0
列出容器:,您需要 containerID才能在下一步中打开控制台:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b48ba39b0d38 image1:1.0 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:27017-27019->27017-27019/tcp my-awesome-container
连接到我的超赞容器:
docker exec -ti b48ba39b0d38 sh
使用以下命令连接到数据库:(在要求输入密码时使用admin-password)
mongo --username admin-user
MongoDB shell version v4.2.8
Enter password:
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("17b28c1e-edb7-4c41-93bd-bdc3abcb47a6") }
MongoDB server version: 4.2.8
Server has startup warnings:
2020-07-30T12:20:09.577+0000 I STORAGE [initandlisten]
2020-07-30T12:20:09.577+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-07-30T12:20:09.577+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

>
列出数据库:
> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
test-database 0.000GB
享受使用通过 mongo-init.js创建的测试数据库的乐趣

关于mongodb - 无法在Docker容器启动时创建Mongodb数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63172735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com