简体   繁体   中英

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. 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?

You can create an external volume and add the data of the mongoDB into it. That way your data doesn't get wiped even when you turn off your 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 volume create --name=mongo_data

then docker-compose down and

 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. 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:

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.

Make sure the data is stored outside the docker container because are treated like cattles and not pets. 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.
  2. I'd use an absolute path on the host side too when mapping persistent data folders in Docker.

Referring to:

    volumes:
      - ./data:/data/db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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