Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is data persisting in mongo docker container?

Tags:

docker

mongodb

From my understanding, new containers should not persist data after they die.

Right now I have two Dockerfiles. One is for my application and the other is to seed a database.

This is my project structure

/
 package.json
 Dockerfile
 docker-compose.yml
 mongo-seeding/
   Dockerfile
   seedDatabase.mjs

This is what my seedDatabase looks like

import mongodb from "mongodb";
import data from "./dummyData";

// Connection URL
const url = "mongodb://mongo:27017/poc";

async function mongoSeeder() {
  try {
    // first check to see if the database exists
    const client = await mongodb.MongoClient.connect(url);
    const db = client.db("poc");
    const questionCollection = await db.createCollection("question");
    const answerCollection = await db.createCollection("answer");
    await questionCollection.insertMany(data.questions);
    await answerCollection.insertMany(data.answers);
  } catch (e) {
    console.log(e);
  }
}

mongoSeeder();

This is what my the Dockerfile for seeding the database looks like

FROM node:10

WORKDIR /usr/src/app
COPY package*.json ./
COPY mongo-seeding mongo-seeding
RUN yarn add mongodb uuid faker

CMD ["yarn", "seed"]

This is what the other Dockerfile looks like

FROM node:10

WORKDIR /usr/src/app
COPY package*.json ./
RUN yarn install
RUN yarn add mongodb uuid faker
RUN yarn global add nodemon babel-cli babel-preset-env
COPY . .
EXPOSE 3000
CMD ["yarn", "dev-deploy"]

This is what my docker-compose.yml looks like

version: "3"
services:
  mongo:
    container_name: mongo
    image: mongo
    ports:
      - "4040:27017"
    expose:
      - "4040"
  app:
    container_name: ingovguru
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    links:
      - mongo
    expose:
      - "3000"
  seed:
    container_name: seed
    build:
      context: .
      dockerfile: ./mongo-seeding/Dockerfile
    links:
      - mongo
    depends_on:
      - mongo
      - app

Initially, I am seeding the database with 1000 questions and 200 answers.

Notice that I am not using any volumes, so nothing should be persisted.

I run

docker-compose build docker-compose up

I jump into the mongo container, and I see that I have a 1000 questions and 100 answers.

I then Ctrl-C and re-run.

I now have 2000 questions and 100 answers.

Why does this happen?

like image 798
praks5432 Avatar asked Mar 26 '26 11:03

praks5432


1 Answers

Even though you are not declaring any volumes, the mongo image itself declares volumes for /data/db and /data/configdb in containers, which cover database data (see Dockerfile#L87). With Docker Compose, these anonymous volumes are re-used between invocations of docker-compose up and docker-compose down.

To remove the volumes when you bring down a stack, you need to use the -v, --volumes option. This will remove the volumes, giving you a clean database on the next up.

docker-compose down --volumes

docker-compose down documentation

-v, --volumes           Remove named volumes declared in the `volumes`
                        section of the Compose file and anonymous volumes
                        attached to containers.
like image 165
King Chung Huang Avatar answered Mar 29 '26 02:03

King Chung Huang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!