[英]How to connect a Docker container to PostgreSQL running on the localhost using a DATABASE_URL?
I'm following the Aptible Django Quickstart Tutorial which described how to create and deploy an app Aptible's PaaS.我正在关注Aptible Django 快速入门教程,该教程描述了如何创建和部署应用程序 Aptible 的 PaaS。 I would also like to be able to run the Docker container with the Django app locally and connect to a local PostgreSQL database, however, and it is unclear to me how to do this.
我还希望能够在本地运行带有 Django 应用程序的 Docker 容器并连接到本地 PostgreSQL 数据库,但是,我不清楚如何做到这一点。
The Dockerfile
for the Django app is: Django 应用程序的
Dockerfile
是:
FROM python:3.6
# System prerequisites
RUN apt-get update \
&& apt-get -y install build-essential libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# If you require additional OS dependencies, install them here:
# RUN apt-get update \
# && apt-get -y install imagemagick nodejs \
# && rm -rf /var/lib/apt/lists/*
# Install Gunicorn. If Gunicorn is already present in your requirements.txt,
# you don't need that (but if won't hurt).
RUN pip install gunicorn
ADD requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt
ADD . /app
EXPOSE 8000
CMD ["gunicorn", "--access-logfile=-", "--error-logfile=-", "--bind=0.0.0.0:8000", "--workers=3", "mysite.wsgi"]
where mysite
is the name of the Django app.其中
mysite
是 Django 应用程序的名称。 Its settings.py
uses dj-database-url as follows:它的
settings.py
使用dj-database-url如下:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}
In order to connect to a local instance of PostgreSQL, it would seem I should follow the Docker for Mac Solution on Allow docker container to connect to a local/host postgres database :为了连接到 PostgreSQL 的本地实例,我似乎应该遵循Docker for Mac 解决方案上的Allow docker container to connect to a local/host postgres database :
docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal
However, if I try this I still get an error ending with但是,如果我尝试此操作,我仍然会收到以结尾的错误
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
It would seem to me that the app as currently configured is not 'doing anything' with the DB_PORT
and DB_HOST
environment variables, because it is reading these in one go from the DATABASE_URL
.在我看来,当前配置的应用程序没有对
DB_PORT
和DB_HOST
环境变量“做任何事情”,因为它正在从DATABASE_URL
一次性读取这些。 Is this the problem?这是问题吗? If so, how could I fix this while still using
DATABASE_URL
as this appears to be the 'interface' to Aptible?如果是这样,我如何在仍然使用
DATABASE_URL
同时解决这个问题,因为这似乎是 Aptible 的“接口”?
I managed to fix the problem by changing the DATABASE_URL
in the .env
file from我设法通过更改
.env
文件中的DATABASE_URL
从
DATABASE_URL=postgres://my_app:my_password@localhost/my_database
to到
DATABASE_URL=postgres://my_app:my_password@docker.for.mac.host.internal/my_database
and running the container, which I tagged lucy_web
, like so:并运行我标记为
lucy_web
的容器,如下所示:
docker run -p 8000:8000 --env-file .env lucy_web
It was not necessary to change listen_addresses
to '*'
in my postgresql.conf
;没有必要在我的
postgresql.conf
中将listen_addresses
更改为'*'
; this was still at its default, localhost
.这仍然是它的默认值,
localhost
。 (I believe this is because the -p 8000:8000
argument maps port 8000 on localhost
to the container). (我相信这是因为
-p 8000:8000
参数将localhost
上的端口 8000 映射到容器)。
As I understand, postgres is not running on a container but as a service on your host os, right?据我了解,postgres 不是在容器上运行,而是作为主机操作系统上的服务运行,对吗?
Please theck below:请点击以下:
Usually postgres listens to localhost only, that means, connections to other than localhost
are refused.通常 postgres 只侦听 localhost,这意味着拒绝与
localhost
以外的连接。 Each docker container is attached to a virtual network and communication from container to host happens through that virtual network.每个 docker 容器都连接到一个虚拟网络,从容器到主机的通信通过该虚拟网络发生。
Update your postgresql.conf
file and add:更新您的
postgresql.conf
文件并添加:
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
As stated before, container communicates to host through this virtual network.如前所述,容器通过这个虚拟网络与主机通信。 Make sure your are using the correct hostname or ip address.
确保您使用的是正确的主机名或 IP 地址。
localhost
is not going to work because from within the container context, localhost refers to the container localhost
不会工作,因为在容器上下文中, localhost 指的是容器
At least for me it doesn't work (docker-ce on a mac), see that host resolution is not working至少对我来说它不起作用(mac上的docker-ce),看到主机分辨率不起作用
docker run --rm -it postgres:10 ping docker.for.mac.host.internal
ping: unknown host
I'm using my the ip address given by my dhcp server, ping works我正在使用我的 dhcp 服务器提供的 IP 地址,ping 工作正常
docker run --rm -it postgres:10 ping 192.168.0.* -c 1
PING 192.168.0.* (192.168.0.*): 56 data bytes
64 bytes from 192.168.0.*: icmp_seq=0 ttl=37 time=0.455 ms
So try using a valid ip address, either the ip of the docker device or the ip from your physical network interface.因此,请尝试使用有效的 ip 地址,无论是 docker 设备的 ip 还是来自物理网络接口的 ip。
Use a docker compose file to run with either compose or swarm, see sample application here: https://docs.docker.com/engine/swarm/stack-deploy/#create-the-example-application使用 docker compose 文件与 compose 或 swarm 一起运行,请参阅此处的示例应用程序: https : //docs.docker.com/engine/swarm/stack-deploy/#create-the-example-application
Windows 用户配置他们的 env 以使用:
DATABASE_URL=postgres://my_app:my_password@docker.host.internal/my_database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.