Reputation: 21
In order to connect spring boot to mongoDB, i am using docker. And i created docker images for mongo and springboot application.
In the beginning running container is:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
C:\Users\ASUS>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-mongodb 1.0 5a1cf26b0e0b 5 minutes ago 550MB
mongo latest d98599fdfd65 2 days ago 696MB
However for making connection for them i used below command.
C:\Users\ASUS>docker run -p 8080:8080 --name springboot-mongodb --link mongodbproject:mongo -d springboot-mongodb:1.0
36120b50f09ae07c7c88ca10b1f478d726a1a5c318ee0c9d0f0fc3fb9eff5750
After that i can not see springboot-mongodb in running containers:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
After that when i check Docker application it says status for springboot-mongodb as exited(1). when i was trying to run it again, it stops again.
Upvotes: 1
Views: 102
Reputation: 191671
Your Spring application is dying. Don't use -d
, then look at the logs, and fix the error that is shown. Or use docker ps -a
, then get the ID of the killed container, and run docker logs <id>
Ideally, you'd use Docker Compose, anyway. The --link
option is deprecated.
x-mongo: &mongo-opts
MONGODB_USERNAME: mongoUser
MONGODB_PASSWORD: mongoPass
MONGODB_DATABASE: example
version: '2'
services:
web:
image: springboot-mongodb:1.0
ports:
- "8080:8080"
environment:
<<: *mongo-opts
MONGODB_PROTOCOL: mongodb # Example variables. Added in case you wanted to change to mongo+srv://
MONGODB_HOST: mongodb:27017
depends_on:
- mongodb
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
<<: *mongo-opts
MONGODB_ROOT_PASSWORD: l0c@l
Upvotes: 1