有人可以帮我吗?
Dockerfile 用于我的图像:
FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /hlcup
WORKDIR /hlcup
ADD requirements.txt /hlcup/
RUN pip install --upgrade pip
RUN pip3 install -r requirements.txt
ADD . /hlcup/
EXPOSE 80
EXPOSE 5432
我的 docker-compose.yml :
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- "5432:5432"
web:
build: .
command: python3 main.py
volumes:
- .:/hlcup
- ./data:/tmp/data/
ports:
- "80:80"
depends_on:
- db
我运行我的构建: docker-compose up --build
结果是,应用程序应连接到数据库,但出现连接错误:
web_1 | conn = await asyncpg.connect(**DB_PARAMS)
web_1 | File "/usr/local/lib/python3.6/site-packages/asyncpg/connection.py", line 1688, in connect
web_1 | max_cacheable_statement_size=max_cacheable_statement_size)
web_1 | File "/usr/local/lib/python3.6/site-packages/asyncpg/connect_utils.py", line 551, in _connect
web_1 | raise last_error
web_1 | File "/usr/local/lib/python3.6/site-packages/asyncpg/connect_utils.py", line 543, in _connect
web_1 | connection_class=connection_class)
web_1 | File "/usr/local/lib/python3.6/site-packages/asyncpg/connect_utils.py", line 513, in _connect_addr
web_1 | connector, timeout=timeout, loop=loop)
web_1 | File "/usr/local/lib/python3.6/asyncio/tasks.py", line 352, in wait_for
web_1 | return fut.result()
web_1 | File "/usr/local/lib/python3.6/asyncio/base_events.py", line 776, in create_connection
web_1 | raise exceptions[0]
web_1 | File "/usr/local/lib/python3.6/asyncio/base_events.py", line 763, in create_connection
web_1 | yield from self.sock_connect(sock, address)
web_1 | File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 451, in sock_connect
web_1 | return (yield from fut)
web_1 | File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 481, in _sock_connect_cb
web_1 | raise OSError(err, 'Connect call failed %s' % (address,))
web_1 | ConnectionRefusedError: [Errno 111] Connect call failed ('0.0.0.0', 5432)
答案 0 :(得分:0)
您的容器web
尝试使用本地IP 0.0.0.0:5432
连接到数据库,而数据库位于另一个IP所在的其他容器上。
docker-compose支持容器之间的DNS,因此我尝试将python应用程序中的IP地址更改为DNS。
在您的docker-compose文件中,postgres数据库的DNS为db