6

I'm creating a docker-compose config for an django app, the Dockerfile builds successfully but when I compose them up, django return an issue -- cannot connect to posgres.

I run docker-compose run web bash, found redis and posgres both cannot be connected.

My docker-compose.yml file:

db:
  image: postgres:9.1
  environment:
    - POSTGRES_PASSWORD=mysecretpassword

redis:
    image: redis:2.8

web:
  links:
    - db
    - redis
  build: .
  volumes:
    - .:/workspace
  ports:
    - "8000:8000“
  command: python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default

Error info when I do docker-compose up:

sudo docker-compose up
Recreating breadtrip_db_1...
Recreating breadtrip_redis_1...
Recreating breadtrip_web_1...
Attaching to breadtrip_redis_1, breadtrip_web_1
redis_1 | [1] 06 May 06:07:30.469 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

... 

redis_1 | [1] 06 May 06:07:30.490 # Server started, Redis version 2.8.19
redis_1 | [1] 06 May 06:07:30.490 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | [1] 06 May 06:07:30.490 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | [1] 06 May 06:07:30.491 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | [1] 06 May 06:07:30.491 * DB loaded from disk: 0.000 seconds
redis_1 | [1] 06 May 06:07:30.491 * The server is now ready to accept connections on port 6379
web_1   | Traceback (most recent call last):
web_1   |   File "/workspace/BreadTripServer/webapps/manage.py", line 14, in <module>
web_1   |     execute_manager(settings)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
web_1   |     utility.execute()
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
web_1   |     self.fetch_command(subcommand).run_from_argv(self.argv)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
web_1   |     self.execute(*args, **options.__dict__)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 209, in execute
web_1   |     translation.activate('en-us')
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 100, in activate
web_1   |     return _trans.activate(language)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 202, in activate
web_1   |     _active.value = translation(language)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 185, in translation
web_1   |     default_translation = _fetch(settings.LANGUAGE_CODE)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 162, in _fetch
web_1   |     app = import_module(appname)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
web_1   |     __import__(name)
web_1   |   File "/workspace/BreadTripServer/webapps/lib/haystack/__init__.py", line 83, in <module>
web_1   |     backend = load_backend(settings.HAYSTACK_SEARCH_ENGINE)
web_1   |   File "/workspace/BreadTripServer/webapps/lib/haystack/__init__.py", line 57, in load_backend
web_1   |     return importlib.import_module('haystack.backends.%s_backend' % backend_name)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
web_1   |     __import__(name)
web_1   |   File "/workspace/BreadTripServer/webapps/lib/haystack/backends/__init__.py", line 6, in <module>
web_1   |     from django.db.models import Q
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 78, in <module>
web_1   |     connection = connections[DEFAULT_DB_ALIAS]
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __getitem__
web_1   |     conn = backend.DatabaseWrapper(db, alias)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/base.py", line 11, in __init__
web_1   |     self.ops = PostGISOperations(self)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 91, in __init__
web_1   |     vtup = self.postgis_version_tuple()
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 445, in postgis_version_tuple
web_1   |     version = self.postgis_lib_version()
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 425, in postgis_lib_version
web_1   |     return self._get_postgis_func('postgis_lib_version')
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 406, in _get_postgis_func
web_1   |     cursor = self.connection._cursor()
web_1   |   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 140, in _cursor
web_1   |     self.connection = Database.connect(**conn_params)
web_1   |   File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
web_1   |     connection_factory=connection_factory, async=async)
web_1   | psycopg2.OperationalError: could not connect to server: Connection refused
web_1   |   Is the server running on host "localhost" (::1) and accepting
web_1   |   TCP/IP connections on port 5432?
web_1   | could not connect to server: Connection refused
web_1   |   Is the server running on host "localhost" (127.0.0.1) and accepting
web_1   |   TCP/IP connections on port 5432?
web_1   | 
breadtrip_web_1 exited with code 1

Update:

Cause I set a link to other image, docker linked pg on host 172.17.0.67, I need to set the pg host to that address which not mentioned on docker's official docs. (This address would change every time, but can be gotten by env)

Now, I got another issue:

web_1   | Unknown command: 'runserver'
web_1   | Type 'manage.py help' for usage.
breadtrip_web_1 exited with code 1

If I change the command python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default to python /workspace/BreadTripServer/webapps/manage.py runserver, it works OK except I cannot reach the web page.

Kane Blueriver
  • 4,170
  • 4
  • 29
  • 48

4 Answers4

6

It looks like your application is looking for the Database on localhost. It should be looking for the db at the host db (the host name will have been added to /etc/hosts by the link argument).

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • 1
    This is what the official demo looks like, what need I do to set the hosts right – Kane Blueriver May 06 '15 at 08:54
  • I have no idea what your source code looks like. In the code there will be a point where it connects to the DB. You need to set this to "db". It may be set to "localhost" or "127.0.0.1" currently, or it might be unspecified. – Adrian Mouat May 06 '15 at 09:57
  • cause I set the link, docker linked pg on host 172.17.0.67, I need to set the pg host to that address which not mentioned on docker's official docs. – Kane Blueriver May 06 '15 at 10:04
  • Docker will have set up an entry in /etc/hosts on the web container with the name db and the IP of the db container, so you don't need to discover the IP address manually - you can just use the name "db". – Adrian Mouat May 06 '15 at 11:14
  • I know now, but I use env variable instead. – Kane Blueriver May 07 '15 at 02:26
3

somewhere in your BUILD tree there is probably a file called settings.py. you can find this by typing the command :

find . -name settings.py

once found, you need to edit that file. It will look something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydb',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'myuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',                      # Empty for localhost through domain sockets or           '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

see the 'localhost' ? Change that to 'db' (As @Adrian suggested) and you will have this solved. (well, you will move on to the next problem :-) ) You should give Adrian credit for the answer.

Greg
  • 6,571
  • 2
  • 27
  • 39
  • But why I cannot start my site by `python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default` – Kane Blueriver May 07 '15 at 02:28
  • 2
    i don't know. your components are misconfigured. to configure them properly requires modifying the configuration to use the provided linking (either /etc/hosts or env variables) variables correctly. your supplied traceback is clearly trying to open postgres at localhost, postgres is not at localhost, postgres is at db. you say this is the official demo. can you provide a link to the breadtripserver official demo, i can take a look there and see what the problem is. – Greg May 07 '15 at 11:05
  • You're right! Now I'm working on the next issue now. – Kane Blueriver May 08 '15 at 05:13
0

I had this issue and was grasping at straws as I came across this SO answer: django.db.utils.OperationalError Could not connect to server.

The solution, which worked for me, was to kill all postgres processes using

sudo pkill postgres
Greg Brown
  • 1,251
  • 1
  • 15
  • 32
0

I had this issue and It was only because I was using a VPN. If you are using something like sshuttle just turn it off.

Soorena
  • 4,352
  • 5
  • 30
  • 42