2

I am building a Python app and I am trying to run it inside Docker. (My works completely in virtualenv with no Docker.)

So, my app consists of config.py file and other files which make everything work, but are in this case unimportant.

My config file looks like this:

SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://gimtest:gimtest@localhost:5432/gimtest-users"

App requires postgresql to be run before the app starts, so I tried using Docker-Compose. My docker-compose.yml looks like this:

version: '2'
services:
   web:
      build: ./web
      ports:
         - "5000:5000"
      depends_on:
         - postgres
      links:
         - postgres
         - elastic
      expose:
         - 5000
      command:
         "python run.py"
   postgres:
      image: postgres:9.5
      expose:
         - 5432
      environment:
         POSTGRES_USER: "gimtest"
         POSTGRES_PASSWORD: "gimtest"
         POSTGRES_DB: "gimtest-users"

   elastic:
      image: elasticsearch:2.3

Now the problem is that I can't connect to postgresql database. (At least that's what compile says because it throws an error from python app that no postgresql is running on specified url (up in config.py).

How can I fix the url so that it will work?

5
  • If you can manually connect to the postges db, then try add a delay to your app. Commented Oct 4, 2016 at 19:28
  • I can't connect to docker postgres. What I can is connecting to separate psql program. The delay should be fixed with depends_on definition. Thanks for thought though. Commented Oct 4, 2016 at 19:36
  • depends_on will not wait until the db is ready. See docs.docker.com/compose/startup-order You'll need to add a manual delay if db takes time to be ready. Also you db connection should be to postgres, not localhost Commented Oct 4, 2016 at 19:39
  • Ok, I see now. I don't get the part about connecting to postgres instead of localhost... How should the URL look like then? Commented Oct 4, 2016 at 20:43
  • 1
    Just change localhost to postgres. postgres is the service name or host name for the db container. Commented Oct 4, 2016 at 20:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.