我遵循的是Aptible Quickstart教程,它描述了如何创建和部署应用程序Aptible的PaaS。我还希望能够在本地运行带有Django应用程序的Docker容器,并连接到本地PostgreSQL数据库,但我不清楚如何做到这一点。
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"]其中mysite是Django应用程序的名称。它的settings.py使用dj-数据库-网址如下:
import dj_database_url
DATABASES = {'default': dj_database_url.config()}为了连接到PostgreSQL的本地实例,我似乎应该遵循Docker解决方案 on 允许码头容器连接到本地/主机postgres数据库:
docker run -e DB_PORT=5432 -e DB_HOST=docker.for.mac.host.internal然而,如果我尝试这样做,我仍然会得到一个错误,以
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?在我看来,目前配置的应用程序并没有对DB_PORT和DB_HOST环境变量“做任何事情”,因为它是一次从DATABASE_URL中读取这些变量。这就是问题所在吗?如果是这样的话,我如何在仍然使用DATABASE_URL时修复这个问题,因为这似乎是Aptible的“接口”?
发布于 2018-02-05 21:43:25
通过将DATABASE_URL文件中的.env文件更改为
DATABASE_URL=postgres://my_app:my_password@localhost/my_database至
DATABASE_URL=postgres://my_app:my_password@docker.for.mac.host.internal/my_database并运行容器,我将其标记为lucy_web,如下所示:
docker run -p 8000:8000 --env-file .env lucy_web没有必要在我的listen_addresses中将'*'更改为'*';这仍然是默认的localhost。(我认为这是因为-p 8000:8000参数将localhost上的端口8000映射到容器)。
发布于 2018-02-05 21:04:25
据我所知,postgres不是在容器上运行,而是作为主机os上的服务运行,对吗?
请在下面检查:
1.检查您的postgres是否接受所有ip地址上的连接
通常postgres只监听本地主机,这意味着拒绝与localhost以外的其他连接。每个码头容器连接到虚拟网络,从容器到主机的通信通过该虚拟网络进行。
更新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)2.使用正确的ip地址/主机名
如前所述,容器通过这个虚拟网络与主机进行通信。确保您正在使用正确的主机名或ip地址。
localhost将无法工作,因为在容器上下文中,localhost引用容器。
至少对我来说它不起作用(mac上的docker-ce),看主机分辨率不起作用。
docker run --rm -it postgres:10 ping docker.for.mac.host.internal
ping: unknown host我正在使用我的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因此,尝试使用有效的ip地址,无论是对接设备的ip还是物理网络接口中的ip。
3.使用码头组合文件
使用docker文件来运行组合或群集,参见这里的示例应用程序:https://docs.docker.com/engine/swarm/stack-deploy/#create-the-example-application
发布于 2021-02-14 18:43:10
Windows用户将其env配置为:
DATABASE_URL=postgres://my_app:my_password@docker.host.internal/my_databasehttps://stackoverflow.com/questions/48630545
复制相似问题