简体   繁体   English

如何使用 MongoDB 和 RabbitMQ 在 Docker 中运行 NodeJS?

[英]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?如何在 Docker 中运行 NodeJ 并将其与 MongoDB 和 RabbitMQ 连接? Do I need to run it in with docker-compose ?我需要用docker-compose运行它吗?

And also I need 2 environment而且我还需要2个环境

  1. Run nodejs application in a development environment with nodemon .使用 nodemon 在开发环境中运行nodemon应用程序。
  2. The Second environment is production experiment node app.js第二个环境是生产实验node app.js

I found this docker-compose yml, but I can not understand how to connect it from nodejs我找到了这个docker-compose yml,但我不明白如何从 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.几天后,我找到了这个带有 RabbitMQ、MongoDB 的仓库。 Also here you can find Mongo-express, where you can work with Mongo directly.您还可以在这里找到 Mongo-express,您可以在其中直接使用 Mongo。

https://github.com/stdakov/docker-rabbit-mongo 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 .不,您不需要使用docker-compose运行它。

I would recommend creating the following files:我建议创建以下文件:

  • Dockerfile - production image build Dockerfile - 生产镜像构建
  • Dockerfile.dev - development image build Dockerfile.dev - 开发镜像构建

The Dockerfile.dev would look something like this: Dockerfile.dev 看起来像这样:

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 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 docker build. -t node-app -f Dockerfile.dev运行镜像的命令: 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看看我看到的这个例子: 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您还需要实现 rmq 和 mongo 客户端连接,并在与 node-app 相同的网络内运行官方图像

All compose options can be translated into docker run commands所有 compose 选项都可以翻译成 docker 运行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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