Mongodb not accessible on host when run via docker-compose but works via docker - mongodb

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.

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

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.

Related

Mongo dbshell command history

I am trying to find the location of .dbshell storing, but I can't manage to find.
May I know normally where does it storing?
Environment:
Docker mongo
The server is installing in GCP, I am using debian server and run with ssh to manage the server.

How to run Mongo container on Windows Server 1809

I'm trying to run a mongodb container on Windows Server 1809 but am getting the following error:
docker run --name mongodb --restart unless-stopped -d -p 27017:27017 mymongo
docker: Error response from daemon: hcsshim::CreateComputeSystem bf9bf4086726d00cbcaff1f082e6b1db12bd803f3ba2c2ca7c0db1e5d75aa316: The container operating system does not match the host operating system.
(extra info: {"SystemType":"Container","Name":"bf9bf4086726d00cbcaff1f082e6b1db12bd803f3ba2c2ca7c0db1e5d75aa316","Owner":"docker","VolumePath":"\\\\?\\Volume{452442a7-ac87-4117-8e82-b00a8d87b444}","IgnoreFlushesDuringBoot":true,"LayerFolderPath":"C:\\ProgramData\\docker\\windowsfilter\\bf9bf4086726d00cbcaff1f082e6b1db12bd803f3ba2c2ca7c0db1e5d75aa316","Layers":[{"ID":"fc52f55a-6f1e-5651-93c6-09c1fcaf7c9a","Path":"C:\\ProgramData\\docker\\windowsfilter\\c9035a2f08cd42f11c3c76895b03021b6d48266d0f86bd083c12fa5f86455e60"},{"ID":"ab78d923-9c41-55fc-92ec-a9330d5db560","Path":"C:\\ProgramData\\docker\\windowsfilter\\c01be26c30f8769641fe00741c162d71bb3305df4c71cbaf138ff2b901ec6b11"},{"ID":"026d80b8-19b3-5c4d-ae3d-35a54251fe3e","Path":"C:\\ProgramData\\docker\\windowsfilter\\def72c2b739b9f453610c44a8ed61e997306eaf34a2ecddd00198d0e7a002f97"},{"ID":"3f241ae1-ba2c-5fec-8d70-c2396e176817","Path":"C:\\ProgramData\\docker\\windowsfilter\\c7cedcded6a8aa587cef67edb4918bec61be7a3500892ea1e96ff7b2c3e13af2"},{"ID":"2efd8648-ea16-508d-8c5b-6a0ef3e6c84d","Path":"C:\\ProgramData\\docker\\windowsfilter\\60049935b45a47f9649619a2f5bcd87d9b9a909452c3bd0fbd1a6a7275bcb7b0"},{"ID":"eddd0e96-fd43-58c7-94fc-530892d35234","Path":"C:\\ProgramData\\docker\\windowsfilter\\80f9aa5efa51e2fa9b068a68b4b5dd5a4b10724cdeab62238dfe7d3f123767d1"},{"ID":"11f58e92-5415-5594-9910-9ff4f59731fb","Path":"C:\\ProgramData\\docker\\windowsfilter\\65f94285263b26ecb465ede59d13113b09e2e31891fc832e2700c00a5902c147"},{"ID":"cedb9cc2-9b4c-5175-bd37-f74b69253886","Path":"C:\\ProgramData\\docker\\windowsfilter\\95cb73e37cc8f93d5e63143c0d0de000cc1af77e498108babaf5ceebd09cebf0"},{"ID":"745ef0c5-ae72-5e25-b6bb-7208c533560e","Path":"C:\\ProgramData\\docker\\windowsfilter\\8984fb6ffe0575d27d836a764d64591ae175272a75260ee476507100618d3b78"},{"ID":"b0dcfd28-95d6-506d-bbcf-48e5f62ef97f","Path":"C:\\ProgramData\\docker\\windowsfilter\\e747665dffb09875cdeeb4438a422f2c6a3a83f3d30f1d77001e19a078ec3392"},{"ID":"f2fb9c30-cdca-513e-8409-c21af8fbfcaa","Path":"C:\\ProgramData\\docker\\windowsfilter\\f5641e2fbb273e21e654e8671af314de80a62f5a5074cfdb3a7784f296c6d171"},{"ID":"67e9a990-1ee4-597f-8e73-ee95b6f65d30","Path":"C:\\ProgramData\\docker\\windowsfilter\\e18aba5da9d71b86e113123d6635fcec544f1228b6c29e76b7027ed1d5b207d7"},{"ID":"741cd5e2-3dfe-5693-ba08-ac8fed4ae741","Path":"C:\\ProgramData\\docker\\windowsfilter\\6029438e12ce66ab4f7b48ca5f7aa10ff6d1b284602f2f6fa0ab2b56daa96796"}],"HostName":"bf9bf4086726","MappedDirectories":[{"HostPath":"C:\\ProgramData\\docker\\volumes\\e1898e25db59a589102c266339cc21243e62ba950a70a82f6b3fc31d693f1494\\_data","ContainerPath":"c:\\data\\configdb","ReadOnly":false,"BandwidthMaximum":0,"IOPSMaximum":0,"CreateInUtilityVM":false},{"HostPath":"C:\\ProgramData\\docker\\volumes\\e42d0b5e649c60b87b7ff283b0db51f28d95cf1e9b2ef33ddb50344bf61358b7\\_data","ContainerPath":"c:\\data\\db","ReadOnly":false,"BandwidthMaximum":0,"IOPSMaximum":0,"CreateInUtilityVM":false}],"HvPartition":false,"EndpointList":["A64F960D-CB2B-4452-8438-2A1E25105432"],"AllowUnqualifiedDNSQuery":true}).
I tried with both 4.2.1-windowsservercore-1803 and 4.2.1-windowsservercore-ltsc2016. I also tried using isolation=hyperv, but it gave me "Request is not supported". On another Windows Server, this one 2016, everything worked.
What can I do to run a mongo container on windows server 1809?
I got it working by copying the image for the MongoDB version I wanted on their github and changing the FROM line to
FROM mcr.microsoft.com/windows/servercore:1809

Can't connect Mongo shell to Mongo Atlas M0 using mongodb+srv

I am trying to connect to my MongoDB Atlas Cloud cluster via the mongo+srv connection like so:
mongo "mongodb+srv://cluster0-mhzdc.mongodb.net/test" --username myuser
I am getting this response:
DNSHostNotFound: Failed to look up service "_mongodb._tcp.cluster0-mhzdc.mongodb.net": Undefined error: 0
try 'mongo --help' for more information
I am using the following version of Mongo client:
mongo --version
MongoDB shell version v4.0.5
git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
allocator: system
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
I can't find any resolution online. Any ideas what's wrong? Is this a bug in the given version of the Mongo shell client?
It looks like bug 34117, still unresolved:
https://jira.mongodb.org/browse/SERVER-34117
To work around the bug check if you have a DNS resolver active on your notebook.
On windows:
ipconfig /displayDNS
to see the current DNS resolver cache.
You might even try to erase the cache with the command:
ipconfig /flushdns
and retry.
If you are working on linux ubuntu try the command:
named -v
to check if the DNS resolver software is already installed.
If not:
sudo apt update
sudo apt install bind9 bind9utils bind9-doc bind9-host
to install the needed packages, then start the service:
sudo systemctl start bind9
and retry.
On Mac OSX, the command is:
sudo killall -HUP mDNSResponder;sudo killall mDNSResponderHelper;sudo dscacheutil -flushcache
The same happens to me, but just after i change my internet provider. Before, i could connect to mongo atlas with no problem.
I guess this happens because the DNS resolver of my internet provider could not resolve the uri to connect to mongodb atlas.
The Solution ->
Change de DNS resolver on my PC:
Open the Control Panel.
Click View network status and tasks
Click Change adapter settings on the left portion of the window.
Double-click the icon for the Internet connection you're using.
Click the Properties button.
Click and highlight Internet Protocol Version 4 (TCP/IPv4) and click Properties.
If not already selected, select the Use the following DNS server addresses option.
Enter the new DNS addresses and click OK and close out of all other windows.
I used google public dns 8.8.8.8.
After that i could connect again with my mongo shell ou compass to mongo atlas.
Hope that helps someone..
I also had this problem with Comcast Xfinity. For those running Ubuntu 18.04 or similar, I had to edit (you'll need root permissions) the /etc/dhcp/dhclient.conf file, and add to following line:
supersede domain-name-servers 8.8.8.8, 8.8.4.4;
I hope this helps somebody, took me too long to figure it out. :-)
I spent lot of time to figure the out issue. after the change DNS it worked. thanks
Used google dns servers.
8.8.8.8
8.8.4.4

How to enable access control for MongoDB with Docker (Plesk)

How do I enable access control for my MongoDB server?
This line shows up in the log:
CONTROL [initandlisten] ** WARNING: Access control is not enabled for
the database. I CONTROL [initandlisten] ** Read and write access to
data and configuration is unrestricted.
I use Plesk to start MongoDB and I have read their manual about it
but it doesn't tell me how to enable access control since I can only add environment variables and volume mapping via the Plesk control panel such as /data/db and /data/configdb.
I'm able to use a terminal to login without credentials to create a superadmin and all the roles, but that doesn't solve the unrestricted access.
I can also access files within the container with this:
docker exec -it mongo /bin/bash
And I see this directory: docker-entrypoint-initdb.d
but I don't know what to put there. Also packages that I installed inside the container like vim don't persist and are lost after a restart.
Furthermore I have no idea where Plesk is storing the docker-compose.yml file. Is this a file that only belongs inside a image file?
If I change the /etc/mongod.conf and add this:
security: authorization: enabled
nothing happens after restart, could I get some help with this?
system information:
Plesk Onyx 17.8.11 Update 25
Host: CentOS 7.5.1804
Container: Ubuntu 16.04
Docker version 18.06.1-ce, build e68fc7a
You should read the library/mongo image documentation : https://hub.docker.com/_/mongo/
If you want to use your own mongo configuration file, put it somewhere like /etc/mongo/mongod.conf in your container (with volume or COPY).
After that you have to tell to mongo to use it as configuration file :
$ docker run --config /etc/mongo/mongod.conf
Or in docker-compose :
services:
mongo:
image: mongo
command: --config /etc/mongo/mongod.conf
I can't help you for the location of Plesk docker-compose.yml, try to use find command.
I hope it will help you.

Docker container mongod error when starting via ssh

I have installed mongodb on a docker container together with openssh on ubuntu 14.04. The container is running with ssh but when I ssh into the container I get the following error when trying to start mongod.
root#430f9502ba2d:~# service mongod start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service mongod start
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start mongod
Also start mongod does not affect anything.
Tried looking at this also Mongo daemon doesn't run by service mongod start without it helping.
mongod --config /your/path/to/mongod.conf doesn't seem to work also, just locks up.
The error below is standard as of course there is no mongod server running.
root#430f9502ba2d:/# mongo
MongoDB shell version: 2.6.9
connecting to: test
2015-05-07T20:49:56.213+0000 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-05-07T20:49:56.214+0000 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
The problem here is your approach. Docker does not have an init system like you are used to on traditional systems. What docker does is replace PID 1 with the process you specify in the CMD or ENTRYPOINT Dockerfile commands. For now, ignore ENTRYPOINT, because it replaces what your CMD is run with (normally, it's /bin/sh -c). You need to instruct docker to start your mongod service in your Dockerfile with the CMD command, like:
CMD usr/bin/mongod
And when you run your container, mongod will be your PID 1. Now, you're probably wondering at this point "But what about my SSH server?" and the answer is: Don't run an SSH server on your docker containers. There are some use cases where running an SSH server is okay, but almost all of the "normal" reasons (debug, C&C, etc) are nullified with the "best practice" for getting a shell on your container:
docker exec -it myContainer /bin/bash
This will drop you into a shell on your running container. The recommendation here for managing configuration and changes in your docker container is to use something like Ansible. However, remember that docker containers are ephemeral, and you shouldn't be restarting services and changing configuration state on them. If you need a config change, change the Dockerfile or config data, and then start a new container. Good luck! Here is a little more information on Dockerizing MongoDB, but keep in mind that the method described there alters the ENTRYPOINT in the Dockerfile, which is a little more involved and requires a better understanding of what's going on in Dockerfiles.
This is really helpful. I was trying to make old Ansible playbooks work with Docker by creating several blank containers and let Ansible do the rest.
It works through command
mongod --dbpath /var/lib/mongodb --smallfiles