关于django: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文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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时的错误信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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得到)

现在,我又遇到了另一个问题:

1
2
3
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,它可以正常工作,除非我无法访问该网页。


看起来您的应用程序正在寻找localhost上的数据库。它应该在主机db上查找db(主机名将通过link参数添加到/etc/hosts)。


你的BUILD树中的某个地方可能有一个名为settings.py的文件。你可以通过输入命令找到它:

1
find . -name settings.py

找到后,您需要编辑该文件。它看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
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建议的那样),您将解决此问题。 (好吧,你将继续讨论下一个问题:-))你应该给予阿德里安的答案。


我有这个问题,这只是因为我使用的是VPN。如果您使用sshuttle之类的东西,请将其关闭。


当我遇到这个问题答案时,我遇到了这个问题并正在抓住吸管:django.db.utils.OperationalError无法连接到服务器。

对我有用的解决方案是使用的方法杀死所有的postgres进程

1
sudo pkill postgres