Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm trying to find a good way to populate a database with initial data for a simple application. I'm using a tutorial from realpython.com as a starting point. I then run a simple python script after the database is created to add a single entry, but when I do this the data is added multiple times even though I only call the script once. result

population script (test.py):

   from app import db                                                                                                                                                          
   from models import *                                                                                                                                                        

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

edit:

Here is the docker-compose file which i use to build the project:

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"

it references two different Dockerfiles:

Dockerfile #1 which builds the App container and is 1 line:

FROM python:3.4-onbuild

Dockerfile #2 is used to build the nginx container

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

edit2:

Some people have suggested that the data was persisting over several runs, and that was my initial thought as well. This is not the case, as I remove all active docker containers via docker rm before testing. Also the number of "extra" data is not consistent, ranging randomly from 3-6 in the few tests that I have run so far.

share|improve this question
    
Full code project is here: Repo. I'm following this Tutorial. –  aleedom Sep 2 at 20:27
    
Can you show your dockerfile, your docker run? –  user2915097 Sep 2 at 20:53
    
@user2915097 I updated the post with the docker-compose file as well as the 2 Dockerfiles it references. –  aleedom Sep 2 at 23:34

1 Answer 1

up vote 0 down vote accepted

It turns out this is a bug related to using the run command on containers with the "restart: always" instruction in the docker-compose/Dockerfile. In order to resolve this issue without a bug fix I removed the "restart: always" from the web container.

related issue: https://github.com/docker/compose/issues/1013

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.