簡體   English   中英

將數據添加到Postgres Docker容器的Python腳本多次運行

[英]Python script to add data to postgres docker container runs multiple times

我正在嘗試尋找一種很好的方法來用簡單應用程序的初始數據填充數據庫。 我使用realpython.com的教程作為起點。 然后,在創建數據庫以添加單個條目之后,我運行一個簡單的python腳本,但是當我這樣做時,即使我只調用一次腳本 ,數據也會被多次添加。 結果

填充腳本(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:

Dockerfile#1構建App容器,為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刪除了所有活動的Docker容器。 同樣,“額外”數據的數量也不一致,到目前為止,在我進行的一些測試中,隨機數據的范圍是3-6。

事實證明,這是與在docker-compose / Dockerfile中使用“ restart:always”指令對容器使用run命令有關的錯誤。 為了解決此問題而沒有錯誤修復,我從Web容器中刪除了“重新啟動:始終”。

相關問題: https : //github.com/docker/compose/issues/1013

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM