【问题标题】:Python script to add data to postgres docker container runs multiple times将数据添加到 postgres docker 容器的 Python 脚本多次运行
【发布时间】:2015-09-02 20:27:04
【问题描述】:

我正在尝试找到一种使用简单应用程序的初始数据填充数据库的好方法。我使用来自 realpython.com 的教程作为起点。然后我在创建数据库后运行一个简单的 python 脚本来添加一个条目,但是当我这样做时,即使我只调用了一次script,数据也会被添加多次。 result

人口脚本(test.py):

   from app import db                                                                                                                                                          
   from models import *                                                                                                                                                        

   t = Post("Hello 3")                                                                                                                                                         
   db.session.add(t)                                                                                                                                                           
   db.session.commit()  

编辑:

这是我用来构建项目的 docker-compose 文件:

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - /usr/src/app/static
  env_file: .env
  command: /usr/local/bin/gunicorn -w 2 -b :8000 app:app

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

data:
  restart: always
  image: postgres:latest
  volumes:
    - /var/lib/postgresql
  command: "true"

postgres:
  restart: always
  image: postgres:latest
  volumes_from:
    - data
  ports:
    - "5432:5432"

它引用了两个不同的 Dockerfile:

构建 App 容器的 Dockerfile #1 是 1 行:

FROM python:3.4-onbuild

Dockerfile #2 用于构建 nginx 容器

FROM tutum/nginx
RUN rm /etc/nginx/sites-enabled/default
ADD sites-enabled/ /etc/nginx/sites-enabled

编辑2:

有些人认为数据会在多次运行中持续存在,这也是我最初的想法。情况并非如此,因为我在测试之前通过 docker rm 删除了所有活动的 docker 容器。此外,“额外”数据的数量并不一致,在我到目前为止运行的少数测试中随机从 3-6 不等。

【问题讨论】:

  • 完整代码项目在这里:Repo。我正在关注这个Tutorial
  • 你能展示你的dockerfile,你的docker run吗?
  • @user2915097 我用 docker-compose 文件以及它引用的 2 个 Dockerfile 更新了帖子。

标签: python postgresql docker docker-compose


【解决方案1】:

事实证明,这是一个在容器上使用运行命令和 docker-compose/Dockerfile 中的“restart: always”指令相关的错误。为了在不修复错误的情况下解决此问题,我从 Web 容器中删除了“重新启动:始终”。

相关问题:https://github.com/docker/compose/issues/1013

【讨论】:

    猜你喜欢
    • 2017-03-05
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多