Using Docker for MEAN Stack Application: ERR_EMPTY_RESPONSE - mongodb

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

Related

Can't see my mongo database when using mongo cmd on a Docker container

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.

How do I properly set up my Keystone.js app to run in docker with mongo?

I have built my app which runs fine locally. When I try to run it in docker (docker-compose up) it appears to start, but then throws an error message:
Creating mongodb ... done
Creating webcms ... done
Attaching to mongodb, webcms
...
Mongoose connection "error" event fired with:
MongoError: failed to connect to server [localhost:27017] on first connect
...
webcms exited with code 1
I have read that with Keystone.js you need to configure the Mongo location in the .env file, which I have:
MONGO_URI=mongodb://localhost:27017
Here is my Docker file:
# Use node 9.4.0
FROM node:9.4.0
# Copy source code
COPY . /app
# Change working directory
WORKDIR /app
# Install dependencies
RUN npm install
# Expose API port to the outside
EXPOSE 3000
# Launch application
CMD ["node","keystone"]
...and my docker-compose
version: "2"
services:
# NodeJS app
web:
container_name: webcms
build: .
ports:
- 3000:3000
depends_on:
- mongo
# MongoDB
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db/mongo
ports:
- 27017:27017
When I run docker ps it confirms that mongo is up and running in a container...
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3e06e4a5cfe mongo "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:27017->27017/tcp mongodb
I am either missing some config or I have it configured incorrectly. Could someone tell me what that is?
Any help would be appreciated.
Thanks!
It is not working properly because you are sending the wrong host.
your container does not understand what is localhost:27017 since it's your computer address and not its container address.
Important to understand that each service has it's own container with a different IP.
The beauty of the docker-compose that you do not need to know your container address! enough to know your service name:
version: "2"
volumes:
db-data:
driver: local
services:
web:
build: .
ports:
- 3000:3000
depends_on:
- mongo
environment:
- MONGO_URI=mongodb://mongo:27017
mongo:
image: mongo
volumes:
- "db-data:/data/db/mongo"
ports:
- 27017:27017
just run docker-compose up and you are all-set
A couple of things that may help:
First. I am not sure what your error logs look like but buried in my error logs was:
...Error: The cookieSecret config option is required when running Keystone in a production environment.Update your app or environment config so this value is supplied to the Keystone constructor....
To solve this problem, in your Keystone entry file (eg: index.js) make sure your Keystone constructor has the cookieSecret parameter set correctly: process.env.NODE_ENV === 'production'
Next. Change the mongo uri from the one Keystone generated (mongoUri: mongodb://localhost/my-keystone) to: mongoUri: 'mongodb://mongo:27017'. Docker needs this because it is the mongo container address. This change should also be reflected in your docker-compose file under the environment variable under MONGO_URI:
... environment: - MONGO_URI=mongodb://mongo:27017 ...
After these changes your Keystone constructor should look like this:
const keystone = new Keystone({
adapter: new Adapter(adapterConfig),
cookieSecret: process.env.NODE_ENV === 'production',
sessionStore: new MongoStore({ url: 'mongodb://mongo:27017' }),
});
And your docker-compose file, something like this (I used a network instead of links for my docker-compose as Docker has stated that links are a legacy option. I've included mine in case its useful for anyone else):
version: "3.3"
services:
mongo:
image: mongo
networks:
- appNetwork
ports:
- "27017:27017"
environment:
- MONGO_URI=mongodb://mongo:27017
appservice:
build:
context: ./my-app
dockerfile: Dockerfile
networks:
- appNetwork
ports:
- "3000:3000"
networks:
appNetwork:
external: false
It is better to use mongo db atlas if you does not want complications. You can use it in local and in deployment.
Simple steps to get the mongo url is available in https://www.mongodb.com/cloud/atlas
Then add a env variable
CONNECT_TO=mongodb://your_url
For passing the .env to docker, use
docker run --publish 8000:3000 --env-file .env --detach --name kb keystoneblog:1.0

Mongo restore docker container returns 'error connecting to db server'

I've been searching for an answer to this using this question here as the basis since last night, however I get this error that I can't solve:
Step 4/4 : RUN mongorestore /trapsdump/ --host mongo:27017
--->
Running in be3d84e526ab
2018-05-18T10:52:50.575+0000 Failed: error
connecting to db server: no reachable servers
I'm using Docker-compose for this. I've used Docker a few times before building containers, but am no expert, however I think everything is correctly configured. I'm using this to seed the database as we have a 'demo' collection stored on mLab.
This is my Dockerfile:
FROM node:9.11.1
RUN mkdir /src
RUN npm install nodemon -g
WORKDIR /src
ADD ./package.json /src/package.json
RUN npm install
COPY . .
EXPOSE 3000
CMD npm start
This is the docker-compose.yml:
version: "2"
services:
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
- "11990:11990"
mongo-seed:
build: ./mongo-seed
links:
- mongo
app:
container_name: traps
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
And this is the mongo-seed/Dockerfile:
FROM mongo
RUN mongodump --uri=mongodb://xxxxxxxxxxxxx#xxxxxxxxxxxx:xxxxx/xxxxxx -o trapsdump
RUN mongorestore /trapsdump/ --host mongo:27017
If pulls down the collection from mLab and dumps it locally.
So if anyone can point out where i'm going wrong, that would be excellent.

Can't connect to mongo docker image with mongoose

I'm trying to run a simple docker setup with node and mongo:
Dockerfile:
FROM node:8.9.4-alpine
RUN mkdir /app
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
ADD . /app/
docker-compose.yml:
version: '3'
services:
db:
image: 'mongo'
ports:
- "27017:27017"
api:
build: .
restart: always
command: sh -c "npm install && npm run start"
volumes:
- .:/app
ports:
- "3000:3000"
environment:
PORT: 3000
depends_on:
- db
Now in my app.js I'm connecting to mongo like that:
mongoose.connect('mongodb://localhost:27017')
.catch(err => {
console.log(err)
})
However I'm getting a failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
The mongo container seems to boot up and run fine giving me waiting for connections on port 27017.
What's wrong with my setup? I also tried swapping out localhost for mongo when connecting, but it had no effect either.
I didn't realise I named my database container db instead of mongo, so all I had to do was to switch that name out in my app.js:
mongoose.connect('mongodb://db:27017')
.catch(err => {
console.log(err)
})
I think without mounting volumes in db service , container won't be able to continue its process. it will start for just a second and then it will be terminated.
so that's why you are getting an error.
you have to look at the port part of your container in the inspector, and my opinion is that you have to put : mongodb://0.0.0.0:27017/
enjoy !

Trying to connect a NodeJS app to MongoDB via Docker Compose

I'm following a MongoDB + NodeJS tutorial with my app. Everything works without Docker.. and I can in fact get the app to work up until it needs to connect to MongoDB.
If my app doesn't see MongoDB, it will print out an error and halt.
Here's my files
.env
NODE_VIEWS_PATH=../
NODE_PUBLIC_PATH=../
MONGODB_URI='mongodb://127.0.0.1:27017/myappsdb'
...
Dockerfile
FROM node:carbon
# Create app directory
WORKDIR /usr/src/mahrio
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
COPY . .
EXPOSE 6085
CMD ["npm", "start"]
docker-compose.yml
version: "2"
services:
app:
container_name: someappname
restart: always
build: .
ports:
- "6085:6085"
links:
- mongo
depends_on:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./tmp:/data/db
ports:
- "27017:27017"
When using docker-compose, for a container to connect to another container it can use the service name as a hostname to connect.
In your case, the node app needs to connect to mongo:27017 rather than localhost:27017, since localhost from the respective of the app container will refer to itself and not to your machine.
Therefore, change the mongo url to MONGODB_URI='mongodb://mongo:27017/myappsdb'. Also make sure that you consume the env file by adding:
app:
...
env_file:
- file.env