Docker running host but not accessible

on running application every time container ip address will change?

Before u said This ip 192.168.99.100:8082 check the application running host.

Now ur saying 172.17.0.2:8088 host. I have confused. ///Now this ip is not running

I am confused as well… what is your docker host? windows, linux? i cannot remember.

please describe again the two sides of your solution

server side

app side

Hi @sdetweil,

docker-machine env default
Docker_host=“tcp://192.168.99.100:2376”

I am using winodws 8.

I developed one sample application using (mean stack technology). I want to run this application in all environment(in every where also). So I start to use docker to run my sample application. for that I have built , push the image into docker using (docker quickstart terminal). Now when I run that application in docker by docker run nitikishu/samplenodejs command I am getting following message:

> $ docker run -p 8088:8088 nitikishu/samplenodejs
> sampleproject@1.0.0 start /C:Usersuser2Sample
> node server.js
> server running on port 8088
> Press CTRL-C to stop
> MongoDB connection error. Please make sure MongoDB is running.

in my application I gave mongodb path like this:

MONGODB_URI=mongodb://192.168.99.100:27017/sample_DB
App_PORT = 8088

I want to access my system database(I am using the database named as pmsdb) I want to access that db with the sample application in docker.

I dont know how to connect mongodb database into that docker. Which path should I give also I don’t know. Because When I run the application in my system I will use mongodb path ="monogdb://localhost:27017/sample_DB
If I use the same path in docker built application It is not working.

using docker -machine ip address 192.168.99.100:8088 I am running my project. But Db which ip address I should give?

yesterday you said to bind all ip in mongod.config file. I done that by below code

mongodb.cfg file (this file is in mongodb installed path of my local windows system c:mongodb/bin/mongod.cfg)

systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
net:
bindIp: 0.0.0.0

Now How to connect my local db into samplenodejs application which is running in docker.

Mongodb version :3.4.2
ip address of mongodb: 127.0.0.1:27017

Here, I included the steps which I followed to run the application in docker

##         .
  ## ## ##        ==
   ## ## ## ## ##    ===
   /"""""""""""""""""\___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
   \______ o           __/
 \    \         __/
  \____\_______/

docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

Start interactive shell

user2@system2 MINGW64 ~
$ cd sample

user2@system2 MINGW64 ~/sample
$ docker --version
Docker version 17.05.0-ce, build 89658be

user2@system2 MINGW64 ~/sample
$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.06.0-ce

user2@system2 MINGW64 ~/sample
$ docker build -t nitikishu/samplenodejs .
Sending build context to Docker daemon   48.7MB
Step 1/7 : FROM node:carbon
 ---> b87c2ad8344d
Step 2/7 : WORKDIR C:\Users\user2\Sample
 ---> Using cache
 ---> 3880524e00d9
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> 6e116b22c908
Step 4/7 : RUN npm install
 ---> Using cache
 ---> a31a7a85f633
Step 5/7 : COPY . .
 ---> 7124ff43c59c
Removing intermediate container c8e8c639d7f0
Step 6/7 : EXPOSE 8088
 ---> Running in 4cd031953ed5
 ---> 9c252b8cb2c2
Removing intermediate container 4cd031953ed5
Step 7/7 : CMD npm start
 ---> Running in 19910c6a969f
 ---> 36e4e33d1466
Removing intermediate container 19910c6a969f
Successfully built 36e4e33d1466
Successfully tagged nitikishu/samplenodejs:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' p
ermissions. It is recommended to double check and reset permissions for sensitive files and directories.

user2@system2 MINGW64 ~/sample
$ docker push nitikishu/samplenodejs
The push refers to a repository [docker.io/nitikishu/samplenodejs]
4638b36f4098: Pushed
bc17d0d0ec47: Layer already exists
d228ef051c7d: Layer already exists
0697092f077d: Layer already exists
092504c250f7: Layer already exists
299e6ab856be: Layer already exists
50599c766115: Layer already exists
d4141af68ac4: Layer already exists
8fe6d5dcea45: Layer already exists
06b8d020c11b: Layer already exists
b9914afd042f: Layer already exists
4bcdffd70da2: Layer already exists
Head https://registry-1.docker.io/v2/nitikishu/samplenodejs/blobs/sha256:36e4e33d1466f119410498bd88b9a33d51c4792cbd7a2810dbf455e8ca8f4ce4: net/http: TLS handshake timeout

user2@system2 MINGW64 ~/sample
$ docker pull nitikishu/samplenodejs
Using default tag: latest
latest: Pulling from nitikishu/samplenodejs
Digest: sha256:7db75ca39cf597ac92a5fa3f459d5315f8909c6042e14a491238026bfdd24892
Status: Downloaded newer image for nitikishu/samplenodejs:latest

user2@system2 MINGW64 ~/sample
$ docker run -p 8088:8088 nitikishu/samplenodejs

> sampleproject@1.0.0 start /C:Usersuser2Sample
> node server.js

 server running on port 8088
  Press CTRL-C to stop

 MongoDB connection error. Please make sure MongoDB is running.

user2@system2 MINGW64 ~/sample
$

thanks… it is still unclear what you mean by the word ‘local’ for the mongo db…

do you mean running on the machine on which you started docker.

there is windows machine
there is virtual machine inside windows (the docker host)
and there is the container…

i THINK you are saying you want the application, running in the container on the virtual machine to connect to the mongo DB running natively on windows. (because your steps do not include starting a mongo db server container)

I will wait for your reply…

yes… In my machine I have a mongodb database. I want to access my system and also LAN connected(local machine) database. In my system only I am running docker.

yes @sdetweil… my application running in the docker container. I want to connect to mongodb database which is running in my own system .

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)

  1. the client app uses the syntax server_IP_address_or_name:27017 to connect to the remote from itself database.

  2. 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

  1. 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
1 Like

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
}
}
}
}
]