I`m trying to connect to mongodb in docker container from a host ( for dev purposes solely) without any luck. I have following docker-compose file.
version: '2.2'
services:
mongo:
image: mongo
ports:
- "27017:21017"
And when i try to connect to it i'm getting following error:
C:\Program Files\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
2017-10-25T03:48:53.331+0300 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:27017' :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
But if i launch it without compose directly e.g.
docker run --name my_mongox6 -d -p 27017:27017 mongo
Which should be equivalent of the docker-compose file above. And it works perfectly fine
C:\Program Files\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2017-10-25T00:49:58.983+0000 I STORAGE [initandlisten]
2017-10-25T00:49:58.983+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-10-25T00:49:58.983+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-10-25T00:49:59.076+0000 I CONTROL [initandlisten]
2017-10-25T00:49:59.076+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-25T00:49:59.076+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-10-25T00:49:59.076+0000 I CONTROL [initandlisten]
Versions
c:\Program Files\MongoDB\Server\3.4\bin>docker --version
Docker version 17.09.0-ce, build afdb6d4
c:\Program Files\MongoDB\Server\3.4\bin>docker-compose --version
docker-compose version 1.16.1, build 6d1ac219
I'm using docker for windows that is run via hyper-v.
Whats wrong with my compose file and why it works via docker run but doesn't work via docker-compose. I'm scratching my head and googling without any luck for hours
Other port sharing via docker-compose ( e.g. nginx, postgresql) works perfectly fine.
UPDATE
Asked friend on ubuntu to try. Same behaviour. So its reproducible and its not specific to windows at all.. Works via docker, doesnt work via docker-compose. Even when i try to connect via telnet mongo logs show connect attempts when running via docker, but nothing shown when run via compose.
Also I see a difference in docker ps when run via compose vs when run via docker.
Ports on compose listed as
( 27017/tcp, 0.0.0.0:27017->21017/tcp )
vs
(0.0.0.0:27017->21017/tcp )
on docker itself.
It missed first 27017/tcp part which might be the issue, but no idea why.