Postgres docker-image不会运行并且不会开始

时间:2017-05-01 05:22:19

标签: python django postgresql docker docker-compose

我跑

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

此时我不知道该怎么做。请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

你的django配置被设置为在localhost中查找数据库,但是由于django在容器中运行,它所引用的localhost就是容器本身,在compose文件中它应该是

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

因为docker-compose为您创建了一个简单的名称解析网络。 所以在你的情况下你的作品使用&#39; db&#39;作为您的django(&#39; web&#39;)容器将能够访问数据库的名称的服务名称

相关问题