Not able to connect SpringBootApplication to mongo container in Docker - mongodb

I'm having docker container up and running by using the following command:
docker run -p 27017:27017 -d mongo
Docker Logs for reference.
Then I clone a github repo: https://github.com/springframeworkguru/spring-boot-mongodb.git
Import the project in IntelliJ IDE, build it and run.
SpringBoot App Error Logs here
Issue : I'm not able to connect to the mongo app running in the container from my SpringBoot application as I'm getting MongoSocketOpenException as shown in the logs.
Any help is appreciated?
Docker version 18.03.0-ce, build 0520e24302
OS: Windows 10

Docker for win has some problems with loopback interfaces.
https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/
Try run docker inspect and configure your application with the container address.

Problem: I was trying to configure SpringBoot Application to mongo container IP which I retrieved from mongo inspect <mongo_container> command which was incorrect.
Solution: Configured my application using docker IP retrieved by docker-machine <env> which resolved the problem.
Link to the post is here.

Related

Can't connect to MongoDB docker from Compass

I created a MongoDB docker instance as follows:
sudo docker run --name mongo -d -p 27000:27000 mongo
I confirmed that the instance is running by using command sudo docker ps --all :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f509276287eb mongo "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:27000->27000/tcp, :::27000->27000/tcp, 27017/tcp mongo
Then I tried to connect to it using Compass GUI tool with the following connection string:
mongodb://localhost:27000/
I get a screen the following screen:
and after waiting for a while, the screen disappears with the following error message
connection <monitor> to 127.0.0.1:27000 closed
My goal is to use this docker image to run tests, but I cannot connect to it.
I tried using 0.0.0.0 instead of localhost but it didn't work.
My Question: How do I connect to MongoDB docker image?
Environment info:
OS is Ubuntu 22.04
Docker version 20.10.14, build a224086349 (snap package)
Compass version 1.33.1 (deb package)
I figured it out. I made a silly mistake when picking the port, I should have used:
sudo docker run --name mongo -d -p 27000:27017 mongo
Because Mongo will always be on port 27017 inside the image.

How to connect to local mongodb from docker container of flask application

I have a flask application which is running as docker container. Flask application uses local mongodb. This docker container is not able to connect to local mongodb.
I have tried following option :
set --network="host" in docker run ... command
set MONGO_URI = "mongodb://host-ip-address:27017/model-service-sample",
set MONGO_URI = "mongodb://container-gateway-ip-address:27017/model-service-sample"
None of the above options worked.
Can anyone please suggest a way to accomplish this?
For MacOS you should use:
host.docker.internal or gateway.docker.internal
for connecting from a container to a service on the host.
refer : https://docs.docker.com/docker-for-mac/networking/#/known-limitations-use-cases-and-workarounds

How to connect SpringBoot application inside Docker to outside PostgreSQL

I have Java SpringBoot Web-application working on host machine. Application connects to PostgreSQL database. All works well. OS - Ubuntu 18.
Now i need to move application in Docker container, except for PostgreSQL which will remain on host machine.
I installed Docker, rise up container, but my app inside docker cannot connect to PostgreSQL database with default settings (localhost).
Here is my application.properties file:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/webdemodb
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.generate-ddl=true
Here is my Dockerfile:
FROM java:8
WORKDIR /
ADD target/webaccount-1.0-SNAPSHOT.jar app.jar
EXPOSE 8080
RUN fc-cache -f -v
ENTRYPOINT ["java","-jar","/app.jar"]
I read about Docker's networking but didn't find solution. What i need to setup?
Artemiy, thanks! For simple usage i've just applied the next option when running container:
--network="host"
Full command:
docker run -d -p 9000:8080 --network="host" --name webaccount webaccount:1.0

How to connect local Mongo database to docker

I am working on golang project, recently I read about docker and try to use docker with my app. I am using mongoDB for database.
Now problem is that, I am creating Dockerfile to install all packages and compile and run the go project.
I am running mongo data as locally, if I am running go program without docker it gives me output, but if I am using docker for same project (just installing dependencies with this and running project), it compile successfully but not gives any output, having error::
CreateSession: no reachable servers
my Dockerfile::
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang
WORKDIR $GOPATH/src/myapp
# Copy the local package files to the container's workspace.
ADD . /go/src/myapp
#Install dependencies
RUN go get ./...
# Build the installation command inside the container.
RUN go install myapp
# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/myapp
# Document that the service listens on port 8080.
EXPOSE 8080
EXPOSE 27017
When you run your application inside Docker, it's running in a virtual environment; It's just like another computer but everything is virtual, including the network.
To connect your container to the host, Docker gives it an special ip address and give this ip an url with the value host.docker.internal.
So, assuming that mongo is running with binding on every interface on the host machine, from the container it could be reached with the connection string:
mongodb://host.docker.internal:21017/database
Simplifying, Just use host.docker.internal as your mongodb hostname.
In your golang project, how do you specify connection to mongodb? localhost:27017?
If you are using localhost in your code, your docker container will be the localhost and since you don't have mongodb in the same container, you'll get the error.
If you are starting your docker with command line docker run ... add --network="host". If you are using docker-compose, add network_mode: "host"
Ideally you would setup mongodo in it's own container and connect them from your docker-compose.yml -- but that's not what you are asking for. So, I won't go into that.
In future questions, please include relevant Dockerfile, docker-compose.yml to the extent possible. It will help us give more specific answer.

configure mongo with docker on win7

I got a problem with configure connection to mongoDB via docker container in spring boot. I run mongo conteiner and it's waiting for action print screen of docker terminal but in the same time I got error in spring logs logs screen
Problem appears on win7 while working on udemy course with open source code which You can check on https://github.com/springframeworkguru/spring-boot-mongodb
On Windows, since you're running Docker Machine you need to connect to the docker machine instead of localhost. The IP will usually be 192.168.99.100, but you can check by executing the docker-machine ip default command.
So your mongo connection string will normally be something like mongodb://192.168.99.100/dbName
Hey I had same problem and solution for me was to add these two lines to specify port and host of vm and image.
spring.data.mongodb.host=your_host_ip
spring.data.mongodb.port=your_image_port
You can find both easly in Kitematic in home tab or by commands. For host_ip in command line enter ipconfig command and for image_port $docker ps to get container ID and than $docker inspect <container id>.
Hope it will help.
First do what Strelok said
docker-machine ip default and get the ip,
then start mongo
docker run -p 27017:27017 -d mongo.
The port is 27017
Then do what trajanesco suggested, edit the application.properties and add these two lines
spring.data.mongodb.host=192.168.99.100 # usually the default ip
spring.data.mongodb.port=27017