Home > other >  Docker wipes out mongoDB container data
Docker wipes out mongoDB container data

Time:03-17

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?

CodePudding user response:

you can create a volume external and add the data of the mango into it by this way you data didn't wipe even 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

CodePudding user response:

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.

  • Related