Postgres泊坞窗图像将不会运行,不会启动

我跑

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是:

 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容器之间创build一个链接,并显示它们的端口

 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' 

创build一个.env文件,如:

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

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

 #!/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'), } } 

做logging

利用env文件来隐藏重要的密钥和证书是很好的

在对文件进行更改后,再次尝试执行构builddocker-compose up --build

你的djangoconfiguration设置为在本地主机上查找数据库,但是由于django运行在一个容器中,它所指向的本地主机就是容器本身,在组成文件的情况下,它应该是

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

becouse docker-compose使用简单的名称parsing为您创build一个networking。 所以在你的情况下,当你的撰写使用“数据库”作为服务名称是您的django('networking')容器将能够到数据库