Локальная оболочка Mongo не подключается к серверу mongo, работающему в docker

#mongodb #docker #centos7 #mongo-shell

#mongodb #docker #centos7 #mongo-shell

Вопрос:

Я запустил сервер mongo в docker, и журналы появились просто отлично.

 2019-04-12T10:39:51.334 0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8a03346e57d7
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] db version v3.2.22
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] git version: 105acca0d443f9a47c1a5bd608fd7133840a58dd
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] OpenSSL version:            OpenSSL 1.0.1e-fips 11 Feb 2013
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] allocator: tcmalloc
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] modules: none
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] build environment:
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten]     distmod: rhel70
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-04-12T10:39:51.335 0000 I CONTROL  [initandlisten] options: { net: { port: 27017 }, processManagement: { pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/data/db" } }
2019-04-12T10:39:51.339 0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),
2019-04-12T10:39:51.389 0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-04-12T10:39:51.389 0000 I CONTROL  [initandlisten] 
2019-04-12T10:39:51.406 0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2019-04-12T10:39:51.406 0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-04-12T10:39:51.406 0000 I NETWORK  [initandlisten] waiting for connections on port 27017
  

Но при подключении сервера mongodb из локальной оболочки время ожидания соединения истекает.

 ~/mongodb/bin/mongo --host 172.17.0.2
MongoDB shell version v3.6.11
connecting to: mongodb://172.17.0.2:27017/?gssapiServiceName=mongodb
2019-04-12T16:04:45.750 0530 W NETWORK  [thread1] Failed to connect to      172.17.0.2:27017 after 5000ms milliseconds, giving up.
2019-04-12T16:04:45.753 0530 E QUERY    [thread1] Error: couldn't connect to server 172.17.0.2:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed
  

где 172.17.0.2 — это IP-адрес, полученный при запуске docker inspect <container-id

Я смог подключить оболочку mongo с помощью docker с помощью команды docker exec --it <container-id> bash . Вот файл Docker для справки.

 RUN echo -e "
[mongodb]n
name=MongoDB Repositoryn
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.2/x86_64/n
gpgcheck=0n
enabled=1n" >> /etc/yum.repos.d/mongodb.repo

# Install mongodb
RUN yum update -y amp;amp; yum install -y mongodb-org

# Set up directory requirements
RUN mkdir -p /data/db /var/log/mongodb /var/run/mongodb
VOLUME ["/data/db", "/var/log/mongodb"]

# Expose port 27017 from the container to the host
EXPOSE 27017

# Start mongodb
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--port", "27017", "--dbpath", "/data/db", "--pidfilepath", "/var/run/mongodb/mongod.pid"]
  

Дайте мне знать, если я что-то упустил.
Использование macOS