permission denied for mongodb container error while running from docker_compose file - mongodb

Using Windows 10 Pro.
This is one of the services under my docker_compose.yml file.
version: '3'
networks:
demo-net:
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- ./mongo_data:/data/db
networks:
- demo-net
When I am doing docker_compose up in vs code, I am getting this error
mongodb | 2020-05-07T16:53:34.336+0000 W STORAGE [initandlisten] Failed to start up WiredTiger under any compatibility version.
mongodb | 2020-05-07T16:53:34.337+0000 F STORAGE [initandlisten] Reason: 1: Operation not permitted
mongodb | 2020-05-07T16:53:34.337+0000 F - [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 915
mongodb | 2020-05-07T16:53:34.337+0000 F - [initandlisten]
mongodb |
mongodb | ***aborting after fassert() failure
mongodb |
mongodb |
mongodb exited with code 14
Could anyone tell me what I am doing wrong?
This same piece of code is working on a friend's mac.
Will keeping MongoDb in my local as well as using it in the container will cause any problem?

EDIT:
Your issue is container / host permissions. This answer might help.
ORIGINAL ANSWER:
Will keeping MongoDb in my local as well as using it in the container will cause any problem?
Yes, if you are running on the same default port (27017). You can avoid this by mapping a different local port, e.g. if your docker compose:
ports:
- 27018:27017
Then connect to mongo on port 27018 for the container.

Related

Docker mongo-express cannot connect to mongo if started with /data/db volume

I have a docker-compose.yml which I start with docker-compose up — and it works.
version: "3"
services:
mongo:
image: mongo
restart: always
ports:
- 27017-27019:27017-27019
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: brandRegistry
volumes:
- ./setup/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
# - ./data/mongo-volume:/data/db
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
But if I comment - ./data/mongo-volume:/data/db in the log says:
mongo-express_1 | Tue Jan 28 22:21:16 UTC 2020 retrying to connect to mongo:27017 (4/5)
mongo-express_1 | /docker-entrypoint.sh: connect: Connection refused
mongo-express_1 | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
I do not see how this relates to the volume...
I would like to store persistent data. Do you have a tip?
Thanks in advance!
I just did not wait long enough. Mongo needs way longer if started with a data volume outside of the container. And mongo-express will try to connect again and again. After a while it will succeed.
Add this line to mongo-express configuration on docker-compose.yaml [or how ever you named the file]
restart: unless-stopped
mongo-express will restart , until mongo container is ready, and when it is ready it will connect and stay up.

Mongo-db container shuts down after 30 s

I am developping a web app which has two containers the first container is a web app developed in flask and the second a mongodb image. I want to run them at the same time so I have written the following docker-compose.yml
version: '3.7'
services:
flask:
build: .
depends_on:
- mongo
ports:
- "5000:5000"
volumes:
- ./flask:/flask
mongo:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- ./dump:/dump # data
- ./datos_db:/data/db # persistance database
First I run $docker-compose build with no errors,then I get the following error when I run $ docker-compose up:
mongo_1 | 2019-10-30T16:18:47.420+0000 error connecting to host: could not connect to server: server selection error: server selection timeout
mongo_1 | current topology: Type: Single
mongo_1 | Servers:
mongo_1 | Addr: localhost:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(localhost:27017[-121]) connection is closed
mongo_1 |
practica4_mongo_1 exited with code 1
As I understand it the container waits too much time for localhost:271017 to be open and decides to shut down. (I have also tried with port 49155)
Is there anything I am missing and that makes my docker crash?

How to utilize a Mongo seed container to import users to admin? [duplicate]

I've been trying to get a basic nodeJS api to connect to a mongo container. Both services are defined in a docker-compose.yml file. I've read countless similar questions here and on docker's forum all stating that the issue is your mongo connection URI. This is not my issue as you'll see below.
docker-compose.yml
version: '3.7'
services:
api:
build: ./
command: npm run start:dev
working_dir: /usr/src/api-boiler/
restart: always
environment:
PORT: 3001
MONGODB_URI: mongodb://mongodb:27017/TodoApp
JWT_SECRET: asdkasd9a9sdn2r3513032
links:
- mongodb
ports:
- "3001:3001"
volumes:
- ./:/usr/src/api-boiler/
depends_on:
- mongodb
mongodb:
image: mongo
restart: always
volumes:
- /usr/local/var/mongodb:/data/db
ports:
- 27017:27017
Dockerfile
FROM node:10.8.0
WORKDIR /usr/src/api-boiler
COPY ./ ./
RUN npm install
CMD ["/bin/bash"]
db/mongoose.js
Setting up mongodb connection
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(
process.env.MONGODB_URI,
{ useMongoClient: true }
);
module.exports.mongoose = mongoose;
But no matter what the api container cannot connect. I'm tried setting the mongo uri to 0.0.0.0:3001 but no joy. I checked the config settings used to launch mongo in the container using db.serverCmdLineOpts(). And, the command bind_ip_all has been passed so mongo should accept connections from any ip. The typical issue is people forgetting to replace localhost with their mongo container name. EG:
mongodb://localhost:27017/TodoApp >> mongodb://mongodb:27017/TodoApp
But, this has been done. So pretty stumped.
Logs - for good measure
Successfully built 388868008521
Successfully tagged api-boiler_api:latest
Starting api-boiler_mongodb_1 ... done
Recreating api-boiler_api_1 ... done
Attaching to api-boiler_mongodb_1, api-boiler_api_1
mongodb_1 | 2018-08-20T20:09:27.072+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify -- sslDisabledProtocols 'none'
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=72af162616c8
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] db version v4.0.1
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] modules: none
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] build environment:
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] distmod: ubuntu1604
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] distarch: x86_64
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] target_arch: x86_64
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] options: { net: { bindIpAll: true } }
mongodb_1 | 2018-08-20T20:09:27.088+0000 W STORAGE [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongodb_1 | 2018-08-20T20:09:27.093+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb_1 | 2018-08-20T20:09:27.096+0000 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
mongodb_1 | 2018-08-20T20:09:27.097+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=487M,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_progress),
api_1 |
api_1 | > api-boiler#0.1.0 start:dev /usr/src/api-boiler
api_1 | > cross-env NODE_ENV=development node server/server.js
api_1 |
api_1 | Started on port 3001
api_1 | (node:24) UnhandledPromiseRejectionWarning: MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: connect ECONNREFUSED 172.18.0.2:27017]
OK. I've solved it. With the help of this blog here - https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b
You need to wait for mongod to fully start inside the container. The depend_on key in docker-compose.yml is not sufficient.
You'll also need to update your Dockerfile to take advantage of docker-compose-wait.
For reference - here is my updated docker-compose and Dockerfile files.
version: '3.7'
services:
api:
build: ./
working_dir: /usr/src/api-boiler/
restart: always
environment:
PORT: 3001
MONGODB_URI: mongodb://mongodb:27017/TodoApp
JWT_SECRET: asdkasd9a9sdn2r3513032
ports:
- "3001:3001"
volumes:
- ./:/usr/src/api-boiler/
depends_on:
- mongodb
environment:
WAIT_HOSTS: mongodb:27017
mongodb:
image: mongo
container_name: mongodb
restart: always
volumes:
- 27017:27017
FROM node:10.8.0
WORKDIR /usr/src/api-boiler
COPY ./ ./
RUN npm install
EXPOSE 3001
## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose- wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
# CMD ["/bin/bash"]
CMD /wait && npm run start:dev
I had a similar issue and the cause was that despite I explicitly pointed that backend depends_on database it was not enough and backend was starting earlier than my database was fully ready.
This is what helped:
backend:
depends_on:
db:
condition: service_healthy
db:
...
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh db:27017 --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
This way backend service isn't started until healthcheck is passed

Unable to make a connection to Dockerized MongoDb server - bind ip - No Chance to Authorize

Background:
I am using a "custom" dockerfile (just a modified versino of this) to setup my MongoDB server as well as a docker-compose file to link my dockerized Asp.Net Core application to the mongodb docker container.
Issue:
I got this error:
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
mongodb | 2018-07-09T17:28:24.327+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
I tried using bind-ip in my docker-compose file but then when I try to connect using the IP, Robo3T just times out and says "Failed to connect to {ip}:{port} - No chance to authorize"
If I do just:
command: --bind_ip 127.0.0.1
Then the error goes away but I still can't make a connection from Robo3T to the database
Docker Compose File
version: '3.4'
services:
sensormonitor:
container_name: sensormonitor
build: .
ports:
- "80:80"
environment:
- NODE_PATH=Production
- ASPNETCORE_ENVIRONMENT=Production
links:
- mongodb
depends_on:
- mongodb
mongodb:
#image: mongo:latest
container_name: mongodb
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
- MONGO_INITDB_DATABASE=SensorMonitorDb
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=123ewq
ports:
- 27017:27017
#volumes:
# - ./data:/data/db:rw
build:
context: ./Scripts/Mongodb/MongoDbDocker
dockerfile: Dockerfile.mongodb
#command: --bind_ip 127.0.0.1,0.0.0.0
Why --bind_ip 127.0.0.1,0.0.0.0?
If you put --bind_ip 0.0.0.0 then it can listen on all the interfaces (including 127.0.0.1)
By default mongo container opens for all interfaces(i.e, --bind_ip_all/--bind_ip 0.0.0.0(mutual exclusive)).
shakeel#airflow:~/docker-space/mongo$ sudo docker exec -it mongo_mongodb_1 bash
root#airflow:/# ps -ef|grep mongo
mongodb 1 0 3 20:24 ? 00:00:01 mongod --bind_ip_all
root 65 55 0 20:24 pts/0 00:00:00 grep --color=auto mongo
root#airflow:/#
network_mode: "host" is the configuration you need to connect mongo without container IP(docker-compose version 3.7).
shakeel#airflow:~/docker-space/mongo$ cat docker-compose.yml
version: '3.7'
services:
# MongoDB: https://hub.docker.com/_/mongo/
mongodb:
image: mongo:4
network_mode: "host"
volumes:
- mongo-db-data:/data/db
# Volumes for persisting data, see https://docs.docker.com/engine/admin/volumes/volumes/
volumes:
mongo-db-data:

GOLANG client docker container getting unreachable when attempting to connect to mongodb container

I have a simple program written in golang which connect to a mongodb instance thus:
func main() {
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
}
I use docker compose to spin up two containers one to spin up my GOLANG API and the other to spin up mongo:
version: "3.3"
services:
api:
image: golang:latest
volumes:
- .:/go
working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
networks:
- api-net
db:
build: .
expose:
- '27017'
container_name: 'mongo'
ports:
- "27017:27017"
networks:
- api-net
networks:
api-net:
driver: bridge
I use a Dockerfile in order to spin up mongo with a conf file that sets the instance to bind to all IPV4 and IPV6 networks:
FROM mongo:latest
COPY mongod.conf /etc/mongod.conf
CMD mongod --config /etc/mongod.conf##
and this is the mongod.conf file:
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
When I run docker-compose.up this is what I get:
Building db
Step 1/3 : FROM mongo:latest
---> a0f922b3f0a1
Step 2/3 : COPY mongod.conf /etc/mongod.conf
---> Using cache
---> f270e718c11e
Step 3/3 : CMD mongod --config /etc/mongod.conf
---> Running in 89ffc2495a2a
Removing intermediate container 89ffc2495a2a
---> fe2677d53122
Successfully built fe2677d53122
Successfully tagged carsupermarket_db:latest
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating mongo ... done
Creating carsupermarket_api_1 ... done
Attaching to mongo, carsupermarket_api_1
api_1 | panic: no reachable servers
api_1 |
api_1 | goroutine 1 [running]:
api_1 | main.main()
api_1 | /go/src/main.go:38 +0x291
api_1 | exit status 2
I've trawled Google and stackoverflow and the only thing I could manage to find which is vaguely related to my issue is:
mongod --bind_ip using docker-compose version 2
however, my docker-compose.yml file as it stands 'Should' on paper work.
Can someone please point me in the correct direction as to why my GOLANG code cannot find my mongodb instance.
I think that when you call mgo.Dial("localhost:27017") the localhost part refers to the localhost within the golang container, and not the host the the containers are running on.
Docker will resolved container names as host names, change the dial to mgo.Dial("mongo:27017") and it should work.
From the docker container networking bridge docs
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by
IP addresses, unless you use the --link option, which is considered
legacy. On a user-defined bridge network, containers can resolve each
other by name or alias.
https://docs.docker.com/network/bridge/
version: "2"
services:
api:
image: golang:latest
volumes:
- .:/go
# Work dir should be defined in dockerfile
#working_dir: /go
environment:
- GOPATH=/go
command:
go run /go/src/main.go
ports:
- "8082:8000"
depends_on:
- db
db:
build: .
container_name: 'mongo'
# You should define some volumes here if you want the data to persist.
You should be able to connect from the golang container to the mongo container using inter container communication using the hostname: 'mongo'