Jenn-ccf
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Docker tutorial -2 === ![](https://www.capdio.com/wp-content/uploads/2024/07/Docker-Logo.png) **📌 本次內容:** - 端口映射(Port mapping) - 建立 Dockerfile,打包自己的image - Docker compose - 實作一個本地的LLM(Ollama+Open WebUI) --- ![](https://goodvibe.tw/wp-content/uploads/2023/12/basic-website-development-image-7-1024x453.png.webp) ## 端口映射(Port mapping) 在 Docker 中,容器內部運行的服務(例如 Web 伺服器、資料庫等)通常會綁定到**容器的內部端口**,但這個內部端口對外部世界是不可見的。 端口映射(Port mapping)是讓**容器內的端口**與**本機的端口**對接,這樣才能夠從主機或其他容器訪問容器內運行的服務。 :::info 端口映射(Port mapping)就像是把辦公室內線電話(容器端口)轉接到手機(本機端口),這樣外部才能撥打這支電話。 ::: ### `-p` 參數 #### 語法格式 ``` -p <host_port>:<container_port> ``` - `host_port`:宿主機上的端口 - `container_port`:容器內部的端口 #### 範例 1. **Web 伺服器運行** 假設要啟動一個運行 Web 伺服器的容器,並希望讓本機或其他設備能夠通過瀏覽器訪問它。 ``` $ docker run -d -p 8080:80 my_web_image ``` - `-p 8080:80`:將宿主機的端口 `8080` 映射到容器的端口 `80` - 可以通過 `http://localhost:8080` 來訪問容器內部的 Web 伺服器 2. **Web + Database 運行** 也可以在啟動容器時,進行**多端口映射**,這樣就能同時訪問容器內部的多個服務 ``` $ docker run -d -p 8080:80 -p 3306:3306 my_database_web_image ``` - `8080` 端口訪問容器內部的 Web 伺服器 - `3306` 端口訪問容器內部的 MySQL 資料庫 ### 端口數字 #### 容器內端口 1. 查看 image 官方說明 2. 自己創建 Dockerfile 時定義 #### 宿主機端口 可以自訂數字,但有幾個小規則: 1. `8080`, `8888`, `3000`, `5000` 等是開發常用的 port 👍🏻 2. 不要重複選到正在運行的 port 3. 避開常見系統服務的 port(如:`80`, `443`, `3306`...) 4. `1024` 以下的 port 會需要 sudo 權限 5. 可使用 `docker container ls` 檢查正在運行的容器端口映射情況 ## Dockerfile Dockerfile 是一個用 YAML 語法格式撰寫的純文字檔。裡面會透過多種「指令」來一步一步描述如何建置出一個 Image。 通常會創建在你本機中,專案的資料夾底下,如下結構圖。 ``` myProject/ ├── app.py ├── requirements.txt └── Dockerfile ``` :::info Docker 預設會找尋目錄中的 "Dockerfile" 來執行,所以這個檔案名稱是約定成俗的,通常不建議自行改名~ ::: ### Dockerfile 常用創建指令 | 指令 | 說明 | 格式 |範例 | |----------------|-------|-------|-------| | `FROM` | 設定要使用哪個基底 Image,所有 Dockerfile 都要有這個指令。 | `FROM <image>`, `FROM <image>:<tag>` |`FROM python:3.10` | | `WORKDIR` | 設定容器的工作目錄,後續的指令都會在這個目錄下執行。 | `WORKDIR <path>` |`WORKDIR /app` | | `COPY` | 複製本地檔案到容器內。 | `COPY <source path> <dist path>`|`COPY requirements.txt .`(`.`的意思是當前目錄) | | `RUN` | 在建構 Image 時執行的命令,常用來安裝軟體包或執行其他初始化操作。 | `RUN <command> && <command>`(一個`RUN` 就是開啟一個新的映像層,所以只會寫一個`RUN`,用`&&`來連接不同執行步驟)|`RUN apt-get update && apt-get install -y python3` | | `CMD` | 設定容器啟動後執行的命令,通常只會有一個 `CMD` 指令。 | `CMD [“command name”, argv1, argv2, …]` |`CMD ["python", "app.py"]` | | `EXPOSE` | 指定容器內(服務器)的端口。 | `EXPOSE <port>` |`EXPOSE 8080` | | `LABEL` | 添加一些資訊,如:版本號、作者、描述等。 | `LABEL <key1>=<value1> <key2>=<value2> ` |`LABEL version="1.0" maintainer="Michael"` | #### Dockerfile 範例 ``` FROM python:3.10 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"] ``` ### 創建 Image ``` docker build -t <name>:<tag> <Dockerfile path> ``` - `-t`, `--tag`:指定 Image 的名稱與版本標籤。 - `<name>`:看自己想要為這個 Image 取什麼名字。 - `<tag>`:預設是`:latest`,開發測試中可以用 `:dev` 或 `:test`,穩定版本可以用 `:v1.0` 或`:stable` 等等。 - `<Dockerfile path>`:Dockerfile所在的目錄。通常會先進到該專案資料夾中,這時可以用 `.` 表示當前工作目錄。 #### 實作 ``` $ docker build -t 0509_demo:test . $ docker run -d -p 7860:7860 0509_demo:test # 瀏覽器:http://localhost:7860 ``` ### 推上 Docker Hub ``` $ docker login # 格式 $ docker tag <local-image-name> <dockerhub-username>/<image-name>:<tag> $ docker tag 0509_demo jack555/0509_demo:dev # 格式 $ docker push <dockerhub-username>/<image-name>:<tag> $ docker push jack555/0509_demo:dev ``` ## Docker compose Docker Compose 是一個工具,它可以用一個 YAML 檔案,通常叫做 docker-compose.yml,來定義和管理多個 Container 的服務。 例如下圖:在專案資料夾中建立 docker-compose.yml,即可用一行指令,根據 docker-compose.yml 去同時啟動 Web 伺服器 + 後端 API + 資料庫。 ``` myProject/ ├── docker-compose.yml ├── frontend/ │ ├── Dockerfile │ └── ... │ ├── backend/ │ ├── Dockerfile │ ├── requirements.txt │ └── ... │ └── db/ ├── init.sql └── ... ``` ### docker-compose.yml 寫法 ``` services: 服務名稱: image: 映像檔名稱 container_name: 自定義容器名稱 ports: - <主機 port>:<容器 port> volumes: - <主機 path>:<容器 path> environment: - 環境變數=值 depends_on: - 其他服務名稱 networks: - 自定義網路名稱 restart: always / unless-stopped / no / on-failure command: 覆蓋預設執行指令 volumes: volume名稱: networks: 網路名稱: ``` ### 實作:Ollama + Open WebUI ``` services: ollama: # 服務 1 image: ollama/ollama container_name: ollama ports: - "11434:11434" # Ollama API port volumes: - ollama-data:/root/.ollama # 儲存模型資料 restart: unless-stopped open-webui: # 服務 2 image: ghcr.io/open-webui/open-webui:main container_name: open-webui ports: - "3000:8080" # 網頁前端 http://localhost:3000 environment: - OLLAMA_API_BASE_URL=http://ollama:11434 depends_on: - ollama restart: unless-stopped volumes: - openwebui-data:/app/backend/data # 儲存 WebUI 設定資料 volumes: ollama-data: openwebui-data: ``` #### 運行 根據 docker-compose.yml 啟動所有服務。 ``` $ docker compose up -d # -d 不佔用終端機的方式在背景運行 # 瀏覽器:http://localhost:3000 ``` #### 關閉 停掉所有服務,並刪除 Container(但不會刪掉 Image 和 Volume)。 ``` $ docker compose down ``` 暫時停掉,但不刪除 Container。 ``` $ docker compose stop ``` #### 重新啟動 啟動已存在的 Container,不會重新建構。 ``` $ docker compose start ``` #### 選擇模型 ``` $ docker exec -it ollama ollama pull <LLM>:<tag> $ docker exec -it ollama ollama pull gemma3:1b ``` [官方 Ollama 模型 library](https://ollama.com/library):去選擇你想要跑什麼模型 ## References 1. [Docker 進階實戰:Volume、Port Mapping 與 Docker Compose 完整指南](https://realnewbie.com/basic-concent/architecture/docker-advanced-hands-on-guide-volume-port-mapping-and-docker-compose/) 2. [Docker 學習筆記 (四) — 如何撰寫Dockerfile](https://medium.com/%E4%B8%80%E5%80%8B%E5%B0%8F%E5%B0%8F%E5%B7%A5%E7%A8%8B%E5%B8%AB%E7%9A%84%E9%9A%A8%E6%89%8B%E7%AD%86%E8%A8%98/docker-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%9B%9B-%E5%A6%82%E4%BD%95%E6%92%B0%E5%AF%ABdockerfile-2a209b485530) 3. [使用 Docker Compose 摻在一起做懶人包 ](https://ithelp.ithome.com.tw/articles/10243618)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully