使用 Postgres 和 Redis 的 docker-compose
用一条命令运行完整技术栈
使用 Postgres 和 Redis 的 docker-compose 是 CoddyKit 上的免费 Django Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
One Command, Whole Stack
docker-compose defines many containers in one file, so Django, Postgres, and Redis start together with a single command. 🧩
The compose File
Everything lives in docker-compose.yml, where each container you need becomes an entry under the services key.
services:
web:
db:
redis:The web Service
Your web service builds from your Dockerfile and runs Django, the heart of the whole stack.
web:
build: .
command: gunicorn config.wsgi:application --bind 0.0.0.0:8000Add Postgres
The db service uses the official Postgres image, giving you a real database without installing anything locally.
db:
image: postgres:16Add Redis
The redis service runs the official Redis image, ready to act as a cache or a Celery message broker.
redis:
image: redis:7Connect by Service Name
Compose gives each service a DNS name, so Django reaches Postgres at the host db, not localhost.
DATABASE_URL=postgres://user:pass@db:5432/appWait with depends_on
Use depends_on so web starts after db and redis, keeping your startup order sane.
web:
depends_on:
- db
- redisPersist Data with Volumes
A named volume stores Postgres data outside the container, so your rows survive restarts and rebuilds.
db:
volumes:
- pgdata:/var/lib/postgresql/dataPass Configuration as Env
Feed secrets and settings through environment or an env_file, keeping config out of your image.
db:
environment:
POSTGRES_DB: app
POSTGRES_PASSWORD: passMap Ports to Your Host
The ports key forwards a container port to your machine, so you can open the app in a browser.
web:
ports:
- "8000:8000"Bring It All Up
Run docker compose up and the entire stack boots together; add -d to run it in the background.
docker compose up -dQuick Check
How does your web container reach the database in Compose?
Recap
You wired a full stack in docker-compose: web, db, and redis services, connected by name, with volumes for data and one command to launch. 🚀
常见问题解答
「使用 Postgres 和 Redis 的 docker-compose」课时是免费的吗?
是的 — 「使用 Postgres 和 Redis 的 docker-compose」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「使用 Postgres 和 Redis 的 docker-compose」这节课中我会学到什么?
用一条命令运行完整技术栈 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Postgres 和 Redis 的 docker-compose」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 编写 Django Dockerfile
- 使用 Postgres 和 Redis 的 docker-compose
- 入口点、迁移与 collectstatic
- 生产镜像最佳实践