I am trying to create a docker compose file with Spring Boot and Mongo Db but unfortunately I was not successful so far. I checked many pages and videos on the Internet and tried the solutions but it did not work for me so far. Is there anyone else face such issue and solve it somehow?
here is my docker compose file
version: '3'
services:
mongodb:
image: mongo:4.0.7
ports:
- "27017:27017"
expose:
- 27017
networks:
- mynetwork
volumes:
- ./data/mongo:/data/db
api:
build: ../
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=prod
depends_on:
- mongodb
networks:
- mynetwork
networks:
mynetwork:
driver: bridge
and here is my application-prod.yml file
server:
port: 8080
spring:
data:
mongodb:
host: mongodb
database: book-retailer
port: 27017
Related
I am trying to connect to mongoDB in a container from a service in another container but getting connection exceptions
I have created a docker-compose.yml
version: "3"
services:
mongo-container:
image: mongo:latest
container_name: "mongo-container"
environment:
- MONGO_INITDB_DATABASE=transactions
ports:
- 27017:27017
networks:
- banking-network
volumes:
- mongodb_data_container:/data/db
transaction-service:
image: transaction-service
container_name: transaction-service
build:
context: transaction-service
dockerfile: local.dockerfile
ports:
- "8083:8083"
depends_on:
- mongo-container
environment:
- MONGO_URL=mongodb://mongo-container:27017/transactions
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
volumes:
mongodb_data_container:
And in my service application.yml
spring:
application:
name: transaction-service
server:
port: '8083'
mongodb:
connection:
string: mongodb://mongo-container:27017/transactions
database: transactions
So I have a service in one container, and mongoDb in another, both on the same network
d666b7a242fc transaction-service "java -jar /app/tran…" 18 minutes ago Up 18 minutes 0.0.0.0:8083->8083/tcp transaction-service
516d539b29c7 mongo:latest "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:27017->27017/tcp mongo-container
The following is MONGO_URL=mongodb://name of container:27017/name of database
- MONGO_URL=mongodb://mongo-container:27017/transactions
Now I can connect to this mongodb from a rest service running on my local host without needing a password and username so I have not added it to my docker-compose. I have seen other examples not using it.
But I am still getting an exception.
It seems this should be straightforward, it has the docker container name, the port and its on the same network. What am I missing.
Thanks for any help.
I found wiring up that does allow one container to talk to another.
The confusing thing, is its totally different to how the docs say to do it
.
in my application.yml I have
spring:
application:
name: transaction-service
data:
mongodb:
database: Transaction
host: mongo-container
port: 27017
server:
port: '8083'
And in my docker-compose
services:
mongo-container:
image: mongo:latest
container_name: "mongo-container"
ports:
- 27017:27017
networks:
- banking-network
volumes:
- mongodb_data_container:/data/db
transaction-service:
image: transaction-service
container_name: transaction-service
build:
context: transaction-service
dockerfile: local.dockerfile
ports:
- "8083:8083"
depends_on:
- mongo-container
links:
- mongo-container
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
volumes:
mongodb_data_container:
Its confusing that, what should work, what the documents detail does not work.
But this does.
How to change this script so that I can connect remotely to my Mongodb running in docker-compose, from different machines (that are not connected to the same network/internet provider).
I want to allow all remote connections.
I don't care about security matters as it's just for practice purposes!
docker-compose.yaml script file:
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=admin
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network
I solved my issue by migrating my data to Atlas cloud.mongodb.com
Answer update: To provide more info as suggested by #nuhkoca, This is the video tutorial to create a mongodb atlas: https://www.youtube.com/watch?v=xrc7dIO_tXk&t=15s And this is the link to the db from my resources file in my Springboot backend api:
server.port=<Port_number>
spring.data.mongodb.uri=<Link_to_mongodb_atlas>
I am trying to configure postgres to run with springboot and metabase. Each service is running separately alone but when I try to put the 3 together in a docker-compose file, I am getting the following error :
Caused by: org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
metabase-container | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
However, I have mapped the port 5432 of db to the port 5432 of the metabase container.
And yet, It doesn't seem to work. Any help on this issue? (please find my docker-compose file below)
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=compose-postgres
- SPRING_DATASOURCE_PASSWORD=same
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=********
- METABASE_PASSWORD=same
ports:
- "8078:5432"
- "54320:5432"
metabase:
container_name: metabase-container
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
- "5432:5432"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=db
- MB_DB_PORT=5432
- MB_DB_USER=compose-postgres
- MB_DB_PASS=same
- MB_DB_HOST=0.0.0.0
- MB_ENCRYPTION_SECRET_KEY=********
Try changing MB_DB_HOST in the metabase environmental variables from 0.0.0.0 to db.
The subtilty I hadn't understood is that all the services are in the same network. Hence, deleting port forwarding (which exposes the services to outside components but is irrelevant for communication within the container) between the services and changing the Metabase configuration file does the job. Here is the modified docker-compose.yml file :
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=********
- SPRING_DATASOURCE_PASSWORD=**********
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=compose-postgres
- METABASE_PASSWORD=compose-postgres
ports:
- "8078:5432"
metabase:
container_name: metabase-container
depends_on:
- db
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=********
- MB_DB_PORT=5432
- MB_DB_USER=************
- MB_DB_PASS=compose-postgres
- MB_DB_HOST=db
- MB_ENCRYPTION_SECRET_KEY=***********
I have a implemented a microservice architecture with several servers and databases. I have installed elasticsearch with docker and when I do docker-compose up, everything seems to run fine.
However I would like to integrate the elasticsearch with the several databases (2 mongodb in this sample below) in the system. How do I synch the two mongodb in two different containers with elasticsearch so that I can search them?
client:
container_name: client
stdin_open: true
build:
context: ./client
dockerfile: Dockerfile
restart: always
volumes:
- './client:/app'
ports:
- '1000:3000'
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true
weatherdb:
container_name: weather-db
image: mongo
restart: always
ports:
- '2002:27017'
volumes:
- ./weather_service/weather_db:/data/db
networks:
- backend
weather-service:
container_name: weather-service
build: ./weather_service
restart: always
ports:
- "1002:3000"
depends_on:
- weatherdb
links:
- elasticsearch
networks:
- backend
newsdb:
container_name: news-db
image: mongo
restart: always
ports:
- '2003:27017'
volumes:
- ./news_service/news_db:/data/db
networks:
- backend
news-service:
container_name: news-service
build: ./news_service
restart: always
ports:
- "1003:3000"
depends_on:
- newsdb
links:
- elasticsearch
networks:
- backend
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.0
container_name: elasticsearch
restart: always
ports:
- 9200:9200
- 9300:9300
environment:
ES_JAVA_OPTS: '-Xms512m -Xmx512m'
network.bind_host: 0.0.0.0
network.host: 0.0.0.0
discovery.type: single-node
volumes:
- ./elasticsearch/esdata:/usr/share/elasticsearch/data
networks:
- backend
Its very simple to just add a elasticsearch docker section in any docker-compose file and start it, all these are independent docker containers and as long as their exposed port on host is not interfering each other and you have the correct configuration in place it should work.
Please refer elasticsearch multi-docker installation using docker file for more info.
NOTE: You have not mentioned what exact issue you are facing, you have mentioned everything ie all docker containers are running file, so please explain in detail what exactly you are trying to solve
Here is my docker compose file:
version: "3.3"
services:
test:
image: test
networks:
- mongo_net
ports:
- 4000:80
depends_on:
- mongodb
links:
- mongodb
mongodb:
image: mongo:latest
networks:
- mongo_net
ports:
- 27017:27017
volumes:
- local_data:/data/db
volumes:
local_data:
networks:
mongo_net:
driver: bridge
The 'test' image cannot find the 'mongodb' instance.
My assumption is that the 'links' section would connect the two, but it is not happening.
What am I missing?
for your compose file try just using depends_on. links is deprecated and maybe thats why you are currently getting issues since this is a V3 compose file.