Docker - 05 - 构建流程

本地启用compose流程(写的有点烂,简单记一下吧)

流程概览

复制代码
1. 概念验证(hello-world / volume)
2. Dockerfile 构建 API 镜像 → docker run 单容器
3. Compose 起 postgres + redis → migrate
4. Compose 加 api → 服务名互联
5. Compose 加 worker → 全链路
6. 环境变量整理 → docker compose up -d --build 一键启动

1. 概念验证

powershell 复制代码
docker version
docker run --rm hello-world
docker ps -a

# Volume 体验
docker run --rm -v demo-vol:/data alpine sh -c "echo hello > /data/test.txt && cat /data/test.txt"
docker volume ls
docker volume rm demo-vol   # 可选清理

2. Dockerfile ------ 单容器跑 API

文件: Dockerfile、.dockerignore

powershell 复制代码
docker build -t nodejs-api .

docker run --rm -p 8080:8080 `
  -e DATABASE_URL="postgresql://postgres:密码@host.docker.internal:5432/nodejs_study?schema=public" `
  -e REDIS_URL="redis://host.docker.internal:6379" `
  -e JWT_SECRET="你的JWT_SECRET" `
  nodejs-api

curl http://localhost:8080/health

API 在容器内连宿主机服务 → 用 host.docker.internal,不用 localhost。


3. Compose ------ postgres + redis

文件: docker-compose.yml(先只写 postgres、redis)

powershell 复制代码
docker compose up -d
docker compose ps

docker exec nodejs-redis redis-cli ping   # 期望 PONG

# 初始化 Compose 空库(端口看 compose 映射,本项目是 5433)
$env:DATABASE_URL="postgresql://postgres:密码@localhost:5433/nodejs_study?schema=public"
npx prisma migrate deploy

端口冲突时改 compose 映射,如 "5433:5432";宿主机 migrate 用映射后的端口(5433),不是 5432。


4. Compose ------ 加 api

在 docker-compose.yml 追加 api 服务后:

powershell 复制代码
docker compose up -d --build api
docker compose ps
docker compose logs api --tail 30

curl http://localhost:8080/health
curl http://localhost:8080/api/products

容器内连 PG/Redis → @postgres:5432、redis://redis:6379(服务名,不是 localhost)。


5. Compose ------ 加 worker

在 docker-compose.yml 追加 worker 服务后:

powershell 复制代码
docker compose up -d worker
docker compose logs worker --tail 10

# 下单后看 worker 是否消费
docker compose logs worker --tail 5

6. 环境变量 + 一键启动

.env(宿主机 / compose 变量替换):

env 复制代码
POSTGRES_PASSWORD=你的密码
DATABASE_URL="postgresql://postgres:密码@localhost:5432/nodejs_study?schema=public"
REDIS_URL="redis://localhost:6379"
JWT_SECRET="..."

compose 里 api 环境变量(容器内):

yaml 复制代码
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD}@postgres:5432/nodejs_study?schema=public
REDIS_URL: redis://redis:6379
JWT_SECRET: ${JWT_SECRET}

操作 Compose 库时(migrate / studio / test),临时覆盖,不必改 .env:

powershell 复制代码
$env:DATABASE_URL="postgresql://postgres:密码@localhost:5433/nodejs_study?schema=public"
npx prisma migrate deploy
npx prisma studio
npm test

日常命令:

powershell 复制代码
# 一键启动全部
docker compose up -d --build

# 查看状态 / 日志
docker compose ps
docker compose logs api --tail 30
docker compose logs worker --tail 10

# 停止(Volume 保留)
docker compose down

# 停止并删 Volume(慎用,丢库)
docker compose down -v

连接串速查

场景 DATABASE_URL REDIS_URL
宿主机 npm run dev @localhost:5432 redis://localhost:6379
单容器 docker run API @host.docker.internal:5432 redis://host.docker.internal:6379
Compose 内 api / worker @postgres:5432 redis://redis:6379
宿主机操作 Compose 库 @localhost:5433 redis://localhost:6379
相关推荐
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
分布式存储与RustFS3 天前
MinIO 官方 Docker 镜像被移除:依赖它的项目该怎么办
docker·云原生·devops·对象存储·minio·分布式存储
AI职业加油站3 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
此冬歌咏3 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
玉&心3 天前
通过Arthas在线诊断K8S中的内存及JVM等使用情况
docker·k8s·arthas
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
-梅3 天前
linux(8) 软硬链接
linux·运维·服务器
赵文宇(温玉)3 天前
CoreDNS的hosts插件的缺行配置导致k8s集群异常
容器·kubernetes·rancher
guo_wen_qiang3 天前
上传本地镜像到harbor中
docker·容器·持续部署
张洛闻Eren3 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github