Mongodb connection refused from other application in docker-compose - mongodb

I have below mongodb configuration in docker-compose.yml file -
version: '3.7'
networks:
app-tier:
driver: bridge
mongodb:
image: 'bitnami/mongodb:latest'
container_name: "mongodb"
environment:
MONGODB_INITIAL_PRIMARY_HOST: mongodb
MONGODB_ADVERTISED_HOSTNAME: mongodb
MONGODB_REPLICA_SET_MODE: primary
MONGODB_INITDB_DATABASE: testdb
MONGODB_REPLICA_SET_NAME: rs0
ALLOW_EMPTY_PASSWORD: 'yes'
ports:
- "27017:27017"
volumes:
- ./scripts/mongorestore.sh:/docker-entrypoint-initdb.d/mongorestore.sh
- ./data/mongodb:/data/mongodb
networks:
- app-tier
infrastructure:
build:
context: .
dockerfile: Dockerfile
target: base
container_name: infra
environment:
- SPRING_PROFILES_ACTIVE=dev
- KAFKA_BROKERS=kafka:9092
- REDIS_ENDPOINT=redis
- APP_NAME=infrastructure
volumes:
- ~/.m2:/root/.m2
depends_on:
- "kafka"
- "redis"
- "mongodb"
networks:
- app-tier
Whenever I run docker-compose my app infrastructure giving below error -
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
Inside application I am not even trying to connect mongodb, I am just trying to set up my application first using docker-compose
Am I missing anything here?

Something in infrastructure image is trying to connect to mongodb. localhost is likely a default host, if you didn't set it explicitly. You need to find out who is that and set host name to mongodb

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

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

Docker: Unable to connect one container with another on same network

I've a following docker-compose.yaml file:
version: "3"
services:
# Database
database:
image: mongo
container_name: database
restart: always
volumes: ["dbdata:/data/db"]
environment:
- MONGO_INITDB_ROOT_USERNAME=myusername
- MONGO_INITDB_ROOT_PASSWORD=mypassword
ports:
- 7000:27017
command: mongod
networks:
- webapp
# Server
server:
container_name: server
restart: always
build: ./server
volumes: ["./server:/var/www", "/var/www/node_modules"]
ports:
- "9000:3000"
depends_on:
- database
networks:
- webapp
networks:
webapp:
driver: bridge
volumes:
dbdata:
Now, I'm able to connect with my mongo database from my local windows machine through mongodb client but my server container is not able connect with database container.
I'm using following connection URI on my server to establish a connection:
mongodb://myusername:mypassword#database:7000/mydatabase
It must be noted that on my local machine I'm using the same URI to connect successfully with the database, the only difference is that I'm using localhost instead of database (container name) on my local machine.
Can you please tell me what I'm doing wrong here?

How configure docker / pgadmin with nest

I'm trying to configure my docker to pgadmin4 in my nest js project, to do the backend, but when i try connect, show this message "Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting"
I already tried configure the docker-compose.yml with pgadmin configurations, network configurations and
my docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "12345678"
ports:
- "5432:5432"
volumes:
- ./data/pgadmin/
networks:
- pg-network
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "monnerat#monnerat.com.br"
PGADMIN_DEFAULT_PASSWORD: "abc"
ports:
- "5050:80"
depends_on:
- db
networks:
- pg-network
networks:
pg-network:
driver: bridge
My actual result is connection refused is server running on host "localhost (127.0.0.1) and accepting"
but i expect he open the pgadmin to create the database
Your docker-compose.yml is good. Please see the attached host name should be 'db'

Unable to connect to mongodb service from another service

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.