У меня есть веб-приложение, которое использует Redis и Mon go. Это приложение развернуто в разных местах одновременно, и я изучаю docker, чтобы упростить некоторые настройки. Веб-приложение и Redis работают в сетевых контейнерах docker. Mon go работает либо на хосте, либо на другом сервере полностью.
У меня проблемы с получением контейнера webapp для подключения к mon go. Когда все три содержатся в сетевых контейнерах, это не проблема.
Вот мой docker -compose.yml, настраивающий webapp и redis:
version: '3'
services:
redis:
image: redis:3.2.1
container_name: redis
restart: always
networks:
net1:
ipv4_address: 172.19.0.2
webapp:
image: briyo2289/phythings-webapp:latest
ports:
- "80:8080"
container_name: webapp
restart: always
depends_on:
- redis
networks:
net1:
ipv4_address: 172.19.0.4
env_file:
- .env
networks:
net1:
ipam:
driver: default
config:
- subnet: "172.19.0.0/24"
My Mon go код подключения является стандартным:
return MongoClient.connect(dbUrl, (err, db) => {
if (err) {
throw err
для dbUrl
Я пробовал 127.0.0.1, который я не ожидал, потому что контейнер не является локальным. Я также попытался использовать IP-адрес publi c с портом 27017. В дополнение к IP-адресу publi c я отредактировал файл mongodb.conf
так, чтобы bind_ip был равен 0.0.0.0 вместо 127.0.0.1. (Я не совсем уверен, что это сделало, но это было рекомендовано как решение в других местах.) В любом случае, это не сработало.
Когда я запускаю docker-compose up
, я получаю эту ошибку: Error [MongoError]: failed to connect to server [45.xxx.xxx.xxx:27017]
Я не уверен, что проблема в том, что mon go не хочет удаленного подключения или если docker "сбивает с толку" мое веб-приложение, и я пытаюсь подключиться к неправильной вещи.
Любой совет очень ценится.
РЕДАКТИРОВАТЬ: Вот файл mongodb.conf
# mongodb.conf
# Where to store the data.
dbpath=/var/lib/mongodb
#where to log
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 45.xx.xx.xx
port = 27017
# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#oplog = 0
# Diagnostic/debugging option
#nocursors = true
# Ignore query hints
#nohints = true
# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Accout token for Mongo monitoring server.
#mms-token = <token>
# Server name for Mongo monitoring server.
#mms-name = <server-name>
# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>
# Replication Options
# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com
# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>
# SSL options
# Enable SSL on normal ports
#sslOnNormalPorts = true
# SSL Key file and password
#sslPEMKeyFile = /etc/ssl/mongodb.pem
#sslPEMKeyPassword = pass
Единственное, что я изменил в этом файл bind_ip
, и я удалил комментарий port
прямо под ним.