How can I run NodeJS in Docker with MongoDB and RabbitMQ?

Viewed 790

How can I run NodeJs in Docker and Connect it with MongoDB and RabbitMQ? Do I need to run it in with docker-compose?

And also I need 2 environment

  1. Run nodejs application in a development environment with nodemon.
  2. The Second environment is production experiment node app.js

I found this docker-compose yml, but I can not understand how to connect it from nodejs

services:
    rabbitmq:
        image: rabbitmq:3-management-alpine
        container_name: rabbitmq
        environment:
            RABBITMQ_ERLANG_COOKIE: ${RABBITMQ_ERLANG_COOKIE}
            RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
            RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
1 Answers

No, you do not need to run it with docker-compose.

I would recommend creating the following files:

  • Dockerfile - production image build
  • Dockerfile.dev - development image build

The Dockerfile.dev would look something like this:

FROM node:10-alpine
ENV PORT 8080
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install -g nodemon
RUN npm install

ENTRYPOINT ["nodemon", "/usr/src/app/server.js"]

The command to build the image: docker build . -t node-app -f Dockerfile.dev The command to run the image: docker run --rm -it -p 8080:8080 --network my-node-net -v "${PWD}:/usr/src/app" node-app


Check out this example I saw: https://github.com/kelda/node-todo

You also need to implement rmq and mongo client connections and run the official images inside the same network as the node-app

All compose options can be translated into docker run commands

Related