debugging Docker清除mongoDB容器数据

izkcnapc  于 2022-11-14  发布在  Docker
关注(0)|答案(5)|浏览(286)

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. The docker-compose.yml file:

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:

nodeserver:

FROM node:14.16.1

COPY package*.json ./
RUN npm install

COPY . ./

CMD [ "npm", "start"]

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. I have tried also internal Docker storage which also does the same thing. I have tried to check the logs and I saw that there is an error happening in pythonscript container each time database wipes out. 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).
Any ideas?

vkc1a9a2

vkc1a9a21#

你可以创建一个外部卷,并将mongoDB的数据添加到其中。这样,即使你关闭了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

现在您必须使用以下命令在Docker中创建卷

docker volume create --name=mongo_data

然后向下扩展

docker-compose up --build -d
x6492ojm

x6492ojm2#

我被告知,它总是更好的主意,保存数据以外的docker容器在单独的卷。寻找这个教程volumes

hk8txs48

hk8txs483#

您需要为数据库创建一个持久卷,因为正如您在docker-compose.yml文件中所注意到的,您得到了:

restart: always

所以每次你的python脚本出错时,它就会停止,它依赖于Mariadb,所以它会重新启动,数据也会被擦除。

rkue9o1l

rkue9o1l4#

请确保数据存储在Docker容器之外,因为它们被视为牛而不是宠物。新容器是新创建的,没有来自以前版本的数据。

h43kikqp

h43kikqp5#

1.我将确保容器用户具有预配置的ID,该ID具有对数据库数据持久化目标主机文件夹的写访问权限。
1.在Docker中Map持久数据文件夹时,我也会在主机端使用绝对路径。
指的是:

volumes:
      - ./data:/data/db

相关问题