Docker Compose无法启动Postgres镜像。

6

我正在为一个 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

更新:

由于我设置了到另一张图片的链接,docker 链接的 pg 在主机 172.17.0.67 上,我需要将 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_default更改为python /workspace/BreadTripServer/webapps/manage.py runserver,它可以正常工作,但是我无法访问网页。
4个回答

6

看起来你的应用程序正在寻找位于localhost上的数据库。它应该在主机db上寻找数据库(主机名将已经被添加到/etc/hosts中,这是链接参数完成的)。


1
这就是官方演示的样子,我需要做什么来设置主机呢? - Kane Blueriver
我不知道你的源代码是什么样子的。在代码中,会有一个连接到数据库的地方。你需要将它设置为“db”。它目前可能被设置为“localhost”或“127.0.0.1”,或者可能未指定。 - Adrian Mouat
因为我设置了链接,将docker链接到了主机172.17.0.67上的pg,所以我需要将pg主机设置为该地址,这在docker的官方文档中没有提到。 - Kane Blueriver
Docker将在Web容器的/etc/hosts中设置一个名为db且IP为db容器的条目,因此您无需手动查找IP地址 - 您只需使用名称“db”即可。 - Adrian Mouat
我现在知道了,但我使用环境变量代替。 - Kane Blueriver

3

在您的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' 了吗?按照 @Adrian 的建议,将其更改为 'db' ,问题就会解决。 (好吧,你会继续遇到下一个问题 :-) )你应该感谢 Adrian 给出的答案。


但是为什么我无法通过 python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default 启动我的网站呢? - Kane Blueriver
2
我不知道。你的组件已经配置错误。正确配置需要修改配置,使用所提供的链接(/etc/hosts或环境变量)正确设置变量。你提供的回溯显然试图在localhost打开postgres,但postgres并不在localhost,它在db上。你说这是官方演示。你能提供一个BreadTripServer官方演示的链接吗?我可以到那里看看问题出在哪里。 - Greg
你说得对!现在我正在处理下一个问题。 - Kane Blueriver

0

0

我曾经遇到过这个问题,原因是我使用了 VPN。如果你正在使用像 sshuttle 这样的工具,请关闭它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接