繁体   English   中英

使用 Compose 在 Docker 中运行 MongoDB

[英]Running a MongoDB in Docker using Compose

我正在尝试在 docker 和 python 脚本中运行一个数据库来存储 MQTT 消息。 这给了我使用 Docker Compose 的想法,因为听起来两者都有一定的联系是合乎逻辑的。 我遇到的问题是 Docker 容器确实运行,但它们没有在数据库中存储任何内容。

当我在本地运行我的脚本时,它确实存储了消息,所以我的预感是撰写文件不正确。 这是编写 python 文件的正确方法,该文件将消息存储在数据库和数据库本身中(使用 a.js 文件作为凭证)。 对于任何反馈,我们都表示感谢!

version: '3'
services:
   storing_script:
     build:
      context: .
      dockerfile: Dockerfile
    depends_on: [mongo]

mongo:
  image: mongo:latest
  environment:
        MONGO_INITDB_ROOT_USERNAME: xx
        MONGO_INITDB_ROOT_PASSWORD: xx
        MONGO_INITDB_DATABASE: motionDB
volumes:
- ${PWD}/mongo-data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
ports:
  - 27018:27018
restart: unless-stopped

DockerFile 用于构建:

# set base image (host OS)
FROM python:3.8-slim
# set the working directory in the container
WORKDIR /code 
# copy the dependencies file to the working 
directory
COPY requirements.txt .
# install dependencies
RUN pip install -r requirements.txt
# copy the content of the local src directory to 
the working directory
COPY src/ .
# command to run on container start
CMD [ "python", "./main.py"]

我认为这可能是由于用户许可。

我为部署 docker 的 docker-compose 所做的,是在创建 mongodb 用户后挂载 passwd 文件

volumes:
    /etc/passwd:/etc/passwd:ro

这对我来说是最直接的解决方案。

暂无
暂无

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

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