3

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.

2 Answers 2

4

Ok, the problem is super stupid and simple.

ports: - "27017:21017"

second port 21017 instead of 27017. How to waste full day due to typo

Sign up to request clarification or add additional context in comments.

1 Comment

lmao this just happened to me -.-
-1

If you are on linux it works fine. When you run container in windows you need to do port farwarding.

netsh interface portproxy add v4tov4 listenport=27017 listenaddress=localhost connectport=27017 connectaddress=192.168.99.100(IP of the Docker)

Whatever the call comming to loopback it will get redirected to container with the docker host.

Here is the helping discussion on port farwarding in windows with docker Solution for Windows hosts

This lines I'm using docker for windows that is run via hyper-v., say that you ae on Windows and docker will not be the loopback(127.0.0.1), you have to forward the port.

8 Comments

You are clearly missing the point. Like i write in the post port sharing work as it is. And ip of my docker is 127.0.0.1 so i dont need to forward ports on my windows. Port sharing even works when i run via docker, it only doesnt work via docker-compose for mongodb ( works for other apps like postgresql, nginx etc)
Are you clear about loopback when container runs on docker in windows. Please check the anser updated
yes. like i said it works fine with other services or with docker itself. And i updated question. Exactly same behavior on linux too, so its not specific to windows at all
and docker on windows via hyper-v runs on loopback just so you know. Its only docker-machine that runs on different ip.
github.com/moby/moby/issues/15740#issuecomment-200409378 check with this. Running container in linux and windows is different. because docker container runs completly on different machine having different ip not on the Windows host. Kernel sharable is not possible for WIndows for docker possible for ubuntu
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.