簡體   English   中英

docker-compose不會啟動postgres圖像

[英]docker-compose does not start postgres image

我正在為django應用程序創建一個docker-compose配置,Dockerfile構建成功但是當我編寫它們時,django會返回一個問題 - 無法連接到posgres。

我運行docker-compose run web bash ,發現redis和posgres都無法連接。

我的docker-compose.yml文件:

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

我做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

更新:

因為我在主機172.17.0.67上設置了鏈接到其他圖像,docker鏈接的pg,我需要將pg主機設置為docker官方文檔中未提及的地址。 (這個地址每次都會改變,但可以通過env得到)

現在,我又遇到了另一個問題:

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

如果我將命令python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_defaultpython /workspace/BreadTripServer/webapps/manage.py runserver ,它可以正常工作,除非我無法訪問網頁。

看起來您的應用程序正在查找localhost上的數據庫。 它應該在主機db上查找db (主機名將通過link參數添加到/etc/hosts )。

你的BUILD樹中的某個地方可能有一個名為settings.py的文件。 你可以通過輸入命令找到它:

find . -name settings.py

找到后,您需要編輯該文件。 它看起來像這樣:

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.
    }
}

看到'localhost'? 將其更改為'db'(如@Adrian建議的那樣),您將解決此問題。 (好吧,你將繼續討論下一個問題:-))你應該給予阿德里安的答案。

當我遇到這個問題答案時,我遇到了這個問題並正在抓住吸管: django.db.utils.OperationalError無法連接到服務器

對我有用的解決方案是使用的方法殺死所有的postgres進程

sudo pkill postgres

我有這個問題,這只是因為我使用的是VPN。 如果您使用sshuttle東西, sshuttle其關閉。

暫無
暫無

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

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