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.
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
Related
Similar to Can't connect to MongoDB container from other Docker container - but answers from this post don't work for me.
I am new to Docker. Trying to learn it on a typescript/express/mongo/mongoose api example.
What I am trying to do (and having problems with), is to use mongo cmd line on a running mongo container after it has been spun up using docker compose up. Even though I have my data nicely persisted on a Docker volume, I don't seem to be able to log into the database using cmd line.
This is my docker-compose.yml file:
version: '3.9'
services:
api:
container_name: api_ts
build: .
restart: unless-stopped
environment:
- DB_URL=mongodb://myself:pass123#mongo:27017/
ports:
- '3131:3131'
depends_on:
- mongo
links: # (seems to be needed)
- mongo
mongo:
container_name: mongo_container
image: mongo:latest
restart: always
volumes:
- mongo_dbv:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=myself
- MONGO_INITDB_ROOT_PASSWORD=pass123
ports:
- '27017:27017'
volumes:
mongo_dbv: {}
This is my Dockerfile:
FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json .
RUN npm ci
COPY . .
ENV PORT=3131
EXPOSE 3131
COPY .env ./dist
CMD ["npm", "start"]
I am running
docker compose up -d --build
After both services are ready, I do:
docker exec -it mongo_container mongo
show dbs
...and the output of the last cmd is empty
(same occurs when trying to follow the answers in the post mentioned above)
I am sure the database contains data, because I am able to verify it using REST client.
Also, I am a bit puzzled - and maybe this is somehow connected - why there is no indication, either in docker-compose.yml or in Dockerfile, of the database name which I am using. I would expect it to be part of show dbs output. Despite that, my api runs just fine.
Listing databases requires authentication
docker exec -it mongo_container mongo -u myself -p pass123
Now you can list databases
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
Note: mongo should show you warning that "mongo" shell has been superseded by "mongosh". When you use mongosh, a proper authentication error would be shown on the database listing attempt.
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: The connection attempt failed.
I get the above error when entering sbt run However, inside my docker containers everything works fine.
Inside the first container I have a postgres database. The second container I have an image built from my project folders. When I run docker-compose up --build everything works fine.
I suspect the project (actual codebase) can't see the postgres database in docker-compose container.
Do I need another postgres database outside the docker-compose containers to go with my project code outside the containers?
docker-compose.yml file.
version: '3.6'
services:
# App Backend PostgreSQL
postgres:
container_name: sportsAppApiDb
image: postgres:11.7-alpine
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/sportsappapi
POSTGRES_DB: sportsappapi
POSTGRES_HOST: postgres
ports:
- "5432:5432"
# App Backend
sports-app-api:
container_name: sportsAppApi
build: ./
volumes:
- ./:/usr/src/sports-app-api
command: sbt run
working_dir: /usr/src/sports-app-api
ports:
- "8000:8000"
environment:
POSTGRES_URI: postgres://admin:password#postgres:5432/sportsappapi
Entrypoint for scala project
object SportsAppApiStartup extends App {
SportsAppApiDb(SportsAppApiConfig.appDb).init
WebServer(Endpoints.handler, 8000).start()
println(s"Running sports-app-api on port: 8000")
}
Your database is not accessible outside of docker-compose under postgres:5432. Try to connect to it through psql or pgcli or other client and you'll see.
When you'll call docker-compose ps or docker ps you'll be able to see how to connect to Postgres docker image (under ports) - most likely it will be something like 0.0.0.0:5432.
E.g. if I have:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d90871418bcb postgres "docker-entrypoint.s…" 2 weeks ago Up 4 days 0.0.0.0:7766->5432/tcp postgres_container
it means that Postgres was available under 0.0.0.0:7766 from outside Docker.
This has nothing to do with Scala, sbt and slick as far as I can tell.
I am trying to making it up mongodb container but i am getting error
Starting v2_mongodb ... done
Starting rockmongo_v2 ... done
Attaching to v2_mongodb, rockmongo_v2
v2_mongodb | Starting mongod...
v2_mongodb exited with code 100
Here is the content of docker-compose.yml file and output of docker ps
version: '2'
services:
v2_db:
image: sameersbn/mongodb:latest
container_name: "v2_mongodb"
ports:
- "27017:27017"
volumes:
- ./data/db:/data/db:rw
- ./data/db:/var/lib/mongodb:rw
environment:
- MONGO_DATA_DIR=/data/db
command: mongod --verbose --smallfiles --dbpath=/data/db # --quiet
rockmongo_v2:
image: javierjeronimo/rockmongo:latest
container_name: "rockmongo_v2"
ports:
- "27118:80"
links:
- v2_db:mongo
depends_on:
- v2_db
docker ps output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94794d6f3ff1 javierjeronimo/rockmongo:latest "/bin/sh -c 'service…" 38 minutes ago Up About a minute 0.0.0.0:27118->80/tcp rockmongo_v2
bd4ed92796db sameersbn/mongodb:latest "/sbin/entrypoint.sh…" 38 minutes ago Exited (100) About a minute ago v2_mongodb
I am not able to get the clue what can be the possible cause of above error?
List your volume links:
docker volumes ls
Delete the volumes: (In my case this freed up about 29.35 Gig)
docker volumes prune
I am trying to use Docker for the first time. I have a MEAN stack app that I am trying to create a Dockerfile and docker-compose file for. My Node server is running and displaying the Angular front end, however I get ERR_EMPTY_RESPONSE when the app tries to make the initial requests to my Mongo database, and does not save data to the database either.
My docker-compose file looks like this:
version: "2"
services:
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
My Dockerfile looks like this:
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm i
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
And in my server.js file, I have these lines of code:
const MONGODB_URI = 'mongodb://mongo:27017/myDB';
// connect to mongodb
mongoose.Promise = Promise;
mongoose.connect(MONGODB_URI, { useNewUrlParser: true })
.catch((err) => console.error(err));
I can't seem to figure out what might be causing this, any help would be greatly appreciated. When I run docker container ls it shows two containers, one for my app and one for mongo, but mongo does not appear to be working.
Edit: Here is the output of docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
16451b5b6f9d myapp_app "npm start" 2 days ago Up 2 days 0.0.0.0:3000->3000/tcp app
ad21af079c62 mongo "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:27017->27017/tcp mongo
I am having a strange situation where I can not connect to my running mongo DB in my docker compose. My compose file:
version: '3'
services:
app:
image: myimage:latest
ports:
- "8080:8080"
external_links:
- myname:mongo
environment:
- MONGO_URL=mongodb://myname:27017/test
I have found a few infos on that that all did not solve my issue. I.e. I tried:
1)
Create a custom network:
docker network create mongonet
Then start mongo with the --network mongonet flag and add to the compose:
networks:
default:
external:
name: mongonet
Got nothing there either.
I looked into my /etc/hosts file on my compose, and it did not list any DNS entry.
If i do a docker inspect and grab the mongo IP and add it to my compose, that is fine and works like a charm.
I start mongo like this:
docker run -d -p 27017:27017 -v ~/mongo_data:/data/db mongo
I am really rather confused as I believed this to be a out-of-the-box kind of thing. Strangely I can't make it work. I have found examples on internal links (vs external_link) but that does not work for me as I have many services that I would like to run like this and not all of them should run at the same time.
I start my docker compose as this:
docker-compose up --force-recreate
My versions are:
docker-compose version 1.17.1, build 6d101fb
Docker version 17.05.0-ce, build 89658be
My question: How do I successfully link a running mongo container as an external link into my application containers such that they can connect to them?
My docker PS:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5cf6e08d6fde mongo "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:27017->27017/tcp gallant_feynman
Links are deprecated, use networks instead.
Notes:
If you’re using the version 2 or above file format, the
externally-created containers must be connected to at least one of the
same networks as the service which is linking to them. Links are a
legacy option. We recommend using networks instead.
The network way should work. I think you are missing some pieces. Make sure to give the mongo container a name, and make sure to attach the app container to the network in the compose file:
docker network create mongonet
docker run -d -p 27017:27017 --network mongonet --name mongo -v ~/mongo_data:/data/db mongo
version: '3'
services:
app:
image: myimage:latest
ports:
- "8080:8080"
environment:
- MONGO_URL=mongodb://mongo:27017/test
networks:
- mongonet
networks:
default:
external:
name: mongonet