【发布时间】: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 不等。
【问题讨论】:
-
你能展示你的dockerfile,你的docker run吗?
-
@user2915097 我用 docker-compose 文件以及它引用的 2 个 Dockerfile 更新了帖子。
标签: python postgresql docker docker-compose