我試圖找到一種很好的方式來爲初始數據填充一個簡單應用程序的數據庫。我正在使用來自realpython.com的教程作爲起點。然後,在創建數據庫後添加一個簡單的Python腳本來添加單個條目,但是當我這樣做時,即使僅調用script一次,也會多次添加數據。 result將腳本添加到postgres docker容器的Python腳本多次運行
人口腳本(test.py):
from app import db
from models import *
t = Post("Hello 3")
db.session.add(t)
db.session.commit()
編輯:
這裏是泊塢窗,撰寫文件,我用它來構建項目:
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"
它引用兩個不同的Dockerfiles:
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
EDIT2:
有些人認爲數據是持續了幾個運行,這也是我最初的想法。事實並非如此,因爲我在測試之前通過docker rm刪除了所有活動的碼頭集裝箱。此外,「額外」數據的數量並不一致,在我迄今爲止運行的少數測試中,從3-6範圍內隨機測試。
的完整代碼項目是在這裏:[回購](https://github.com/aleedom/Docker-Testing)。我正在關注這個[Tutorial](https://realpython.com/blog/python/dockerizing-flask-with-compose-and-machine-from-localhost-to-the-cloud/)。 – aleedom
您可以顯示您的碼頭文件,碼頭運行嗎? – user2915097
@ user2915097我使用docker-compose文件以及它引用的2個Dockerfiles更新了帖子。 – aleedom