Here, What I meant local means my system database, and also other system which is connected by LAN. In my office all systems are connected by LAN. So I want to access the mongodb which is running in the system… Because, we need to connect mongodb for dynamic ip address. that is I want to connect my system and others system in my office(LAN connected system.) My system also connected with LAN
ok, still unclear…
you could draw a diagram, but I think i got it…
on your windows machine, you are running mongodb, and there are other systems in the lan network also running mongo db.
you will start a docker instance which wants to connect to those databases…
the docker instance (container), will have a unique network IP address, different from all the other systems, including your windows machine.
so, to connect to a mongo db from this container, you need to
1.every mongo DB (server) MUST be configured to allow connection from a client application on a different IP address than the mongo db server. you can do this in the mongodb.cnf or from the mongod command using --bind_ip_all
2.your docker instance must know the resolvable name or IP address of the mongoDB server(s) it wishes to connect to…
note: the container is NOT on the regular lan DNS system so you will have to provide some alternate mechanism
(like write a file on a docker instance volume that contains the ip address or name/ip address of the
systems, or use the --add_host docker run parameter to specify those name/ip address mappings … this –
add_host updates the containers hosts file (which u cannot edit any other way). you can specify multiple –
add_host operand on the docker run. (but cannot ADD additional entries AFTER docker run starts the
container)
-
the client app uses the syntax server_IP_address_or_name:27017 to connect to the remote from itself database.
-
on your windows machine, or any other machine, running the mongodb server, you may have to open the firewall to permit incoming connections. (windows has this off by default as I recall
yes. @sdetweil…
I done 1 solution which you provide. In mongod.config I gave bind-ip:0.0.0.0
- I am not getting 2 solution can you share the file
are you saying about docker-compose file?
I did one docker-compose .yml file in that I have mentioned the volumes,port and image name.
version : ‘3’
services :
web:
image: nitikishu/sampledocker
ports:
- “4200:4200”
depends_on:
- mongo
mongo:
image: mongo:3.4.2
ports:
- “27017:27017”
volumes:
- data:/data/db
volumes:
data:
is the way to achieve the 2nd solution?. or else can you send a sample file and connection path of (mongodb-uri) to connect to mongodb
i do not use docker-compose, but supposedly that is one way to connect containers.
i was just pointing out that YOU (via docker run parameters, or compose file statements) have to supply the info for the containers to know about each other…
the command
docker run -d -name web --add-host mongo_db_container_name:mongo_db_container_ip_address ... nitikishu/sampledocker
will add the mongo_db_container_name mongo_db_container_ip_address information to the web container hosts file, such that ping mongo_db_container_name will work correctly, and u can use the db connection string
mongo_db_container_name:27017
in the web container without having to code the ip address separately.
I prefer NOT to use docker-compose myself, as I like to understand HOW to build the connection between systems(containers)…
none of this is unique… you would have to do the same configuration and setup if these were actual machines.
I am using docker-compose.yml file to connect with application container. in that compose file I am giving port number , image name and volumes
version : ‘3’
services :
sampledocker:
build: .
ports:
- “4200:4200”
depends_on:
- mongo
mongo:
image: mongo:3.4.2
ports:
- “27017:27017”
volumes:
- data:/data/db
volumes:
data:
using this document I will giving the path as
mongoose.connect(“mongodb://mongo:27017/dbname”)
But still I am unable to login the system
here what is the host ? example
docker run -d -name nitikishu/sampledocker --add 192.168.99.100 mongo:172.0.0.1 nitikishu/sampledocker
this is right?
ok, you’ve been at this a long time… lets drop back to basics, and make sure THAT works…
from https://hub.docker.com/_/mongo/
start a mongo instance
$ docker run --name some-mongo -d mongo
This image includes EXPOSE 27017 (the mongo port), so standard container linking will make it automatically available to the linked containers (as the following examples illustrate).
connect to it from an application
...
... or via mongo client
$ docker run -it --link some-mongo:mongo --rm mongo sh -c 'exec mongo "$MONGO_PORT_27017_T
docker run is for starting a container from an image.
you can pass parameters to help add to the configuration to help the app that will be running, and to help in executing later docker commands…
-name gives the container a symbolic name, so you don’t have to look up the actual id.
–add-host allows you to inject an entry into the containers /etc/hosts file, so the application can be configured to the a symbolic name for a system it needs to connect to, and not need to know the actual ip address.
so, in your case, if the mondodb server is running on your windows (docker host) machine, then you would do
docker run -d --rm -name mycontainer --add-host mongo_server:???? nitikishu/sampledocker
where ???? is the ip address of your windows machine where mongod is running, and you app uses the hostname of **mongo_server** in its db connection string.
user2@system2 MINGW64 ~
$ docker run --name mongo -d mongo
d3854f00b741f4370384aab10dc2b71a2fa31c71da0fc121c70d8844cc115610
user2@system2 MINGW64 ~
$ docker run --name sampledocker --link mongo:mongo -d nitikishu/sampledocker
c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44
user2@system2 MINGW64 ~
$
Now How to check, which command should I use to check the application running in browser window
what is the ip address of container sampledocker?
docker inspect sampledocker
$ docker run -it --link mongo:mongo --rm mongo sh -c ‘exec mongo “$MONGO_PORT_27017_TCP_27017_TCP_PORT/PMS_DB”’
MongoDB shell version v3.6.2
connecting to: mongodb:///PMS_DB
2018-02-06T05:13:30.063+0000 E QUERY [thread1] Error: No server(s) specified :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
wrong parameters…
run
docker exec sampledocker env
to dump out the environment variables set from the --link
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3854f00b741 mongo “docker-entrypoint…” 9 minutes ago Up 8 minutes 27017/tcp mongo
user2@system2 MINGW64 ~
$ docker inspect sampledocker
[
{
“Id”: “c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44”,
“Created”: “2018-02-06T05:07:23.172906499Z”,
“Path”: “npm”,
“Args”: [
“start”
],
“State”: {
“Status”: “exited”,
“Running”: false,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 0,
“ExitCode”: 1,
“Error”: “”,
“StartedAt”: “2018-02-06T05:07:23.650935737Z”,
“FinishedAt”: “2018-02-06T05:07:28.898465874Z”
},
“Image”: “sha256:900709b5a25b4dee86b4349e4a846b18b969d1f6c99bdaa47b3b2f5838a05d95”,
“ResolvConfPath”: “/mnt/sda1/var/lib/docker/containers/c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44/resolv.conf”,
“HostnamePath”: “/mnt/sda1/var/lib/docker/containers/c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44/hostname”,
“HostsPath”: “/mnt/sda1/var/lib/docker/containers/c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44/hosts”,
“LogPath”: “/mnt/sda1/var/lib/docker/containers/c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44/c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2
a6f1f2930f0d44-json.log”,
“Name”: “/sampledocker”,
“RestartCount”: 0,
“Driver”: “aufs”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “”,
“ExecIDs”: null,
“HostConfig”: {
“Binds”: null,
“ContainerIDFile”: “”,
“LogConfig”: {
“Type”: “json-file”,
“Config”: {}
},
“NetworkMode”: “default”,
“PortBindings”: {},
“RestartPolicy”: {
“Name”: “no”,
“MaximumRetryCount”: 0
},
“AutoRemove”: false,
“VolumeDriver”: “”,
“VolumesFrom”: null,
“CapAdd”: null,
“CapDrop”: null,
“Dns”: ,
“DnsOptions”: ,
“DnsSearch”: ,
“ExtraHosts”: null,
“GroupAdd”: null,
“IpcMode”: “”,
“Cgroup”: “”,
“Links”: [
“/mongo:/sampledocker/mongo”
],
“OomScoreAdj”: 0,
“PidMode”: “”,
“Privileged”: false,
“PublishAllPorts”: false,
“ReadonlyRootfs”: false,
“SecurityOpt”: null,
“UTSMode”: “”,
“UsernsMode”: “”,
“ShmSize”: 67108864,
“Runtime”: “runc”,
“ConsoleSize”: [
63,
130
],
“Isolation”: “”,
“CpuShares”: 0,
“Memory”: 0,
“NanoCpus”: 0,
“CgroupParent”: “”,
“BlkioWeight”: 0,
“BlkioWeightDevice”: null,
“BlkioDeviceReadBps”: null,
“BlkioDeviceWriteBps”: null,
“BlkioDeviceReadIOps”: null,
“BlkioDeviceWriteIOps”: null,
“CpuPeriod”: 0,
“CpuQuota”: 0,
“CpuRealtimePeriod”: 0,
“CpuRealtimeRuntime”: 0,
“CpusetCpus”: “”,
“CpusetMems”: “”,
“Devices”: ,
“DeviceCgroupRules”: null,
“DiskQuota”: 0,
“KernelMemory”: 0,
“MemoryReservation”: 0,
“MemorySwap”: 0,
“MemorySwappiness”: -1,
“OomKillDisable”: false,
“PidsLimit”: 0,
“Ulimits”: null,
“CpuCount”: 0,
“CpuPercent”: 0,
“IOMaximumIOps”: 0,
“IOMaximumBandwidth”: 0
},
“GraphDriver”: {
“Data”: null,
“Name”: “aufs”
},
“Mounts”: ,
“Config”: {
“Hostname”: “c608f0443953”,
“Domainname”: “”,
“User”: “”,
“AttachStdin”: false,
“AttachStdout”: false,
“AttachStderr”: false,
“ExposedPorts”: {
“4200/tcp”: {}
},
“Tty”: false,
“OpenStdin”: false,
“StdinOnce”: false,
“Env”: [
“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”,
“NODE_VERSION=9.4.0”,
“YARN_VERSION=1.3.2”
],
“Cmd”: [
“npm”,
“start”
],
“ArgsEscaped”: true,
“Image”: “nitikishu/sampledocker”,
“Volumes”: null,
“WorkingDir”: “/C:Usersuser2Sample”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “1825c7f7299863fcf72e8b41352afc2f70685ac3253d487128a93080c6f36de8”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {},
“SandboxKey”: “/var/run/docker/netns/1825c7f72998”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “”,
“Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“MacAddress”: “”,
“Networks”: {
“bridge”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “beb8de95cd96de2d67503fdc6105802eda5b254212baac4d832970e92ed2fa73”,
“EndpointID”: “”,
“Gateway”: “”,
“IPAddress”: “”,
“IPPrefixLen”: 0,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “”,
“DriverOpts”: null
}
}
}
}
]
$ docker exec sampledocker env
Error response from daemon: Container c608f0443953cf0657adc63685366b47f6a475f1fe52d810a2a6f1f2930f0d44 is not running
yes, because the parms were =wrong
i just did this and the two containers connected ok (on my linux box)
start mongo db server
docker run -d --name mongodb mongo
connect from client
docker run -it --link mongodb:mongodb --rm mongo
user2@system2 MINGW64 ~/sample (master)
$ docker run -d --name MongoDB mongo
3dad02c17451e8595f71b7005a5c970aa7c5a538ccf7463a874c023c31639245
user2@system2 MINGW64 ~/sample (master)
$ docker run -it --link MongoDB:MongoDB --rm mongo
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=cfb0f319f4cf
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] db version v3.6.2
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] allocator: tcmalloc
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] modules: none
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] build environment:
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] distmod: debian81
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] distarch: x86_64
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] target_arch: x86_64
2018-02-06T05:29:12.377+0000 I CONTROL [initandlisten] options: { net: { bindIpAll: true } }
2018-02-06T05:29:12.378+0000 I STORAGE [initandlisten]
2018-02-06T05:29:12.378+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-02-06T05:29:12.378+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-02-06T05:29:12.378+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=489M,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_pro
gress),
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten]
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten]
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten]
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten]
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2018-02-06T05:29:12.431+0000 I CONTROL [initandlisten]
2018-02-06T05:29:12.432+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: a888ecd5-8c5c-4de9-abc5-a9244c697ae3
2018-02-06T05:29:12.453+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.6
2018-02-06T05:29:12.456+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: cdf0e6fc-3353-412b-a819-68825837eb21
2018-02-06T05:29:12.463+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ‘/data/db/diagnostic.data’
2018-02-06T05:29:12.463+0000 I NETWORK [initandlisten] waiting for connections on port 27017
@sdetweil
Next what should I do?
on windows: type ipconfig in terminal.
Get the corresponding iPv4 address. and use this as the domain name followed by the port you pass in command line.