简体   繁体   English

如何将MongoDB添加到Docker容器中?

[英]How do I add MongoDB to a Docker container?

I have an app written in NodeJS that I would like to regularly test, ideally in an isolated Docker container. 我有一个要定期测试的用NodeJS编写的应用程序,最好在隔离的Docker容器中进行测试。

I've been following along at the tutorial here: Testing a Node.JS Application Within a Docker Container . 我一直在这里的教程中关注:在Docker容器中测试Node.JS应用程序 One of the things it demonstrates, is how to run your tests inside a Docker container, and how to set up the Dockerfile. 它演示的内容之一是如何在Docker容器中运行测试,以及如何设置Dockerfile。 For instance: 例如:

# This official base image contains node.js and npm
FROM node:7
ARG VERSION=1.0.0
# Copy the application files
WORKDIR /usr/src/app
COPY package.json app.js LICENSE /usr/src/app/
COPY lib /usr/src/app/lib/
LABEL license=MIT \
      version=$VERSION
# Set required environment variables
ENV NODE_ENV production
# Download the required packages for production
RUN npm update
# Make the application run when running the container
CMD ["node", "app.js"]

Since my app uses MongoDB, I need a running MongoDB instance to properly test my application. 由于我的应用程序使用MongoDB,因此我需要一个正在运行的MongoDB实例来正确测试我的应用程序。 I would like to include MongoDB in the Docker container. 我想在Docker容器中包含MongoDB。

I cannot find any instructions on how to do this (or more generally, how to add any database to a container). 我找不到有关如何执行此操作的任何说明(或更笼统地说,如何向容器添加任何数据库)。 Any tips would be greatly appreciated 任何提示将非常感谢

I think having multiple purpose container (ie. business logic + db) is not really docker like, so it probably why you don't find anything about this. 我认为拥有多用途容器(即业务逻辑+数据库)并不是真正意义上的docker,所以这可能就是为什么您对此一无所获的原因。 What you should propably do in this situation is use multiple containers, expose the correct port then call it from your application. 在这种情况下,您应该做的就是使用多个容器,公开正确的端口,然后从应用程序中调用它。

I suggest you to look into Docker-Compose to do such infrastructure, which will probably have two services : one for your node server and one for your mongoDB. 我建议您考虑使用Docker-Compose做这样的基础架构,它可能有两种服务:一种用于节点服务器,另一种用于mongoDB。

It will even be easier to maintain and configure than having all your service inside one containers, since you can split your logic easily. 与将所有服务都放在一个容器中相比,维护和配置甚至更加容易,因为您可以轻松拆分逻辑。

EDIT: I did'nt test the following docker-compose.yml, so it'll probably need some fix, but it should help you enough if you read the documentation along side. 编辑:我没有测试以下docker-compose.yml,所以它可能需要一些修复,但是如果您同时阅读文档 ,它应该对您有足够的帮助。

Starting from here, you could have a docker-compose.yml file looking like this : 从这里开始,您可能会有一个docker-compose.yml文件,如下所示:

version: "3"
services:
  app:
    container_name: app
    restart: always
    build: ./path/to/your/dockerfile/root
    ports:
      - "3000:3000"
    links:
      - mongo
  mongo:
    container_name: mongo
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"

Then, inside your apps, you can access you db from this kind of url : mongodb://mongo:27017/db 然后,在您的应用程序内部,您可以通过以下网址访问数据库: mongodb://mongo:27017/db

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

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