skip to Main Content

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?

5

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
    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
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search