- Docker 已安裝(版本 20.10 或更高)
- Docker Compose 已安裝(版本 2.0 或更高)
檢查版本:
docker --version
docker-compose --versionnpm run docker:compose:up或直接使用 docker-compose:
docker-compose up -dnpm run docker:compose:logs或:
docker-compose logs -fnpm run docker:compose:downnpm run docker:compose:rebuildnpm run docker:build或:
docker build -t tetris-game:latest .npm run docker:run或:
docker run -d \
-p 8800:8800 \
-p 3500:3500 \
--name tetris-game \
tetris-game:latestnpm run docker:stop或:
docker stop tetris-game && docker rm tetris-game容器啟動後,可以通過以下地址訪問:
- 客戶端: http://localhost:3500
- 服務器 API: http://localhost:8800
- 配置端點: http://localhost:8800/config
在 Dockerfile 中設置:
ENV REACT_APP_SERVER_PORT=8800
ENV REACT_APP_CLIENT_PORT=3500
ENV REACT_APP_SERVER_HOST=0.0.0.0編輯 docker-compose.yml 文件:
environment:
- REACT_APP_SERVER_PORT=8801
- REACT_APP_CLIENT_PORT=3501
- REACT_APP_SERVER_HOST=0.0.0.0docker run -d \
-p 8801:8801 \
-p 3501:3501 \
-e REACT_APP_SERVER_PORT=8801 \
-e REACT_APP_CLIENT_PORT=3501 \
-e REACT_APP_SERVER_HOST=0.0.0.0 \
--name tetris-game \
tetris-game:latestdocker psdocker logs tetris-game
docker logs -f tetris-game # 實時查看docker exec -it tetris-game shdocker stats tetris-gamedocker restart tetris-game容器內置健康檢查:
docker inspect --format='{{.State.Health.Status}}' tetris-game可能的狀態:
starting- 正在啟動healthy- 運行正常unhealthy- 運行異常
# Windows
netstat -ano | findstr "8800"
netstat -ano | findstr "3500"# Linux/Mac
lsof -i :8800
lsof -i :3500docker logs tetris-gamedocker ps -adocker ps | grep tetris-gamedocker port tetris-game應該顯示:
3500/tcp -> 0.0.0.0:3500
8800/tcp -> 0.0.0.0:8800
curl http://localhost:3500
curl http://localhost:8800/configdocker system prune -adocker build --no-cache -t tetris-game:latest .docker build --check -t tetris-game:latest .# 構建帶版本標籤的鏡像
docker build -t tetris-game:v2.0.0 .
docker build -t tetris-game:latest .
# 運行特定版本
docker run -d \
-p 8800:8800 \
-p 3500:3500 \
--name tetris-game \
--restart unless-stopped \
tetris-game:v2.0.0# 登錄 Docker Hub
docker login
# 標記鏡像
docker tag tetris-game:latest yourusername/tetris-game:latest
docker tag tetris-game:v2.0.0 yourusername/tetris-game:v2.0.0
# 推送鏡像
docker push yourusername/tetris-game:latest
docker push yourusername/tetris-game:v2.0.0# 拉取鏡像
docker pull yourusername/tetris-game:latest
# 運行容器
docker run -d \
-p 8800:8800 \
-p 3500:3500 \
--name tetris-game \
--restart unless-stopped \
yourusername/tetris-game:latest構建支持多架構的鏡像:
# 創建並使用 buildx builder
docker buildx create --name multiarch --use
# 構建多架構鏡像並推送
docker buildx build \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
-t yourusername/tetris-game:latest \
--push \
.-
不要在鏡像中存儲敏感信息
- 使用環境變量傳遞配置
- 使用 Docker secrets 管理敏感數據
-
定期更新基礎鏡像
docker pull node:18-alpine docker build --no-cache -t tetris-game:latest . -
掃描安全漏洞
docker scan tetris-game:latest
-
限制容器資源
docker run -d \ --cpus="1.0" \ --memory="512m" \ -p 8800:8800 \ -p 3500:3500 \ tetris-game:latest
- Docker 和 Docker Compose 已安裝
- 構建 Docker 鏡像成功
- 容器啟動成功
- 端口映射正確(8800, 3500)
- 健康檢查通過
- 可以訪問 http://localhost:3500
- 多個客戶端可以連接
- 遊戲功能正常運行
- 日誌無錯誤訊息
# 使用 nodemon 進行熱重載
npm run dev# 使用 Docker 部署
docker-compose up -d最後更新: 2025 年 10 月 1 日
Docker 版本: 20.10+
Node.js 版本: 18 LTS