GCE "create-with-container --container-mount-disk" flag mounts disk as read-only - mongodb

I am trying to use a Percona Docker image for MongoDB on GCE, however I'm running into an issue with Mongo saying the mounted path is read-only. I looked around as much as I could, but im stumped at what could be the issue.
gcloud compute instances create-with-container mongo-svr \
--create-disk name=disk-1,size=1GB \
--container-mount-disk mount-path="/data/mongodb",mode=rw \
--container-image=docker.io/percona/percona-server-mongodb:4.2
I used the above command and it creates my instance. I then SSH into the server, connect to the running mongo instance to shutdown, then I run: docker exec -it [NAME] mongod --configsvr --replSet rs0 --dbpath=/data/mongodb --bind_ip localhost
This spits out an error stating:
CONTROL [initandlisten] options: { net: { bindIp: "localhost" }, replication: { replSet: "rs0" }, sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/data/mongodb" } }
STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/mongodb, terminating
At this point, I've been recreating instances with different params, but nothing has worked so far. Anyone have an idea what I'm missing?
Updated with command output
gcloud compute instances create-with-container mongo-config-f --zone us-central1-f --create-disk name=disk-1,size=1GB --container-mount-disk mount-path="/data/mongodb" --container-image=docker.io/percona/percona-server-mongodb:4.2 --machine-type=f1-micro
WARNING: Default device-name for disk name [disk-1] will be [disk-1] because it is being mounted to a container with [`--container-mount-disk`]
Created [https://www.googleapis.com/compute/v1/projects/[PROJECT_NAME]/zones/us-central1-f/instances/mongo-config-f].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
mongo-config-f us-central1-f f1-micro xx.xx.xx.xx xx.xx.xx.xx RUNNING

I've tried to replicate your issue on my test project and found that:
persistent disk was created and mounted in read-write mode as expected;
bash-4.2$ mount
...
/dev/sdb on /data/mongodb type ext4 (rw,relatime)
docker runs containers inside our VM properly;
the cause of the error while running docker exec -it [NAME] mongod --configsvr --replSet rs0 --dbpath=/data/mongodb --bind_ip localhost is permissions inside mongodb container:
bash-4.2$ ls -l /data/
...
drwxr-xr-x 3 root root 4096 Feb 19 15:33 mongodb
As a workaround commands could be executed with root permissions:
$ docker exec -it --user root klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --db path=/data/mongodb
Please find more details and my steps below:
create VM:
$ gcloud compute instances create-with-container mongo-svr \
--create-disk name=disk-1,size=1GB \
--container-image docker.io/percona/percona-server-mongodb:4.2 \
--container-mount-disk mount-path="/data/mongodb"
WARNING: Default device-name for disk name [disk-1] will be [disk-1] because it is being mounted to a container with [`--container-mount-disk`]
Created [https://www.googleapis.com/compute/v1/projects/test-prj/zones/europe-west3-a/instances/mongo-svr].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
mongo-svr-upd europe-west3-a n1-standard-1 10.156.0.9 35.XXX.155.XXX RUNNING
SSH to instance;
check if container is running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dfad9c10235d percona/percona-server-mongodb:4.2 "/entrypoint.sh mong…" About a minute ago Up About a minute klt-mongo-svr-upd-wowt
bbe02c8e8621 gcr.io/stackdriver-agents/stackdriver-logging-agent:0.2-1.5.33-1-1 "/entrypoint.sh /usr…" About a minute ago Up About a minute stackdriver-logging-agent
everything looks good at this point;
try to run command as user:
$ docker exec -it klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --dbpath=/data/mongodb --bind_ip localhost
and observe the same error:
2020-02-19T15:37:53.176+0000 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/mongodb, terminating
here key read-only directory: /data/mongodb;
check mounts and permissions inside of the container:
$ docker exec -it klt-mongo-svr-upd-wowt /bin/bash
bash-4.2$ mount
...
/dev/sdb on /data/mongodb type ext4 (rw,relatime)
...
as we expected disk was created and mounted in read-write mode to the container
bash-4.2$ ls -l /data/
total 8
drwxr-xr-x 4 mongodb root 4096 Feb 19 15:36 db
drwxr-xr-x 3 root root 4096 Feb 19 15:33 mongodb
bash-4.2$
but to work with /data/mongodb you need root permissions;
try to run command as root:
$ docker exec -it --user root klt-mongo-svr-upd-wowt mongod --configsvr --replSet rs0 --dbpath=/data/mongodb
2020-02-19T15:45:24.970+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-02-19T15:45:24.973+0000 I CONTROL [initandlisten] MongoDB starting : pid=119 port=27019 dbpath=/data/mongodb 64-bit host=mongo-svr-upd
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] db version v4.2.2-3
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] git version: 2cdb6e50913583f627acc5de35dc4e04dbfe196f
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] allocator: tcmalloc
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] modules: none
2020-02-19T15:45:24.974+0000 I CONTROL [initandlisten] build environment:
2020-02-19T15:45:24.975+0000 I CONTROL [initandlisten] distarch: x86_64
2020-02-19T15:45:24.975+0000 I CONTROL [initandlisten] target_arch: x86_64
2020-02-19T15:45:24.975+0000 I CONTROL [initandlisten] options: { replication: { replSet: "rs0" }, sharding: { clusterRole: "configsvr" }, storage: { dbPath: "/data/mongodb" } }
2020-02-19T15:45:24.976+0000 I STORAGE [initandlisten] Detected data files in /data/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
...
and it's working with root permissions.

Related

MongoDB service: Failed to open logfile permission denied

I'm trying to restart mongodb service after replacing mongod.conf file with bindip,replication name..etc.But unfirtunately it is failing and stating as unable to open logfile permission denied.But even if i gave 777 perm same issue
cat /etc/mongod.conf
systemLog:
destination: file
path: "/data/log/test.log"
logAppend: true
net:
bindIp: 0.0.0.0
port: 27017
storage:
dbPath: "/data/dbdata/"
replication:
replSetName: "rs0"
Error message:
msg":"Error during global initialization","attr":{"error":{"code":38,"codeName":"FileNotOpen","errmsg":"Failed probe for \"/data/log/test.log\": Permission denied"}}}
Permission on server:
ls -la /data/log
total 0
drwxrwxrw-. 2 mongod mongod 22 Jun 7 06:33 .
drwxrwxrw-. 5 mongod mongod 42 Jun 7 05:34 ..
-rwxrwxrwx. 1 mongod mongod 0 Jun 7 06:33 test.log
ls -la /data/
total 0
drwxrwxrw-. 5 mongod mongod 42 Jun 7 05:34 .
dr-xr-xr-x. 18 root root 236 May 4 17:30 ..
drwxrwxrw-. 2 mongod mongod 6 Jun 7 05:34 dbdata
drwxrwxrw-. 2 mongod mongod 22 Jun 7 06:33 log
ls -ld /data
drwxrwxrw-. 5 mongod mongod 42 Jun 7 05:34 /data
NOTE: /data/dbdata,/data/log are mountpoints
Fixed the issue by followed the steps present in the official documentation which required for selinux setup
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/#std-label-install-rhel-configure-selinux
yum install -y policycoreutils-python-utils
semanage fcontext -a -t mongod_var_lib_t /data/dbdata.*
semanage fcontext -a -t mongod_log_t /data/log.*
chcon -Rv -u system_u -t mongod_var_lib_t /data/dbdata
chcon -Rv -u system_u -t mongod_log_t /data/log
restorecon -R -v /data/dbdata
restorecon -R -v /data/log

Permission denied error Change dbpath for mongodb in centos

I am attempting to update the dbpath for mongo db. I am following the steps as below:
1) Create new directory : sudo mkdir /mnt/database/mongo
2) Change owner : sudo cp -R /var/lib/mongo/ /mnt/database/
3) Copy DB files: sudo chown mongod:mongod -R /mnt/database/mongo
4) Update mongo configuration file:
storage:
dbPath: /mnt/database/mongo
5) Restart mongod service: sudo systemctl restart mongod
The service restart command gives the following error:
Job for mongod.service failed because the control process exited with error code. See systemctl status mongod.service and journalctl -xe for details.
Logfile contents:
I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/mnt/database/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2020-04-17T04:56:00.666+0000 I STORAGE [initandlisten] exception in initAndListen: Location28596: Unable to determine status of lock file in the data directory /mnt/database/mongo: boost::filesystem::status: Permission denied: "/mnt/database/mongo/mongod.lock", terminating
Am I missing any steps with regards to permission.
System logs: /var/log/messages
systemd: Stopping MongoDB Database Server...
systemd: Starting MongoDB Database Server...
mongod: about to fork child process, waiting until server is ready for connections.
mongod: forked process: 17968
mongod: ERROR: child process failed, exited with error number 100
mongod: To see additional information in this output, start without the "--fork" option.
systemd: mongod.service: control process exited, code=exited status=100
systemd: Failed to start MongoDB Database Server.
systemd: Unit mongod.service entered failed state.
systemd: mongod.service failed.
Directory permissions:
/mnt :
drwxr-xr-x. 6 mongod mongod mnt
/mnt/database:
drwxrwxrwx. 3 mongod mongod database
/mnt/database/mongo:
drwxrwxrwx. 4 mongod mongod mongo
Files under /mnt/database/mongo:
-rwxrwxrwx. 1 mongod mongod 20480 collection-0-1788044936069962496.wt
-rwxrwxrwx. 1 mongod mongod 36864 collection-2-1788044936069962496.wt
-rwxrwxrwx. 1 mongod mongod 4096 collection-4-1788044936069962496.wt
drwxrwxrwx. 2 mongod mongod 90 diagnostic.data
-rwxrwxrwx. 1 mongod mongod 20480 index-1-1788044936069962496.wt
-rwxrwxrwx. 1 mongod mongod 36864 index-3-1788044936069962496.wt
-rwxrwxrwx. 1 mongod mongod 4096 index-5-1788044936069962496.wt
-rwxrwxrwx. 1 mongod mongod 12288 index-6-1788044936069962496.wt
drwxrwxrwx. 2 mongod mongod 110 journal
-rwxrwxrwx. 1 mongod mongod 20480 _mdb_catalog.wt
-rwxrwxrwx. 1 mongod mongod 36864 sizeStorer.wt
-rwxrwxrwx. 1 mongod mongod 114 storage.bson
-rwxrwxrwx. 1 mongod mongod 47 WiredTiger
-rwxrwxrwx. 1 mongod mongod 4096 WiredTigerLAS.wt
-rwxrwxrwx. 1 mongod mongod 21 WiredTiger.lock
-rwxrwxrwx. 1 mongod mongod 1185 WiredTiger.turtle
-rwxrwxrwx. 1 mongod mongod 61440 WiredTiger.wt
The security context of mongodb directoreies:
ls -Z /mnt/database
drwxrwxrwx. mongod mongod unconfined_u:object_r:mnt_t:s0 mongo
Resolved the issue executing chcon -R --reference=/var/lib/mongo /mnt/database/mongo

Cannot connect from node to mongo replicaset in docker

I've set up a docker network in which I have set up 3 mongo containers.
Summary of what I've done:
created a docker network in which I set up 3 mongo docker containers
open up the mongo shell for first node and set up the config for the replica set
tried to connect from node app, failed
successfully connect from mongo shell to replicaSet
Below I give a more detailed view of what I've tried.
These are the following cmds I have run for docker:
docker network create my-mongo-cluster
docker run -d -p 30001:27017 --name mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30002:27017 --name mongo2 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30003:27017 --name mongo3 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker exec -it mongo1 mongo
config = { "_id": "my-mongo-set", "members": [{"_id": 0, "host": "mongo1:27017"},{"_id": 1,"host": "mongo2:27017"},{"_id": 3,"host": "mongo3:27017" } ]}
rs.initiate(config)
From MongoDB Compass I've connected to the primary node, 192.168.1.3:30001 and created a database test with one collection user.
From node I try the following:
const app = require('express')();
const mongoose = require('mongoose');
//set up mongo connect
const uri = 'mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test'
mongoose.connect(uri, {useNewUrlParser: true, replicaSet: 'my-mongo-set'})
.then(() => console.log("MongoDB Connected"))
.catch(error => console.log(error));
From which I get
Debugger attached.
{ MongoNetworkError: failed to connect to server [mongo3:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongo3 mongo3:27017]
at Pool.<anonymous> (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\topologies\server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\pool.js:557:14)
at makeConnection (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:39:11)
at callback (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:261:5)
at Socket.err (C:\Workspaces\intelij\trial&error\spring-reactive-security\mongo-transactional\node_modules\mongodb-core\lib\connection\connect.js:286:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Waiting for the debugger to disconnect...
Process finished with exit code 0
but if I try from mongo shell, I am able to connect:
mongo "mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test"
MongoDB shell version v4.0.10
connecting to: mongodb://192.168.1.3:30001,192.168.1.3:30002,192.168.1.3:30003/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("89994673-11c2-4d6c-8cb5-04041094c147") }
MongoDB server version: 4.0.10
Server has startup warnings:
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten]
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-07-20T06:22:00.476+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten]
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-07-20T06:22:01.185+0000 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
my-mongo-set:PRIMARY>
Ok, so I've finally figure out what I was doing wrong. Maybe this will help someone:
you either go in C:\Windows\System32\drivers\etc and put in hosts MACHINE_IP mongo1 mongo2 mongo3, or just replace the config as:
config = { "_id": "my-mongo-set", "members": [{"_id": 0, "host": "MACHINE_IP:30001"},{"_id": 1,"host": "<MACHINE_IP>:30002"},{"_id": 2,"host": "<MACHINE_IP>:30003" } ]}
also I had some problems with mongo figuring out which one is primary so I slightly modified the docker runs, by adding MONGODB_REPLICA_SET_MODE and MONGODB_PRIMARY_HOST as:
docker run -d -p 30001:27017 --name mongo1 -e MONGODB_REPLICA_SET_MODE=primary --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30002:27017 --name mongo2 -e MONGODB_REPLICA_SET_MODE=secondary -e MONGODB_PRIMARY_HOST=mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set
docker run -d -p 30003:27017 --name mongo3 -e MONGODB_REPLICA_SET_MODE=secondary -e MONGODB_PRIMARY_HOST=mongo1 --net my-mongo-cluster mongo mongod --replSet my-mongo-set

Failure to connect to configured mongo instance (Connection refused)

Based on this guide:
https://docs.opsmanager.mongodb.com/current/tutorial/install-simple-test-deployment/
I am installing MongoDB and MongoDB Manager. I have created a docker image for each application and start them on the same virtual network:
docker network create --driver bridge mongo-network
with:
MongoDB:
docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"
And verified that its up and running with:
$ docker exec -ti -u mongod mongodb-container tail -f /data/appdb/mongodb.log
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-04-21T15:26:05.208+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-04-18T06:23:35.268+0000 I CONTROL [initandlisten]
2019-04-18T06:23:35.269+0000 I STORAGE [initandlisten] createCollection: admin.system.version with provided UUID: c0736278-72ec-4dfc-893c-8105eefa0ba8
2019-04-18T06:23:35.320+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 4.0
2019-04-18T06:23:35.341+0000 I STORAGE [initandlisten] createCollection: local.startup_log with generated UUID: 397c17a3-3c5e-4605-b4dc-8a936dd9e40e
2019-04-18T06:23:35.394+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/appdb/diagnostic.data'
2019-04-18T06:23:35.396+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2019-04-18T06:23:35.397+0000 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: ac7bdb6e-4a60-430f-b1a4-34b09012e6da
2019-04-18T06:23:35.475+0000 I INDEX [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-04-18T06:23:35.475+0000 I INDEX [LogicalSessionCacheRefresh] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-04-18T06:23:35.477+0000 I INDEX [LogicalSessionCacheRefresh] build index done. scanned 0 total records. 0 secs
MongoDB Manager:
docker run -ti -d --network mongo-network -p 8080:8080 --name mongodb-manager-container mongodb-manager-image
docker exec -ti -u root mongodb-manager-container "/opt/mongodb/mms/bin/mongodb-mms start"
Below error message:
Generating new Ops Manager private key...
Starting pre-flight checks
Failure to connect to configured mongo instance:Config{
loadBalance=false,
encryptedCredentials=false,
ssl='false',
dbNames=' [
mmsdb,
mmsdbprovisionlog,
mmsdbautomation,
mmsdbserverlog,
mmsdbpings,
mmsdbprofile,
mmsdbrrd,
mmsdbconfig,
mmsdblogcollection,
mmsdbjobs,
mmsdbagentlog,
mmsdbbilling,
backuplogs,
automationcore,
monitoringstatus,
mmsdbautomationlog,
automationstatus,
ndsstatus,
cloudconf,
backupdb,
mmsdbprovisioning,
mmsdbqueues
] ',
uri=mongodb://mongodb-container:27017/?maxPoolSize=150
}Error:Timed out after 30000 ms while waiting to connect. Client view of cluster state is{
type=UNKNOWN,
servers= [
{
address=mongodb-container:27017,
type=UNKNOWN,
state=CONNECTING,
exception= {
com.mongodb.MongoSocketOpenException:Exception opening socket
},
caused by {
java.net.ConnectException:Connection refused (Connection refused)
}
}
]
And for mongodb - based on suggestions below - I am now using:
/etc/mongod.conf:
# network interfaces
net:
port: 27017
#bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
bindIp: 0.0.0.0,::
and for MongoDB manager I am specifying the name of the mongodb container in:
/opt/mongodb/mms/conf/conf-mms.properties
#mongo.mongoUri=mongodb://127.0.0.1:27017/?maxPoolSize=150
#mongo.mongoUri=mongodb://0.0.0.0:27017/?maxPoolSize=150
mongo.mongoUri=mongodb://mongodb-container:27017/?maxPoolSize=150
I have verified that I can ping mongodb-container from mongodb-manager-container with:
docker exec -it -u root mongodb-manager-container bash
[root#e23a34bf2161 /]# ping mongodb-container
PING mongodb-container (172.18.0.2) 56(84) bytes of data.
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=3 ttl=64 time=0.052 ms
^C
--- mongodb-container ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2042ms
rtt min/avg/max/mdev = 0.052/0.062/0.077/0.013 ms
[root#e23a34bf2161 /]#
What am I missing?
EDIT:
Based on below suggestions I have now tried:
docker network create --driver bridge mongo-network
docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
# Copy modified version of mongod.conf file to container before starting mongodb
docker cp mongod.conf mongodb-container:/etc/mongod.conf
docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"
docker run -it --rm --net container:mongodb-container nicolaka/netshoot ss -lnt
Which gives:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.11:36001 0.0.0.0:*
LISTEN 0 128 127.0.0.1:27017 0.0.0.0:*
Not sure if this is expected/good output and why I need to spin up container from the nicolaka/netshoot image...
EDIT 2:
As suggested below if I pass: --bind_ip_all on the command line for starting mongod it works:
docker exec -ti -u mongod mongodb-container "mongod --bind_ip_all --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"
So it seems when running as a docker container it completely ignores the /etc/mongod.conf file and you need to specify all the options in the docker exec command instead :-(
DNS on the container name will resolve to the container ip. To connect to mongo on that name, even from inside the container, you need to have mongo listening on all interfaces:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0,::
The problem was that it was ignoring the configuration in /etc/mongod.conf file. After googling e.g.:
https://jira.mongodb.org/browse/SERVER-36572
I found that you need to pass the --config parameter to mongod e. to get it to read the mongod.conf file e.g.:
mongod --config /etc/mongod.conf
and with docker:
docker exec -ti -u mongod mongodb-container "mongod --config /etc/mongod.conf"
After doing the above I can now get it to listen on all interfaces with the below configuration in /etc/mongod.conf:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
You are running two separate containers and expect them to talk to each other over localhost? That never gonna work. You have to add "--link mongodb-container:mongo" to second docker run command and then use address mongodb://mongo:27017 in manager container.

Mongodb won't start in Docker container when remote-mounting a volume from host

On a Mac, I'm running mongodb from docker with the data directory mounted from a volume on my host like this:
docker run -d -p 27017:27017 -v /Users/me/git/world/db:/var/lib/mongodb gzoller/world
(/var/lib/mongodb is where the mongo config file says its storing data.)
I want to have my data persist on the host even if I kill the container running mongodb.
Using this run command tho, mongo doesn't start in the container. The mongo log in the container has this clip:
Mon Sep 14 18:27:59 [initandlisten] options: { command: [ "run" ], config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", journal: "true", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", unixSocketPrefix: "/var/run/mongodb" }
Mon Sep 14 18:27:59 [initandlisten] Assertion: 13651:Couldn't fsync directory '/var/lib/mongodb': errno:22 Invalid argument
I've chmod a+rwx the db directory on the host I'm mounting to.
What I think I've learned so far is that (at least on a Mac) only root in the container can write to a volume on the host and mongo creates its own user, mongodb, which I presume can't write to my mounted volume.
How can I get mongo writing to a mounted host volume on MacOS?
Looks that's not permission issue. If review the origin repository (gzoller/world/go.sh), you should run the command as below:
docker run -d -p 80:80 -p 5672:5672 -p 15672:15672 -p 27017:27017 -p 28017:28017 -p 11211:11211 -v /Users/me/git/world/db:/var/lib/mongodb -e HOST_IP=`docker-machine ip default` gzoller/world
Let me know the result.