Unable to connect to mongodb service from another service - mongodb

OS: MacOS mojave
version: '3.1'
services:
apiWithKubernetesMongodb:
container_name: apiWithKubernetesMongodb
image: mongo
ports:
- "27018:27018"
expose:
- "27018"
command: --bind_ip_all --port 27018
hostname: mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: mongousradmin
MONGO_INITDB_ROOT_PASSWORD: mongopassadmin
MONGO_INITDB_DATABASE: admin
networks:
- apinet
apiWithKubernetes:
container_name: api_with_kubernetes
image: sherryummen/api_with_kubernetes:v1
build:
context: .
dockerfile: Dockerfile
networks:
- apinet
volumes:
- .:/go/src/api_with_kubernetes
ports:
- 8080:8080
depends_on:
- apiWithKubernetesMongodb
links:
- apiWithKubernetesMongodb
networks:
apinet:
driver: bridge
const (
HOST = "apiWithKubernetesMongodb"
PORT = 27018
DBNAME = "service_db"
COLLECTIONNAME = "user_info"
ROOTUSER = "mongousradmin"
ROOTPASSWORD = "mongopassadmin"
)
client, err := mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s#%s:%d/admin", ROOTUSER, ROOTPASSWORD, HOST, PORT)))
I have thee above docker file and go api which should connect to mongodb.
But my go code gives error "server selection timeout\ncurrent topology: Type: Unknown\nServers:\nAddr: apiwithkubernetesmongodb:27018, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(apiwithkubernetesmongodb:27018[-249]) connection is closed"
Note: For those who stumble to this, it was all about casing of the name of the service, after switching to all lower case naming, it starts working.

Related

Access MongoDB running in a container from local (host) machine [pymongo]

I know this should have been a straight-forward issue as I did it before (but forgot how): I have my MongoDB running on a docker container as a service in my docker-compose that includes an API built with FastAPI. I'm able to connect to the database from my api container with the following code:
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
client = MongoClient('mongodb://USERNAME:PASSWORD#mongo:27017/', server_api=ServerApi('1'),
serverSelectionTimeoutMS=5000)
client.server_info()
Now when I try to do the same from a Jupyter notebook on my local machine, I get this error:
ServerSelectionTimeoutError: mongo:27017: [Errno 8] nodename nor servname provided, or
not known, Timeout: 5.0s, Topology Description: <TopologyDescription id:
63b6adb8b6aabb1624b50879, topology_type: Unknown, servers: [<ServerDescription
('mongo', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongo:27017:
[Errno 8] nodename nor servname provided, or not known')>]>
This is my docker-compose configuration that used to work just fine when I was able to connect to my mongodb from local (so I don't think there is an issue here):
version: '3.8'
services:
api:
container_name: XXXXX
restart: unless-stopped
build:
context: .
dockerfile: ./API/API.Dockerfile
volumes:
- "./:/API"
- type: bind
source: XXXXX
target: /Data
ports:
- "5001:5001"
depends_on:
- mongo
env_file:
- ./secrets/secrets.env
mongo:
container_name: mongo
ports:
- 27017:27017
image: mongo
restart: always
env_file:
- ./secrets/secrets.env
mongo-express:
container_name: Mongo_Express
image: mongo-express
restart: always
ports:
- 8081:8081
env_file:
- ./secrets/secrets.env

How to connect to MongoDB replica set?

I want to connect to MongoDB cluster using
mongodb://localhost:27017
It shows me an error
getaddrinfo ENOTFOUND messenger-mongodb-1
This is my docker-compose.yml file
version: '3'
services:
messenger-mongodb-1:
container_name: messenger-mongodb-1
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27017:27017
networks:
- messenger-mongodb-cluster
volumes:
- messenger-mongodb-1-data:/data/db
depends_on:
- messenger-mongodb-2
- messenger-mongodb-3
healthcheck:
test: test $$(echo "rs.initiate({_id:\"messenger-mongodb-replica-set\",members:[{_id:0,host:\"messenger-mongodb-1\"},{_id:1,host:\"messenger-mongodb-2\"},{_id:2,host:\"messenger-mongodb-3\"}]}).ok || rs.status().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s
messenger-mongodb-2:
container_name: messenger-mongodb-2
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27018:27017
networks:
- messenger-mongodb-cluster
environment:
- MONGO_INITDB_DATABASE=messenger-db
volumes:
- messenger-mongodb-2-data:/data/db
messenger-mongodb-3:
container_name: messenger-mongodb-3
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27019:27017
networks:
- messenger-mongodb-cluster
environment:
- MONGO_INITDB_DATABASE=messenger-db
volumes:
- messenger-mongodb-3-data:/data/db
networks:
messenger-mongodb-cluster:
volumes:
messenger-mongodb-1-data:
messenger-mongodb-2-data:
messenger-mongodb-3-data:
I run it like
docker-compose up -d
How can I connect to my replica set?
I want to use it for the local development of my node.js application
My operating system is Windows 11 Pro

connect EHOSTUNREACH 172.24.0.2:27017 docker-compose

I am currently unable to connect to mongo when I run docker-compose, I keep on getting the error below
connect EHOSTUNREACH 172.25.0.2:27017
Here is my docker-compose.yml file
version: "3.4"
services:
app:
container_name: checki
restart: always
build:
context: .
network: host
ports:
- "3000:9000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27018:27017"
and my db connection string is like this
"mongodb://mongo:27017/checki"
Please assist

DB cannot be found in Mongo container in docker

The app (produced by docker-compose up) works as expected. But when I entered the mongo container (docker exec -it mongo) I cannot find db chatmongoose.
connectionString = 'mongodb://mongo:27017/chatmongoose'
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
version: '3.7'
services:
server:
build:
context: ./server
dockerfile: Dockerfile
image: myapp-server
container_name: myapp-node-server
command: /usr/src/app/node_modules/.bin/nodemon server.js
ports:
- '5000:5000'
links:
- mongo
environment:
- NODE_ENV=development
networks:
- app-network
mongo:
container_name: mongo
image: mongo
volumes:
- data-volume:/data/db
ports:
- '27017:27017'
networks:
- app-network
client:
build:
context: ./client
dockerfile: Dockerfile
image: myapp-client
container_name: myapp-react-client
command: npm start
depends_on:
- server
ports:
- '3000:3000'
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
data-volume:
node_modules:
web-root:
driver: local
The data in app did work as expected but why I cannot find the db in container?
Could you try profile. It probably only start mongo but also volume will be valid.
docker-compose --profile mongo up
mongo:
profiles: ["mongo"]
container_name: mongo
image: mongo
volumes:
- data-volume:/data/db
ports:
- '27017:27017'
networks:
- app-network

can't connect to local postgres from docker container

I have some docker containers such as php, nginx, etc. A also have postgres locally because I have learned that dabatase inside docker container is a bad practise. But I can't connect to local postgres from docker container.
At this moment I have done next
In postgresql.conf I changed listen_addresses
listen_addresses = '*'
In pg_hba.conf I have added next line
host all all 0.0.0.0/0 md5
The I executed next command for iptables
iptables -I INPUT -p tcp -m tcp -s 0.0.0.0 --dport 5432 -j ACCEPT
Then I restarted postgres.
My database configuration
DB_CONNECTION=pgsql
DB_HOST=my_server_ip_address
DB_PORT=5432
DB_DATABASE=mydbname
DB_USERNAME=mydbuser
DB_PASSWORD=mydbpasswd
But i still can't connect to posgresql. At the same moment I can connect to postgres via psql or phpstorm
My docker-compose.yml
version: '3.7'
networks:
backend-network:
driver: bridge
frontend-network:
driver: bridge
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- ./:/var/www/k4fntr
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&queue-service queue:
<<: *app-service-template
container_name: k4fntr_queue
restart: always
hostname: *queue-service
depends_on:
- app
environment:
CONTAINER_ROLE: queue
&schedule-service schedule:
<<: *app-service-template
container_name: k4fntr_schedule
restart: always
hostname: *schedule-service
depends_on:
- app
environment:
CONTAINER_ROLE: scheduler
&sportlevel-listener sportlevel_listener:
<<: *app-service-template
container_name: k4fntr_sl_listener
restart: always
hostname: *sportlevel-listener
ports:
- "${SPORTLEVEL_LISTEN_PORT}:${SPORTLEVEL_LISTEN_PORT}"
depends_on:
- app
environment:
CONTAINER_ROLE: sl_listener
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize
networks:
- backend-network
- frontend-network
echo-server:
container_name: k4fntr_echo
image: oanhnn/laravel-echo-server
volumes:
- ./:/app
environment:
GENERATE_CONFIG: "false"
depends_on:
- app
ports:
- "6001:6001"
networks:
- backend-network
- frontend-network
nginx:
container_name: k4fntr_nginx
image: nginx
volumes:
- ./docker/nginx/config:/etc/nginx/conf.d
- ./:/var/www/k4fntr
depends_on:
- *php-fpm-service
ports:
- "${NGINX_LISTEN_PORT}:80"
networks:
- frontend-network
redis:
container_name: k4fntr_redis
image: redis
restart: always
command: redis-server
volumes:
- ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./docker/redis/redis-data:/data:rw
ports:
- "16379:6379"
networks:
- backend-network