I'm currently running MongoDB and Mongo Express with docker-compose. When I look on express, it shows I only have 815 available connections. How do I increase this? I tried adding 'command: maxConns 2000' in the docker-compose file but it had no impact. I believe MongoDB doesn't limit the number of connections so I assume this is a limitationenter code here with docker-compose?
version: '2'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
Unless constrained by maxConns, mongo will calculate available connections based on system limits. UNIX ulimit settings has guidance on checking and properly configuring ulimit values.
Related
I have a SpringBoot application running in the docker and I am using PostgreSQL as a database for this project and the database also running in the docker.
Now, I want to use MongoDb along with PostgreSQL as database to my SpringBoot application.
I added MongoDb info in the docker-compose.yml file and created new Dockerfile and ran the application. After that MongoDb got installed and running in the docker successfully.
I created a api for insertion of a document into the collection. When I hit the api I am getting the error. I think, I am not able to connect to the MongoDb which is running in the docker.
error:- com.mongodb.MongoSocketOpenException: Exception opening socket
I think I have to do configuration in the MongoDb before doing any CRUD operations.
Can anyone please share a detailed configuration of MongoDb with some examples.
Or provide some information which can help me achieve my task.
Thanks.
Docker-compose.yml
mongodb:
build:
context: mongodb
args:
DOCKER_ARTIFACTORY: ${DOCKER_ARTIFACTORY}
container_name: “mongodb”
image: mongo:6.0.4
restart: always
environment:
- MONGODB_USER=${SPRING_DATASOURCE_USERNAME:-username}
- MONGODB_PASSWORD=${SPRING_DATASOURCE_PASSWORD:-password}
ports:
- “27017:27017”
volumes:
- “/mongodata:/data/mongodb”
networks:
- somenetwork
Dockerfile
ARG DOCKER_ARTIFACTORY
FROM ${DOCKER_ARTIFACTORY}mongo:6.0.4
COPY init/mongodbsetup.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/mongodbsetup.sh
CMD [“mongod”]
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
mongodb:
image: mongo
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
You can find above configuration of docker file that includes both MongoDB and PostgreSQL configurations.
I resolve the issue and successfully able to connect and able to perform CRUD operations too.
Below are the changes I did.
1. docker-compose.yml
mongodb:
build:
context: mongodb
args:
DOCKER_ARTIFACTORY: ${DOCKER_ARTIFACTORY}
container_name: mongodb
hostname: mongodb
restart: always
environment:
- MONGO_INITDB_DATABASE=databaseName
- MONGO_INITDB_ROOT_USERNAME=username
- MONGO_INITDB_ROOT_PASSWORD=password
ports:
- 27017:27017
volumes:
- /mongodata:/data/mongodb
networks:
- somenetwork
created below file (filename: mongodbsetup.sh) under projectFolder/builder/mongodb/init
#!/bin/bash
mongosh <<EOF
use code
EOF
created below file (filename: Dockerfile) under projectFolder/builder/mongodb
ARG DOCKER_ARTIFACTORY
FROM ${DOCKER_ARTIFACTORY}mongo:6.0.4
COPY init/mongodbsetup.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/mongodbsetup.sh
CMD ["mongod"]
In application.properties file added below properties
#MongoDB configurations
spring.data.mongodb.database=databasename
spring.data.mongodb.uri=mongodb://username:password#databaseurlOrIpAddress:27017
That's it. It's working.
The following code snippet is a part of my "docker compose" file and as you see, the internal port of 27017 was mapped to 37017 to prevent colliding with the MongoDB instance running on the host development machine:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 27017:37017
I use Compass when trying to connect to this mongo instance in docker and the following connection string, but Compass fails to connect to the database:
mongodb://host.docker.internal:37017
What is missing in this configuration keeping me from connecting to the mongodb in docker?
The credit to this answer goes to David Maze. Switching the port numbers addressed the problem. The correct yaml looks like below:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 37017:27017
I've dockerize nestjs app with postgres and redis.
How can I fix this issue?
Postgres and redis are refusing tcp connections.
This is docker-compose.yml and console result.
I am using typeorm and #nestjs/bull for redis.
Hope much help.
Thanks
When using docker-compose, the containers do not necessarily need to specify the network they are in since they can reach each other from the container name (e.g. postgres, redis in your case) because they are in the same network. See Networking in Compose for more info.
Also, expose doesn't do any operation and it is redundant. Since you specify ports, it should be more than enough to tell Docker which ports of the container are exposed and bound to which ports of the host.
For redis-alpine, the startup command is redis-server and it is not necessary to specify it again. Also, the environment you specified is redundant in this case.
Try the following docker-compose.yaml with all of the above suggestions added:
version: "3"
services:
postgres:
image: postgres:alpine
restart: always
ports:
- 5432:5432
volumes:
- ./db_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_DB=lighthouse
redis:
image: redis:alpine
restart: always
ports:
- 6379:6379
Hope this helps. Cheers! 🍻
I want to deploy in Docker Swarm a stack with a MongoDB and a Web Server; the db must be filled with initial data and I found out this valid solution.
Given that Stack services start in casual order, how could I be sure that the Web Server will read initial data correctly?
Probably I need some notification system (Redis?), but I am new to MongoDB, so I am looking for well-known solutions to this problem (that I think is pretty common).
I would highly suggest looking at health checks of docker-compose.yml. You can change the health check command to specific MongoDB check, Once health-check is ready, then only Web-server will start sending the request to the MongoDB container.
Have a look at the example file. Please change the health-check as per your need
version: "3.3"
services:
mongo:
image: mongo
ports:
- "27017:27017"
volumes:
- ./data/mongodb/db:/data/db
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongo mongo:27017/test --quiet 1
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
webserver:
image: custom_webserver_image:latest
volumes:
- $PWD:/app
links:
- mongodb
ports:
- 8000:8000
depends_on:
- mongo
Ref:- https://docs.docker.com/compose/compose-file/#healthcheck
I've been trying to synchronize my mongodb with elastic for two days and I'm going crazy.
After many attempts and changes in my dockerfile and in my docker-compose I get this error, but the container with mongodb is up and running.
monstache | ERROR 2018/07/27 17:59:07 Unable to connect to mongodb using URL 'mongodb:27018': no reachable servers
monstache | panic: Unable to connect to mongodb using URL 'mongodb:27018': no reachable servers
monstache |
monstache | goroutine 1 [running]:
monstache | log.(*Logger).Panicf(0xc420020c30, 0xd4fc15, 0x2d, 0xc42006fc18, 0x2, 0x2)
monstache | /usr/local/go/src/log/log.go:219 +0xdb
monstache | main.main()
monstache | /home/vagrant/go/src/github.com/rwynn/monstache/monstache.go:2400 +0x320
monstache exited with code 2
This is my dockerfile
FROM golang
ADD build-4.4.0/linux-amd64/monstache /go/bin/monstache
ENTRYPOINT ["monstache", "-mongo-url='mongodb:27018'", "-elasticsearch-url=elasticsearch:9200"]
And this is my docker-compose file
version: '3.3'
services:
mongodb:
image: mongo
restart: always
container_name: mongodb
volumes:
- ./data/mongodb:/usr/share/mongodb/data
ports:
- 27018:27017
elasticsearch:
image: elasticsearch
restart: always
container_name: elasticsearch
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
ports:
- 9200:9200
monstache:
build: ./monstache/
restart: always
container_name: monstache
links:
- elasticsearch
- mongodb
Any idea?
Thanks!
Finally, we desist to use monstache, and we use mongo-connector in a python dockerfile to share the data from mongodb to elasticsearch.
FROM python:3.4.3
RUN pip install 'mongo-connector[elastic5]' && \
pip install 'elastic2-doc-manager[elastic5]'
I hope this can help to someone.
You have to change 'mongo-url' to -mongo-url='mongodb:27017', because all your containers are running in docker network and mongodb is available there on port 27017 since this is exposed port.
And the below block in compose file will also make mongodb available on your host network at port 27018 but that is accessible via your browser in your local machine env, not in docker network.
ports:
- 27018:27017
This compose file should also work without links.
version: '3.3'
services:
mongodb:
image: mongo
restart: always
container_name: mongodb
volumes:
- ./data/mongodb:/usr/share/mongodb/data
ports:
- 27018:27017
elasticsearch:
image: elasticsearch
restart: always
container_name: elasticsearch
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
ports:
- 9200:9200
monstache:
build: ./monstache/
restart: always
container_name: monstache