简体   繁体   English

Docker 擦除 mongoDB 容器数据

[英]Docker wipes out mongoDB container data

I have created a program and tested that works just fine.我创建了一个程序并进行了测试,效果很好。 I decided to dockerize it, and it seems after maybe some hours or few days the data of mongoDB container get all deleted.我决定将其 docker 化,似乎可能在几个小时或几天后 mongoDB 容器的数据被全部删除。 The docker-compose.yml file: docker-compose.yml 文件:

version: '3'
services:
  node:
    restart: always
    build: ./nodeServer
    container_name: nodeserver
    ports:
      - 5000:5000
    depends_on:
      - database
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.2 
    environment:
      - TZ=Europe/Athens
  database:
    restart: always
    build: ./mongoDump/database
    container_name: mongodb
    ports:
      - 27017:27017
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.4 
    volumes:
      - ./data:/data/db
    environment:
      - TZ=Europe/Athens
  pythonscript:
    restart: always
    build: ./python
    container_name: pythonscript
    depends_on:
      - database
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.3 
    environment:
      - TZ=Europe/Athens
networks:
  twitter_articles:
    ipam:
      config:
        - subnet: 172.24.0.0/24

And the three Dockerfile's that they are builded:以及它们构建的三个 Dockerfile:

nodeserver:节点服务器:

FROM node:14.16.1

COPY package*.json ./
RUN npm install

COPY . ./

CMD [ "npm", "start"]

mongodb: mongodb:

FROM mongo:5.0.3
CMD docker-entrypoint.sh mongod

pythonscript脚本

FROM python:3.9
COPY requirements.txt ./
RUN pip install -r requirements.txt
    
COPY . ./
    
CMD [ "python", "-u", "./init2.py" ]

As mentioned before without Docker the app works just fine and there isn't that kind of behaviour of database getting wiped out.如前所述,在没有 Docker 的情况下,该应用程序工作得很好,并且没有那种数据库被清除的行为。 I have tried also internal Docker storage which also does the same thing.我也尝试过内部 Docker 存储,它也做同样的事情。 I have tried to check the logs and I saw that there is an error happening in pythonscript container each time database wipes out.我试图检查日志,发现每次数据库清除时pythonscript容器中都会发生错误。 I know that an error should happen in pythonscript but there is no such a code anywhere in the app to perform deletion of collections or databases (also without Docker this error still happens but nothing gets deleted).我知道 pythonscript 中应该发生错误,但是应用程序中的任何地方都没有这样的代码来执行 collections 或数据库的删除(也没有 Docker,此错误仍然会发生,但不会删除任何内容)。

Any ideas?有任何想法吗?

You can create an external volume and add the data of the mongoDB into it.您可以创建一个外部卷并将 mongoDB 的数据添加到其中。 That way your data doesn't get wiped even when you turn off your docker-compose.这样,即使您关闭 docker-compose,您的数据也不会被擦除。

version: '3'
services:
  node:
    restart: always
    build: ./nodeServer
    container_name: nodeserver
    ports:
      - 5000:5000
    depends_on:
      - database
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.2 
    environment:
      - TZ=Europe/Athens
  database:
    restart: always
    build: ./mongoDump/database
    container_name: mongodb
    ports:
      - 27017:27017
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.4
    volumes: 
      - mongo_data:/data/db
    environment:
      - TZ=Europe/Athens
  pythonscript:
    restart: always
    build: ./python
    container_name: pythonscript
    depends_on:
      - database
    networks:
      twitter_articles:
        ipv4_address: 172.24.0.3 
    environment:
      - TZ=Europe/Athens
networks:
  twitter_articles:
    ipam:
      config:
        - subnet: 172.24.0.0/24
volumes:
  mongo_data:
    external: true

now you have to create a volume in your docker using现在您必须使用在 docker 中创建一个卷

 docker volume create --name=mongo_data

then docker-compose down and然后 docker-compose 下来

 docker-compose up --build -d

I have been advised that it is always better idea to save data outside of docker container in separate volume.我被告知最好将 docker 容器之外的数据保存在单独的卷中。 Look for this tutorial volumes .查找本教程

You need to make an persistant volume for your database, because as you noted on your docker-compose.yml file you got:您需要为您的数据库创建一个持久卷,因为正如您在 docker-compose.yml 文件中所述,您得到:

restart: always

so everytime your python script got an error, it's stopped and it's depending on Mariadb, so it's restarted and data got wiped.因此,每次您的 python 脚本出错时,它都会停止并且取决于 Mariadb,因此它会重新启动并且数据被擦除。

Make sure the data is stored outside the docker container because are treated like cattles and not pets.确保数据存储在 docker 容器之外,因为它们被视为牛而不是宠物。 New containers are created freshly with no data from previous version.新容器是全新创建的,没有来自先前版本的数据。

  1. I'd ensure that container user has a pre-configured ID with write access to the host folder targeted for db data persistence.我会确保容器用户具有预配置的 ID,该 ID 具有对用于数据库数据持久性的主机文件夹的写访问权限。
  2. I'd use an absolute path on the host side too when mapping persistent data folders in Docker.在 Docker 中映射持久数据文件夹时,我也会在主机端使用绝对路径。

Referring to:参考:

    volumes:
      - ./data:/data/db

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

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