[英]Why is data persisting in mongo docker container?
From my understanding, new containers should not persist data after they die. 据我了解,新容器死后不应保留数据。
Right now I have two Dockerfiles. 现在我有两个Dockerfile。 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 这是我的
seedDatabase
样子
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 这就是我
Dockerfile
为数据库Dockerfile
种子的Dockerfile
的样子
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 这就是其他Dockerfile的样子
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 这就是我
docker-compose.yml
样子
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. 最初,我为数据库提供了1000个问题和200个答案。
Notice that I am not using any volumes, so nothing should be persisted. 请注意,我没有使用任何卷,因此不应保留任何内容。
I run 我跑
docker-compose build docker-compose up docker-compose建立docker-compose up
I jump into the mongo container, and I see that I have a 1000 questions and 100 answers. 我跳入mongo容器,发现我有1000个问题和100个答案。
I then Ctrl-C
and re-run. 然后,我
Ctrl-C
并重新运行。
I now have 2000 questions and 100 answers. 我现在有2000个问题和100个答案。
Why does this happen? 为什么会这样?
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 ). 即使您没有声明任何卷,mongo映像本身
/data/configdb
在容器中声明/data/db
和/data/configdb
的卷,这些容器涵盖数据库数据(请参阅Dockerfile#L87 )。 With Docker Compose, these anonymous volumes are re-used between invocations of docker-compose up
and docker-compose down
. 借助Docker Compose,这些匿名卷可在
docker-compose up
和docker-compose down
调用之间重复使用。
To remove the volumes when you bring down a stack, you need to use the -v, --volumes
option. 要在放下堆栈时删除卷,需要使用
-v, --volumes
选项。 This will remove the volumes, giving you a clean database on the next up. 这将删除卷,从而在接下来的工作中为您提供一个干净的数据库。
docker-compose down --volumes
docker-compose down documentation docker-撰写文档
-v, --volumes Remove named volumes declared in the `volumes`
section of the Compose file and anonymous volumes
attached to containers.
The problem that you have is because the mongo image has defined some volumes on its Dockerfile, you can see them doing: 您遇到的问题是,由于mongo映像已在其Dockerfile上定义了一些卷,您可以看到它们正在执行以下操作:
docker history --no-trunc mongo | grep VOLUME
If you only stop the mongo container (you can check doing docker ps -a | grep mongo
), the created volumes are kept. 如果仅停止mongo容器(可以检查
docker ps -a | grep mongo
),则会保留创建的卷。 If you want to remove the container and its volumnes, you can remove the stopped mongo container with docker rm CONTAINER_ID
or removing all the containers created by compose with docker-compose down
. 如果要删除容器及其体积,则可以使用docker
docker rm CONTAINER_ID
删除已停止的mongo容器,或者使用docker-compose down
删除由compose创建的所有容器。
In your case, if you want to build and run the services, you have to do: 就您而言,如果要构建和运行服务,则必须执行以下操作:
docker-compose build && docker-compose down && docker-compose up -d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.