Skip to content
⚠️ This article was written in 2018. Some content may be outdated.

前端專案 Docker 容器化入門

最近把幾個前端專案容器化了,發現比想象中簡單。記錄一下基礎配置。

為什麼前端需要 Docker

  • 統一開發/測試/生產環境(消滅"在我機器上能跑")
  • CI/CD 流水線部署更簡單
  • 多個專案共存,互不干擾

Dockerfile(基礎版)

dockerfile
# 多階段構建:構建階段
FROM node:10-alpine AS builder

WORKDIR /app

# 先複製 package.json,利用 Docker 層快取
# 只有 package.json 變化才重新 npm install
COPY package.json package-lock.json ./
RUN npm ci

# 複製原始碼並構建
COPY . .
RUN npm run build

# 生產階段:只包含 Nginx 和構建產物
FROM nginx:alpine

# 複製構建產物
COPY --from=builder /app/dist /usr/share/nginx/html

# 複製 Nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

nginx
server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.html;

    # SPA 路由支援(所有路由指向 index.html)
    location / {
        try_files $uri $uri/ /index.html;
    }

    # 靜態資源快取
    location ~* \.(js|css|png|jpg|gif|ico|svg|woff2?)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # API 代理(可選)
    location /api {
        proxy_pass http://api-server:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

.dockerignore

node_modules
dist
.git
.env.local
*.log

常用命令

bash
# 構建映象
docker build -t my-frontend:latest .

# 執行容器(對映 8080 → 容器 80)
docker run -d -p 8080:80 --name my-app my-frontend:latest

# 檢視執行中的容器
docker ps

# 檢視日誌
docker logs my-app

# 停止和刪除
docker stop my-app && docker rm my-app

# 進入容器除錯
docker exec -it my-app sh

docker-compose 多服務

yaml
# docker-compose.yml
version: "3.8"

services:
  frontend:
    build: ./frontend
    ports:
      - "80:80"
    depends_on:
      - api
    environment:
      - VUE_APP_API_URL=http://api:3000

  api:
    build: ./api
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DB_URL=mongodb://db:27017/mydb
    depends_on:
      - db

  db:
    image: mongo:4
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:
bash
# 啟動所有服務
docker-compose up -d

# 檢視日誌
docker-compose logs -f frontend

# 停止
docker-compose down

構建時注入環境變數

dockerfile
ARG VUE_APP_API_URL=https://api.example.com
ENV VUE_APP_API_URL=${VUE_APP_API_URL}

RUN npm run build
bash
# 構建時傳參
docker build --build-arg VUE_APP_API_URL=https://api.staging.com -t my-app:staging .

小結

  • 多階段構建:Node 映象構建,Nginx 映象部署,最終映象很小
  • .dockerignore 排除不需要的檔案,加快構建速度
  • Nginx 配置 SPA 路由支援(try_files)
  • docker-compose 管理多服務開發環境

MIT Licensed