2024-09-30

【技術筆記】Windows上建置Docker Image

 最近太多事,驚覺2024都還沒有文章,趕快隨手寫一篇

【環境】

  • Windows 10,企業版,嗯...應該吧
  • 題外話,敝司規模不小,無法免費使用Docker Desktop for Windows,也許你會問為什麼不直接用Linux建?小朋友~大公司有很多咩咩角角一時半刻說不完低
Windows安裝WSL2
  • 需先啟用Hypver-V,打開Power Shell,指令:
    • Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
    • Enable-WindowsOptionalFeature -Online -FeatureName Containers -All
  • 執行C:\WINDOWS\system32> Services.msc,GUI設定server為開啟

WSL2安裝Ubuntu

  • Power Shell,指令:C:\WINDOWS\system32> wsl --install
  • Ubuntu需先配置
    • sudo apt-get update -o Acquire::http::Proxy="http://{ip}:{port}"
    • sudo apt install docker.io -o Acquire::http::Proxy="http://{ip}:{port}"
Ubuntu克服網路問題
  • sudo mkdir -p /etc/systemd/system/docker.service.d
  • sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
    • [Service] Environment="HTTP_PROXY=http://{ip}:{port}" Environment="HTTPS_PROXY=http://{ip}:{port}"
  • sudo systemctl daemon-reload
  • sudo systemctl restart docker
撰寫Dockerfile
# 使用官方 Python 映像作為基礎映像
FROM python:3.11-slim
# 設定工作目錄
WORKDIR /opt/ai-traffic-predictor
# 升级 pip 到最新版本
RUN pip install --upgrade pip
# 複製需求檔案到容器
COPY requirements.txt .
# 安裝依賴包
RUN pip install --no-cache-dir -r requirements.txt
# 複製程式碼
#COPY . .
# 指定容器啟動時的執行指令
CMD ["python", "ai_master.py"]

 製作Docker Image

  • Ubunto預設會將本機(Host)的磁碟用/mnt/{槽}掛載
  • 爬到你要製作image的目錄,指令:sudo docker build --build-arg http_proxy=http://{ip}:{port} --build-arg https_proxy=http://{ip}:{port} -t {image_name} . --no-cache
  • docker run -d --net=host --restart=unless-stopped --name {image_name} -v /opt/app:/opt/app

與Docker Repository互動,也可以是私有Repository
  • docker push harbor.xxx.com:{port}/{image_name}:{image_version}
  • docker pull {image_name}:{image_version}

沒有留言:

張貼留言