Ubuntu 容器化部署实操指南
一 环境准备与 Docker 安装
二 构建镜像与运行容器
示例一 基于官方 Ubuntu 镜像运行临时环境
示例二 将应用容器化(以 Python 为例)
FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
三 多容器应用与 Docker Compose
version: "3.8"
services:
web:
build: .
ports:
- "5000:5000"
environment:
- REDIS_HOST=redis
depends_on:
- redis
redis:
image: redis:7-alpine
四 生产可用要点
五 扩展 Kubernetes 部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: app
image: <your-registry>/myapp:1.0
ports:
- containerPort: 5000
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 5000
type: LoadBalancer