<p>如您所述<code>.env</code>文件是为docker构建设置环境变量的良好实践。<br/>
根据你的问题:</p>
<blockquote>
<p>I am not sure if I still need to create the database locally on my system, or whether Docker handles it automatically.</p>
</blockquote>
<p>如果同时使用docker compose部署postgres容器,则不必在本地创建数据库。如果是这种情况,我建议您将这两个容器连接到您选择的网络,并命名容器,以便django容器可以使用主机名<code>postgres</code>。
可以有选择地将<code>.env</code>文件提供给容器(粘贴docker compose文件时可能会移动缩进),下面的<code>docker-compose.yml</code>文件应该如下所示</p>
<pre><code> version: '3.5'
services:
postgres:
container_name: postgres
image: postgres:11.6
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
volumes:
- ~/srv/docker/template-postgres/data:/var/lib/postgresql/data:rw
networks:
- network_name
django:
container_name: django
build:
context: .
dockerfile: ./conf/django/Dockerfile.windows
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
volumes:
- ./back:/app:rw
depends_on:
- postgres
env_file:
- .env
networks:
- network_name
networks:
network_name:
name: network_name
</code></pre>
<p>您可能会面临用户权限问题,因此您可能希望研究<a href="https://stackoverflow.com/questions/55243023/docker-compose-postgresql-create-db-user-pass-and-grant-permission">this</a>问题,该问题可以指导您为数据库创建<code>Dockerfile</code></p>