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

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

Related

mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"

When trying to spin up a docker container with docker-compose,
I get the following error message:
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery
and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"
I noticed that mongo-express always adds a double quote to the end of my connection string, is this intentional or did I mess something up in my docker-compose file?
The file:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
networks:
- backbone
expose:
- 27017
ports:
- 27017:27017
env_file:
- ./.env
command: [--auth]
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ./data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: unless-stopped
networks:
- backbone
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=127.0.0.1
- ME_CONFIG_MONGODB_URL="mongodb://127.0.0.1:27017"
- ME_CONFIG_BASICAUTH_USERNAME=${DB_USERNAME}
- ME_CONFIG_BASICAUTH_PASSWORD=${DB_PASSWORD}
networks:
backbone:
driver: bridge
I already tried updating my version of docker/docker-compose and mongodb.
How do I resolve the error?
Or is there a workaround I could use?
In a container, localhost refers to the container itself. So when Mongo Express tries to connect to the database at localhost, it looks for the database inside the Mongo Express container.
Docker compose let's you refer to containers using their container names, so you should change your Mongo Express environment variables to
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_URL="mongodb://mongodb:27017"

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?

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.

docker springboot only connects on postgres 5432 via docker-compose

Using docker to connect springboot to postgres via docker-compose. Using port 5432 on postgres works fine, if i try an port other than that it fails
working code
spring
spring.datasource.url=jdbc:postgresql://db:5432/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 5432:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
**connection refused code **
spring
spring.datasource.url=jdbc:postgresql://db:6000/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 6000:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
error:
spring-boot-rest-server | org.postgresql.util.PSQLException: Connection to db:6000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
What am i doing wrong?
You are confusing a bit the ports: your "db" container only exports 1 port: 5432. The 6000 that you put in your docker-compose is the port on localhost that you map to that container (db) on that port (5432).
You shouldn't even use the port mappings for the postgres container unless you want to connect from localhost which I guess you don't.
If you want to use another port than 5432 you need to extend the postgres Dockerfile and change the configuration so that postgres starts listening on a different port.
Hope this helps.
In other words: The port mapping configured in docker-compose has no relevancy to how the containers connect to each other. The mapping is only relevant when something/someone attempts to connect to your containers within the docker-compose from the outside. (Like from the localhost, as #Mihai remarked.)