简体   繁体   中英

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

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}

After a few days, I found this repo with RabbitMQ, MongoDB. Also here you can find Mongo-express, where you can work with Mongo directly.

https://github.com/stdakov/docker-rabbit-mongo

Installation:

git clone https://github.com/stdakov/docker-rabbit-mongo.git
cd docker-rabbit-mongo/
docker-compose up -d

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM