im trying to connect to a mongodb container, but it does not work, does not recognize the host.
this is my docker-compose.yml file with the service:
version: '3'
services:
mongodb:
build:
context: .
dockerfile: docker/mongodb/Dockerfile
restart: always
environment:
DATABASE: database
USERNAME: user
PASSWORD: pass
ports:
- '5100:27017'
volumes:
- ./data/db:/data/db
This is my dockerfile for mongodb:
FROM mvertes/alpine-mongo:4.0.5-0
ENV MONGO_INITDB_DATABASE: ${DATABASE}
ENV MONGO_INITDB_ROOT_USERNAME: ${USERNAME}
ENV MONGO_INITDB_ROOT_PASSWORD: ${PASSWORD}
COPY ./docker/mongodb/init.js /docker-entrypoint-initdb.d
CMD ["mongod"]
EXPOSE 27017
I am trying to establish the connection from spring, but can not find the service, this I have in my properties:
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=5100
spring.data.mongodb.username=user
spring.data.mongodb.password=pass
spring.data.mongodb.database=database
and also i am trying to establish the connection with robo3T, and the problem is the same:
Address: mongodb://mongodb
port:5100
The error in spring say:
com.mongodb.MongoSocketException: mongodb: unknown name or service
and in robo3T say:
Cannot connect to the MongoDB at mongodb:5100.
Error:
Network is unreachable.
what am I doing wrong?
EDIT: my container log:
2019-03-16T16:22:00.714+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=1220eaf96d91
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] db version v4.0.5
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1a 20 Nov 2018
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] allocator: system
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] modules: none
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] build environment:
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] distarch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL [initandlisten] options: {}
2019-03-16T16:22:00.760+0000 I STORAGE [initandlisten]
2019-03-16T16:22:00.760+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-16T16:22:00.760+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-16T16:22:00.768+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1432M,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),statistics_log=(wait=0),verbose=(recovery_progress),
2019-03-16T16:22:01.523+0000 I STORAGE [initandlisten] WiredTiger message [1552753321:523789][1:0x7f9d46a64ca8], txn-recover: Set global recovery timestamp: 0
2019-03-16T16:22:01.562+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten]
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten]
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-03-16T16:22:01.623+0000 I CONTROL [initandlisten]
2019-03-16T16:22:01.638+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: 74fe0f10-e6a8-439c-9548-a2cdd18df3c7
2019-03-16T16:22:01.688+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2019-03-16T16:22:01.700+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 0d6bdbd7-4b15-4ad1-8ded-fa28b89ae871
2019-03-16T16:22:01.762+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-03-16T16:22:01.765+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2019-03-16T16:22:01.825+0000 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: b5462e15-4724-4b1f-8537-57d9ebb1a2e8
2019-03-16T16:22:01.902+0000 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-03-16T16:22:01.902+0000 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-03-16T16:22:01.905+0000 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
This part of docker-compose.yml
means you want to expose the container which works on port 27017
to the public on port 5100
so anyone can access it either localhost or public ip, but the word mongodb
as a host will be only recognizable between containers inside a docker network so if you have another container within the same network it can reach mongodb on mongodb
as a hostname and port 27017
ports:
- '5100:27017'
Based on the mongodb logs you have the following problem:
** WARNING: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP
So you need to start mongodb
with --bind_ip 0.0.0.0
so you can reach it from outside the container. Change your CMD
to the following:
CMD [ "mongod", "--bind_ip", "0.0.0.0" ]
Or remove the CMD
from your Dockerfile and it will fallback to the original CMD
of the base image which is the same as above .
Starting in MongoDB 3.6, mongod bind to localhost by default. See Default Bind to Localhost .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.