I am trying to run two MongoDBs within one docker-compose. As I want to store different data in each of the databases, I need to distinguish them by different ports. If I run the following docker-compose, everything works fine for port 27017, but I cannot access port 27018. In the console everything looks the same for both services, only the following log is missing for the service on port 27018:
NETWORK [initandlisten] waiting for connections on port 27017
(shows up for port 27017)
When I try to access both services in the browser, the one on port 27017 works and the one on 27018 doesn't.
I'm a very glad if anybody can help me out with that issue. Working on it since pretty long. Thanks!
Here's my code:
version: "3"
services:
mongo:
image: mvertes/alpine-mongo:4.0.1-0
volumes:
- mongoDBvolume:/data/db
ports:
- "27017:27017"
testmongo:
image: mvertes/alpine-mongo:4.0.1-0
command: mongod --port 27018
volumes:
- mongoDBvolume:/data/testdb
ports:
- "27018:27017"
volumes:
mongoDBvolume:
driver: local
Just remove command: mongod --port 27018, so it will be on port 27017 inside the container.
The flow is like:
Host 27018 <--> docker bridge 27018:27017 <--> mongo container 27017
version: '3.7'
services:
mongo:
image : mongo
container_name: mongo
networks:
- mongo
environment:
- PUID=1000
- PGID=1000
ports:
- "${MONGO_PORT}:27027"
restart: unless-stopped
command: "mongod --bind_ip_all --config /etc/mongo/mongod.conf"
volumes:
- "./data:/data/db"
- "./mongo:/etc/mongo"
networks:
mongo:
driver: bridge
mongod.conf file :
net:
bindIp: 127.0.0.1
port: 27027
setParameter:
enableLocalhostAuthBypass: false
Related
I am using Orion Context Broker (in docker container) and I need it to connect with MongoDB (which is in its own docker container). At the same time I must deny all incoming traffic to 27017 from external sources, because after running the docker-compose the port 27017 is "exposed" to public.
All of the above using Ubuntu 20.04.
This is my docker-compose.yml file
version: "3.5"
services:
orion:
image: fiware/orion-ld
hostname: orion
container_name: fiware-orion
expose:
- "1026"
ports:
- "1026:1026"
depends_on:
- mongo-db
command: -dbhost mongo-db -logLevel DEBUG
mongo-db:
image: mongo:3.6
hostname: mongo-db
container_name: db-mongo
ports:
- "27017:27017"
networks:
- default
command: --nojournal
volumes:
- mongo-db:/data
volumes:
mongo-db: ~
172.18.0.3 is the internal IP given to Orion's docker container. So I tried adding --bind_ip 172.18.0.3 to command mongo_db parameter in the docker-compose file, but this breaks the docker-compose up process with this error:
db-mongo | 2022-01-12T13:17:56.650+0000 E STORAGE [initandlisten] Failed to set up listener: SocketException: Cannot assign requested address
And this is my ubuntu firewall rules (which I just learnt that docker bypasses[*])
OpenSSH ALLOW Anywhere
1026 ALLOW Anywhere
27017 DENY Anywhere
27017 ALLOW 127.0.0.1
27017 ALLOW 172.18.0.3
OpenSSH (v6) ALLOW Anywhere (v6)
1026 (v6) ALLOW Anywhere (v6)
27017 (v6) DENY Anywhere (v6)
[*] https://www.techrepublic.com/article/how-to-fix-the-docker-and-ufw-security-flaw/
I have also made the fix suggested by the tutorial but if restart the docker then (for some unknown reason) I stop getting access to 1026 port which should be the only public port.
The Compose ports: setting is what makes a container accessible from outside Docker space. It's not necessary (or used) for connections between containers. Deleting this should meet your needs.
version: '3.8'
services:
orion:
image: fiware/orion-ld
ports:
- "1026:1026" # accessible from host port 1026
depends_on:
- mongo-db
command: -dbhost mongo-db -logLevel DEBUG
# same hostname ^^^^^^^^ and default MongoDB port 27017
# vvvvvvvv
mongo-db:
image: mongo:3.6
command: --nojournal
volumes:
- mongo-db:/data
# no ports:, not accessible from outside Docker
volumes:
mongo-db:
(I've also removed unnecessary container_name:, hostname:, networks:, and expose: options from this setup; they make no difference and the Compose stack should work just fine without them.)
Put it on its own network (i.e. new docker network: orion_net) and only expose the ports you need.
Currently you are on "default" network, which is shared.
I run a container with the oficial mongo image https://hub.docker.com/_/mongo
Here my docker-compose.yml file:
services:
mongo:
image: mongo
container_name: mongo-integrator
restart: always
env_file: prod.env
ports:
- 27017:27017
volumes:
- ../mongo/data/db:/data/db
- ../mongo/custom-conf:/etc/custom-conf
command: --config /etc/custom-conf/mongod.conf
Here my mongod.conf file:
net:
bindIpAll: true
aws security group:
Custom TCP TCP 27017 0.0.0.0/0 –
Custom TCP TCP 27017 ::/0 –
I've had success on connect with mongodb from another docker-compose service, but I can't connect from my computer.
It just was my anti virus (Malwarebytes).
So, the answer: verify and disable your anti virus software
I have the following docker compose file that I use to spin up a mongo docker container which works as intended.
However, I cannot seem to connect to the container from compass on my local machine.
I executed docker inspect on my mongo container and got its IP and tried to connect using compass, but it didn't work - connection timed out.
Is it the IP of my docker network or the IP of the mongo container I need?
docker compose file:
version: "3"
services:
pokerStats:
image: pokerStats
container_name: pokerStats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
I was able to get this working using the top answer on this question.
I had to change my docker-compose file to expose port 27018 on my local:
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
And my connection details on Compass to be:
You are mapping port 27017 from your host machine to port 27017 on the container. So the following connection string should work mongodb://localhost:27017
I'm trying to deploy a minimal mongodb cluster into my development swarm environment like this:
Up to now:
version: "3.3"
networks:
net:
driver: overlay
services:
data1:
image: mongo:3.6
container_name: data1
command: mongod --shardsvr --replSet datars --smallfiles --port 27017
expose:
- 27017
networks:
- net
cfg1:
image: mongo:3.6
container_name: cfg1
command: mongod --configsvr --replSet cfgrs --smallfiles --port 27017
expose:
- 27017
networks:
- net
mongos1:
image: mongo:3.4
container_name: mongos1
command: mongos --configdb cfgrs/cfg1:27017
expose:
- 27017
networks:
- net
Into my config server, I'm getting these message:
2019-09-06T09:22:15.693+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s,
Any ideas?
This is docker-compose.yml
version: "3"
services:
web:
build: ./rqiim
command: curl "127.0.0.1:27017"
volumes:
- ./rqiim:/app
- /app/node_modules/
environment:
- MONGO_HOSTNAME=127.0.0.1
ports:
- "8080:8080"
networks:
- host
mongodb:
image: mongo:3.4.18-jessie
container_name: mongodb
volumes:
- /data/db:/data/db
- ./mongo:/etc/mongo
command: --config /etc/mongo/mongod.conf
hostname: mongod1
ports:
- "27017:27017"
networks:
- host
networks:
host:
When I run docker-compose up I get Failed to connect to 127.0.0.1 port 27017: Connection refused
What seems to be the problem, they are on the same network plus I connect to MongoDB container from outside off docker locally just fine.
Here is the content of /etc/mongo/mongod.conf
storage:
dbPath: /data/db
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
replication:
replSetName: rs0
As everywhere else in Docker, 127.0.0.1 usually means "this container". The various mentions of host in your docker-compose.yml file actually create a bridge-type NAT network that happens to be named "host"; it does not enable host networking mode. This having been said, host networking mode is usually unnecessary, so let's get this right:
First of all, Docker Compose on its own will automatically create a private bridge-type network for each separate docker-compose.yml file, so you don't need any networks: blocks at all here. Since it does this, the names of the service blocks web and mongodb will be valid host names for one container to reach another.
In the web: service, you're replacing its command with a curl command. Once that command completes the service will exit, which almost certainly isn't what you want. You haven't included the relevant Dockerfile, but I'll guess it ends with a line like CMD ["npm", "start"] that gives a more useful default command. The volume declarations are also a little odd: having volumes to hold code isn't a typical use case and you'd usually COPY the (built) application tree into the image in the Dockerfile. (Using volumes to hold persistent data and inject config files, like you have for the mongodb: service, is very reasonable.)
You also don't need to explicitly set the container_name: or hostname: of individual containers except in really unusual circumstances.
Stripping that out and fixing the host name, we should get:
version: '3'
services:
web:
build: ./rqiim
environment:
- MONGO_HOSTNAME=mongodb
ports:
- "8080:8080"
mongodb:
image: mongo:3.4.18-jessie
volumes:
- /data/db:/data/db
- ./mongo:/etc/mongo
command: --config /etc/mongo/mongod.conf
ports:
- "27017:27017"