I need to be able to run within the same container both: my nodejs server.js and mongodb.
I know it is possible to achieve this with docker-compose or with two separate containers but that is not an option here. I need both services running on the same container.
My Dockerfile is something like this:
FROM node:16
WORKDIR /usr/src/app
RUN apt-get update && apt-get install -y mongodb
COPY package.json ./
RUN npm install
COPY server.js public/ ./
EXPOSE 3000
EXPOSE 27017
CMD service mongodb start && node server.js
Then at some point my server.js needs to call the mongodb like this:
const uri = 'mongodb://172.0.0.1:27017';
However when I try to build my container I get:
E: Package 'mongodb' has no installation candidate
2
Answers
I don’t know why would you want to do this, you can create two different Dockerfiles as well, but anyway.
Not sure why you are trying to run both MongoDB and Application in same container, Anyway you can do it like this;
I’ve used mongoose NPM Package to connect to MongoDB,