python - Postgres docker-image 不会运行也不会启动

标签 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

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

最佳答案

在您的 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 文件来隐藏重要的 key 和凭据

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

关于python - Postgres docker-image 不会运行也不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43714645/

相关文章:

python - 使用 zipfile 在 python 中执行 mysql 查询

python - 如何在不使用循环的情况下生成循环数字序列?

django - 如何将受邀用户关联到邀请人的公司/群组?

python - django 的分页器中的孤儿是什么意思?

python-2.7 - 命令 "python setup.py egg_info"失败,错误代码为 1 pygresql

sql - 在 PostgreSQL 中插入带单引号的文本

python - 使用 Python 通过串行通信的最小延迟

python - 在 pandas Dataframe 中按级别创建计数器,优化

python - Django表单中标签值之前的下划线是什么意思

postgresql - 如何生成每个 id 的字符串列表?