【问题标题】:Postgres docker-image won't run and won't start [closed]Postgres docker-image 不会运行,也不会启动[关闭]
【发布时间】:2017-05-01 05:22:19
【问题描述】:

我跑

docker-compose up

我明白了:

$ docker-compose up
Starting asynchttpproxy_db_1
Starting asynchttpproxy_web_1
Attaching to asynchttpproxy_db_1, asynchttpproxy_web_1
db_1   | LOG:  database system was shut down at 2017-05-01 05:11:54 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  | Unhandled exception in thread started by <function check_errors.
<locals>.wrapper at 0x7fae3f078e18>
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 119, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in 
get_new_connection
web_1  |     connection = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/psycopg2/__init__.py", line 164, in connect
web_1  |     conn = _connect(dsn, 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" (127.0.0.1) and 
accepting
web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  |        Is the server running on host "localhost" (::1) and 
accepting
web_1  |        TCP/IP connections on port 5432?
web_1  |
web_1  |
web_1  | The above exception was the direct cause of the following 
exception:
web_1  |
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/utils/autoreload.py", line 229, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/core/management/commands/runserver.py", line 116, in 
inner_run
web_1  |     self.check_migrations()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/core/management/commands/runserver.py", line 168, in 
check_migrations
web_1  |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/migrations/executor.py", line 19, in __init__
web_1  |     self.loader = MigrationLoader(self.connection)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/migrations/loader.py", line 47, in __init__
web_1  |     self.build_graph()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/migrations/loader.py", line 182, in build_graph
web_1  |     self.applied_migrations = recorder.applied_migrations()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/migrations/recorder.py", line 59, in applied_migrations
web_1  |     self.ensure_schema()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/migrations/recorder.py", line 49, in ensure_schema
web_1  |     if self.Migration._meta.db_table in 
self.connection.introspection.table_names(self.connection.cursor()):
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 162, in cursor
web_1  |     cursor = self.make_debug_cursor(self._cursor())
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 135, in _cursor
web_1  |     self.ensure_connection()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", 
line 97, in __exit__
web_1  |     six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/utils/six.py", line 658, in reraise
web_1  |     raise value.with_traceback(tb)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1  |     self.connect()
web_1  |   File "/usr/local/lib/python3.6/site-
packages/django/db/backends/base/base.py", line 119, in connect
web_1  |     self.connection = self.get_new_connection(conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-pac 
kages/django/db/backends/postgresql_psycopg2/base.py", line 176, in 
get_new_connection
web_1  |     connection = Database.connect(**conn_params)
web_1  |   File "/usr/local/lib/python3.6/site-
packages/psycopg2/__init__.py", line 164, in connect

我去 /c/program files/postgresql/9.3/

我愿意

postgres -D data

我明白了

$ postgres -D data
2017-05-01 00:18:14 CDT LOG:  could not bind IPv6 socket: No error
2017-05-01 00:18:14 CDT HINT:  Is another postmaster already running on port 
5432? If not, wait a few seconds and retry.
2017-05-01 00:18:14 CDT LOG:  could not bind IPv4 socket: No error
2017-05-01 00:18:14 CDT HINT:  Is another postmaster already running on port 
5432? If not, wait a few seconds and retry.
2017-05-01 00:18:14 CDT WARNING:  could not create listen socket for "*"
2017-05-01 00:18:14 CDT FATAL:  could not create any TCP/IP sockets

我的 settings.py 中的数据库是:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'async_http_proxy2',
        'USER': 'postgres',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

我的 Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

我的 docker-compose.yml

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

我现在不知道该怎么办。请帮忙。提前致谢。

【问题讨论】:

    标签: python django postgresql docker docker-compose


    【解决方案1】:

    在您的 docker-compose.yml 中创建 postgres 容器和 django 容器之间的链接并公开它们的端口

    postgres:
      image: postgres:latest
      volumes:
        - ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
      env_file:
        - .env
      volumes:
        - /usr/src/app/static
      expose:
        - '5432'
    web:
      build: .
      env_file:
        - .env
      volumes: 
        - .:/code
      links:
        - postgres
      expose:
        - '8000'
    

    创建一个 .env 文件,例如:

    DB_NAME=async_http_proxy2
    DB_USER=postgres
    DB_PASS=your_password
    DB_SERVICE=postgres
    DB_PORT=5432
    

    然后创建一个docker-entrypoint-initdb.d文件来创建数据库

    #!/bin/bash
    set -e
    
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
        CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';
        CREATE DATABASE $DB_NAME;
        GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
    EOSQL
    

    然后在 settings.py 中将数据库设置更改为:

    DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('DB_NAME'),
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASS'),
        'HOST': os.environ.get('DB_SERVICE'),
        'PORT': os.environ.get('DB_PORT'), 
        }
    }
    

    注意

    利用 env 文件隐藏重要的密钥和凭据是很好的

    对文件进行更改后,尝试再次构建 docker-compose up --build

    【讨论】:

    • 嘿,Dean,当我运行“docker compose up”时,如何确保 Django 网络服务器自动启动?另外,我必须在构建后修改 postgres.volumes 吗?提前致谢
    • 另外,当 django 尝试启动 Web 服务器时运行“docker-compose up --build”时,我得到“psycopg2.OperationalError: FATAL: database "async_http_proxy3" does not exist'
    • @MikeJohnsonJr,您需要在 .env 文件中的DB_NAME 上进行设置,并且运行docker-compose up 后必须自动启动django 网络服务器。您可以通过运行docker-compose ps 进行检查
    【解决方案2】:

    您的 django 配置设置为在 localhost 中查找数据库,但是由于 django 在容器中运行,它所指的 localhost 是容器本身,在撰写文件的情况下应该是

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'async_http_proxy2',
            'USER': 'postgres',
            'HOST': 'db',
            'PORT': 5432,
        }
    }
    

    因为 docker-compose 通过简单的名称解析为您创建了一个网络。 因此,在您的情况下,当您的撰写使用 'db' 作为服务名称时,您的 django('web') 容器将能够访问数据库

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 2012-02-12
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多