我有一个mongodb实例在主机服务器上的容器内运行(例如host1
)。 docker端口已从容器的端口映射到主机端口。就我而言,27017
。配置如下。
我有一个在另一个容器中运行的webapp。如果将webapp容器放在另一台服务器上,例如host2
,则它可以访问该mongodb,但是如果它在host1
上,则不能。由于我希望webapp始终将mogodb视为Internet连接,以允许在mongodb主机之外的任何主机上使用webapp docker,因此我不想将webapp连接通过LAN网络绑定到主机。
telnet
可以运行该mongodb,但不能从host1
上的另一个docker内部进行。有人知道我搞砸了吗?谢谢
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 48
# where to write logging data.
systemLog:
destination: file
logAppend: false
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIpAll: true
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
对于docker端口映射:
0.0.0.0:27017->27017/tcp, 0.0.0.0:27017->27017/udp
修改:
我使用来自host1
和host2
的mongoose和mongo连接字符串。
docker网络驱动程序为Overlay
。
这是webapp的docker网络配置:
{
"Name": "clientA_webnet",
"Id": "pr4i54oqo6snwc9jev2zs4tq2",
"Created": "2019-07-17T10:00:55.361594601+07:00",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "10.0.6.0/24",
"Gateway": "10.0.6.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"19cf98a368b5fe515eaaba90e7e036d26925af06196beb1027efa1d982778bb6": {
"Name": "clientA_webapp_container",
"EndpointID": "1a326fc3226cb3b9dc85c3426ca9cfdf8e4543366b07e67e83c10b7f924ab538",
"MacAddress": "02:42:0a:00:06:05",
"IPv4Address": "10.0.6.5/24",
"IPv6Address": ""
},
"3bf158bbc7237a98c4607049b8955b7e25b3e0cbde63f7165f7c6d1f86d5f5a2": {
"Name": "clientA_redis_container",
"EndpointID": "4f294d55f27bb02e9adaac27658b2c7d28bf0a1b5d397c8e5a905e896925db10",
"MacAddress": "02:42:0a:00:06:04",
"IPv4Address": "10.0.6.4/24",
"IPv6Address": ""
},
"7d4cfe90ad1d5b29aef135695a517af17c80a5602008fe2125152c0892242e54": {
"Name": "clientA_nginx_container",
"EndpointID": "18a8d5f2c48f7e056585f302c0e1f654e170fac13ef033f2ecd3a0477bd74d57",
"MacAddress": "02:42:0a:00:06:06",
"IPv4Address": "10.0.6.6/24",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4103"
},
"Labels": {},
"Peers": [
{
"Name": "8d418ecd9f27",
"IP": "x.x.x.x"
}
]
}
下面是mongodb网络配置
{
"Name": "common_net",
"Id": "e2c0a6261df67d429e24ae3ac4da82dd6604fb7e1a3d788ce81199391e1d0291",
"Created": "2019-03-04T11:12:19.332798004+07:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.27.0.0/16",
"Gateway": "172.27.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"cdca734916358983fd77f4b9b64434d163a67f146c0c152bdb178502db89d37b": {
"Name": "common_mongo_container",
"EndpointID": "253a7ca61a065de90912aaa8b9dc76f0d491aa10bb93ecba4a71d6417f48a3c4",
"MacAddress": "02:42:ac:1b:00:0a",
"IPv4Address": "172.27.0.10/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
下面是我连接到mongodb的方式,其中x.x.x.x
是服务器的真实Internet IP地址(在本例中为host1
)
mongodb: 'mongodb://username:password@x.x.x.x:27017/dashboard'