简体   繁体   English

为什么在使用postgres Docker镜像和docker-compose时数据仍然存在?

[英]Why is data persisted when using postgres Docker image and docker-compose?

I'm new to Docker and I'm trying to get a Django app working in a Docker container. 我是Docker的新手,我正试图让一个Django应用程序在Docker容器中运行。 This app needs a PostgreSQL database (and redis). 这个应用程序需要一个PostgreSQL数据库(和redis)。

In order to try that, I created an image (Dockerfile) for my app, and used the basic postgres image for the database server. 为了尝试这一点,我为我的应用程序创建了一个图像(Dockerfile),并使用了基本的postgres图像作为数据库服务器。 My docker-compose file looks like: 我的docker-compose文件看起来像:

version: '3.7'

services:
  db:
    image: postgres
  redis:
    image: redis
  api:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    environment:
      - DJANGO_SETTINGS_MODULE=api.settings.docker_settings
    volumes:
      - ./api:/code/api
    ports:
      - "8000:8000"
    depends_on:
      - db
      - redis

As my docker-compose file does not define any volume for the database, I expected the database data to be lost when the container is stopped, but it's not! 由于我的docker-compose文件没有为数据库定义任何卷,我预计在容器停止时数据库数据会丢失,但事实并非如此! That is, if I run docker-compose up , add data to the db, stop docker-compose and start it again, the data is still there! 也就是说,如果我运行docker-compose up ,将数据添加到数据库,停止docker-compose并再次启动它,数据仍然存在!

Is a volume automatically created by Docker? 是否由Docker自动创建卷? (I noticed a VOLUME command in the Dockerfile of the postgres image, see here ). (我注意到postgres图像的Dockerfile中有一个VOLUME命令,见这里 )。

Now I would like to add a volume to docker-compose to store the database somewhere I can control. 现在我想在docker-compose中添加一个卷来将数据库存储在我可以控制的地方。 How can I do that? 我怎样才能做到这一点? I thought I should change the docker-compose file to 我以为我应该将docker-compose文件更改为

...
db:
    image: postgres
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
...

but how can I tell Docker to not use the previous data that has been automatically persisted? 但是如何告诉Docker不使用以前自动保留的数据呢?

You should distinguish between container's stop and container's removal. 您应该区分容器的停止和容器的移除。

First occurs when main process in container stops execution (probably because you manually sent stop signal). 首先发生在容器中的主进程停止执行时(可能是因为您手动发送了停止信号)。 In this case container is still available and could be restarted with all data persisted in it. 在这种情况下,容器仍然可用,并且可以在其中保留所有数据的情况下重新启动。 This is what happens in your example. 这是您的示例中发生的情况。

Second occurs when you explicitly remove the container. 第二种情况发生在您明确删除容器时。 It can be done with special command or with removal argument (like docker run --rm ... ) applied on start. 它可以使用特殊命令或在启动时应用的删除参数(如docker run --rm ... )来完成。 In this case container's data is lost. 在这种情况下,容器的数据会丢失。

docker-compose by default does not delete containers and restarts them. 默认情况下, docker-compose不会删除容器并重新启动它们。 To change the behavior, simply use docker-compose up --force-recreate ... and you will get virgin containers on each startup. 要改变行为,只需使用docker-compose up --force-recreate ...并在每次启动时获得处女容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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