aFish
    • 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

      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.
      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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

    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.
    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: 1082, lsa --- # Week8 - Book mode https://hackmd.io/@ncnu-opensource/By4H6JLNW # Docker ## 虛擬機器 vs 容器 ### 虛擬機器(以作業系統為中心) ![VM](https://oer.gitlab.io/oer-on-oer-infrastructure/figures/OS/virtual-machines.png) :::info 透過選擇不同的 Guest OS,虛擬機器的技術就可以確保只要我的程式在該 Guest OS 上可以正常運作,那放到你的電腦上跑時,可以不管你的 Host OS 是什麼,只要在你的 Host OS 上先裝上我的 Guest OS,我的程式就可以正常在你的電腦上運作。 ::: ### 容器(以應用程式為中心) ![container](https://oer.gitlab.io/oer-on-oer-infrastructure/figures/OS/containers.png) :::info 容器是在作業系統層上虛擬化,透過 Container Manager 直接將一個應用程式所需的程式碼、函式庫打包,建立資源控管機制隔離各個容器,並分配 Host OS 上的系統資源。透過容器,應用程式不需要再另外安裝作業系統(Guest OS)也可以執行。 因為不需要另外安裝作業系統,建立容器所需要的硬碟容量可以大幅降低,且啟動速度可以更快,不需要等待 Guest OS 的開機時間。 ::: ## Container manager(底層實做) - rkt (appc) - **docker** (runc) - LXC/LXD ## 管理工具 - docker compose - docker swarm (stack) - k8s ## 為什麼要用 Container ### 更輕鬆的遷移和擴展 (why not baremetal) ||baremetal|container| |-|---------|---------| |遷移|人工處理|指令一鍵處理| |擴展|再來一台|指令一鍵處理| ### 更簡單的管理 (why not baremetal) |baremetal|container| |---------|---------| |無內建 |指令一鍵處理| ### 更快速的交付和部署 (wht not vm) - container只需要將你的code的部屬即可 - container不需管理跟你服務無關的東西(軟體) - container不需關心相依性 ### 更有效率的虛擬化 (why not vm) - container 只須將你的 process 、 network 虛擬化 - 其他東西與host共用 ### 對比傳統虛擬機總結 |特性||虛擬機| |-|-|-| |啟動|秒級|分鐘級| |硬碟容量|一般為 MB|一般為 GB| |效能|接近原生|比較慢| |系統支援量|單機支援上千個容器|一般幾十個| ## 各種container - 其都具備 - 容器標準化 - 具有可攜式性 - 高效地利用伺服器 - ~~透過namespace and cgroup 來分配不同軟體容器的可用硬體資源~~(有不是的) ### LXC/LXD - Linux Containers的縮寫 - 內含服務本身的程式碼,以及所需要的作業系統核心和函式庫 - 使用上可取代 VM 部分功能,但是更輕量 - [try it](https://linuxcontainers.org/lxd/try-it/) ### docker - 通常只內含服務本身的程式碼 - 通常為一次性使用 - 免洗 container ### rkt - 跟 docker 差不多 - 因應 docker 背離 CoreOS 創辦人期望而研發 - CoreOS 的期望是每個工具就專注做一項技術 - EOL - [official doc](https://coreos.com/rkt/docs/latest/rkt-vs-other-projects.html) ### kata container ![](https://katacontainers.io/static/6e497f9d3752ca1e354d0d2949abc020/8fef6/katacontainers_traditionalvskata_diagram.jpg) ## Why choose docker - docker 比較多人用 - 知名度高 - 社群支援度高 ## 實現方式與演進 - Docker一開始就是dotCloud的內部專屬專案, - Docker的開發者是: - Solomon Hykes - Andrea Luzzardi - Francois-Xavier Bourlet - Jeff Lindsay - 專案如果只用在公司內部,實在是太可惜了 - Docker成為Open Source的專案 - 透過namespace and cgroup 來分配不同軟體容器的可用硬體資源 - 0.9版前其使用lxc,之後是使用runc - 0.9版前是基於很多東西造就docker,既然來如此自己寫一個 - 支援windows ## Docker 3 + 1 元素 ### 映像檔 Image - 一個映像檔裡只包含運行該服務所需的檔案 - 建造方式: - 利用 dockerfile 建造 - 儲存執行中 container 狀態 ### 容器 Container - 用映像檔建立出來後執行 - 可以被啟動、開始、停止、刪除 - 每個容器都是相互隔離、相對安全的平台 - 包含該映像檔加上一層讀寫層 - 映像檔為唯牘 ### 倉庫 Repository - 集中存放映像檔檔案 - 倉庫註冊伺服器 (Registry) - 存放著多個倉庫 - 最大的公開倉庫註冊伺服器是 Docker Hub ### Volume - 永久存放資料的地方 ## 生命週期 ![](https://image.slidesharecdn.com/docker-architecture-v2-140906002548-phpapp02/95/docker-architecture-v13-15-638.jpg?cb=1415102222) - 資料會於 killed 時刪除 - 資料長期儲存要掛 volume ## namespace - 隔離 Process ID 命名空間 - 不同 PID 命名空間 可以擁有相同的 PID - PID 命名空間 容許 containers 提供: - 暫停 / 恢復 container 中的一組 processes - 將 container 遷移到新主機,container 的 process 保持相同的 PID - [Linux Programmer's Manual](http://man7.org/linux/man-pages/man7/pid_namespaces.7.html#top_of_page) ## Network Mode - bridge (預設使用的 mode) - Docker 會分配一個內網 ip 給 container - overlay - 不同的實體主機內的 Container 之間可以互相的溝通 - macvlan - 允許你在主機的一個網路卡上配置多個虛擬的網路卡 - container 的網路和Host 都在同一個網段中 - none - container 沒有網路 ## docker install - [docker install docs](https://docs.docker.com/engine/install/ubuntu/) - install ```shell= sudo apt-get remove docker docker-engine docker.io containerd runc curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io ``` - Verify that Docker Engine is installed correctly `sudo docker run hello-world` ## 操作映像檔 ### 一般操作 - 取得映像檔 `docker pull ubuntu:18.04` - 列出映像檔 `docker images` - 刪除 image `docker rmi lsa/nginx` ![](https://i.imgur.com/CB3ef20.png) :::info 如果沒有指定 `TAG`,預設使用 `latest` - repository: 來自於哪個倉庫,比如 ubuntu - tag: 映像檔的標記,比如 14.04 - image id: 它的 ID 號(唯一) - create: 建立時間 - size: 映像檔大小 ::: ### 建立映像檔 #### 儲存執行中 container 狀態 ```shell= sudo docker run -it ubuntu:18.04 /bin/bash touch a exit sudo docker commit -m "add file" <container name> lsa/ubuntu sudo docker run -it lsa/ubuntu /bin/bash ``` - 得到 Container 狀態(Container name) ```shell= sudo docker ps -a ``` #### 利用 dockerfile 建造 - 使用 docker commit 擴展一個映像檔比較簡單,但是不方便在一個團隊中分享 - 使用 docker build 來建立一個新的映像檔 - 需要建立一個 Dockerfile,裡面包含一些用來建立映像檔的指令 ```shell= curl https://raw.githubusercontent.com/HcwXd/docker-tutorial/master/docker-demo-app/docker.html > index.html vim Dockerfile ``` ```dockerfile= FROM nginx:latest ADD index.html /usr/share/nginx/html ``` - 使用`#`來註釋 - `FROM` 指令告訴 Docker 使用哪個映像檔作為基底 - `RUN` 開頭的指令會在建立中執行,比如安裝一個套件 - `ADD` 命令複製本地檔案到映像檔([補充](https://nickjanetakis.com/blog/docker-tip-2-the-difference-between-copy-and-add-in-a-dockerile)) - `EXPOSE` 命令向外部開放埠號 - `CMD` 描述容器啟動後自動呼叫的程序 ```shell= docker build -t="lsa/nginx" . docker run -p 8000:80 -d --name lsa lsa/nginx ``` `用瀏覽器打開主機的IP加:8000`[範例](http://localhost:8000) - `-t` 標記添加 tag - 注意一個映像檔不能超過 127 層 #### 存出和載入 - 想讓對方知道你做了什麼 ```shell= docker save -o <name>.tar <image name> docker load -i <name>.tar ``` - 單純deploy(歷史會不見) ```shell= docker export -o <name>.tar <container name> docker import <name>.tar ``` - layer image hash會對應不到(save and load)`docker image history` 原本的: ```shell= IMAGE CREATED CREATED BY SIZE COMMENT ca4502c36302 10 seconds ago /bin/sh -c #(nop) ADD file:a0c534257e73fab40… 7.48kB ed21b7a8aee9 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B ... ``` load後: ```shell= IMAGE CREATED CREATED BY SIZE COMMENT ca4502c36302 5 minutes ago /bin/sh -c #(nop) ADD file:a0c534257e73fab40… 7.48kB <missing> 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B ... ``` - export and import`docker image history` import後: ```shell= IMAGE CREATED CREATED BY SIZE COMMENT 7d6e3f6dc970 39 seconds ago 125MB Imported from - ``` ## 操作容器 ### 一般操作 - 建立容器 `docker run -it --name myubuntu -P 8000:22 ubuntu bash` - 終止 `docker stop myubuntu` - 啟動容器 `docker start -it myubuntu bash` - 重新啟動 `docker restart myubuntu` - 查看容器訊息 `docker ps -a` #### behind the scenes - 檢查本地是否存在指定的映像檔,不存在就從公有倉庫下載 - 利用映像檔建立並啟動一個容器 - **分配一個檔案系統,並在唯讀的映像檔層外面掛載一層可讀寫層** - 從宿主主機設定的網路橋界面中橋接一個虛擬埠到容器中去 - 從位址池中設定一個 ip 位址給容器 - 執行使用者指定的應用程式 - 執行完畢後容器被終止 ### 進入容器 - exec 命令 exec 是在 container 裡呼叫或執行 process `docker exec [OPTIONS] CONTAINER` - attach 命令 原本在建立一個 container 時就有包含 `-it` 就代表包含一個 tty(包含 input & output 的 terminal )只是是在背景執行,而 attach 則是把這個執行搬到前景來 `docker attach [OPTIONS] CONTAINER` - 按下 `ctrl` + `P` 然後 `ctrl` + `Q` 跳離容器,讓它繼續在背景執行。 :::warning 但是使用 attach 命令有時候並不方便。當多個窗口同時 attach 到同一個容器的時候,所有窗口都會同步顯示。當某個窗口因命令阻塞時,其他窗口也無法執行操作了。 ::: #### lab :::danger 這是為了lab測試用,平常不建議用 -itd ::: - start container `sudo docker run -idt --name lsa ubuntu` - exec `sudo docker exec -it lsa bash` - attach `sudo docker attach lsa` ## 倉庫 (Repository) - 指令查詢 `docker search [OPTIONS] TERM` - 網頁查詢 [docker hub](https://hub.docker.com) ## 資料卷 (Volume) - 資料長時間保存 - 資料卷可以在容器之間共享和重用 - 對資料卷的修改會立馬生效 - 對資料卷的更新,不會影響映像檔 :::info 資料卷的使用,類似於 Linux 下對目錄或檔案進行 mount。 ::: - 使用自建 volume ```shell= docker volume create --name <name> docker volume ls docker run -d --name web -v <name>:/webapp nginx ``` - 使用系統目錄 ```shell= docker run -d --name web -v ./log:/var/log/nginx nginx ``` ## 網路連線 - 使用 ip 直接連接 - 利用`docker inspect`來取得ip - 外部存取容器 - `-p ip:hostPort:containerPort` - 容器互聯 `--link name` - `name` 是要連接的容器名稱 ```shell= sudo docker run -d --name web --link db -p 8000:80 nginx ``` ## docker compose ```yaml= version: '3' services: db: container_name: postgres image: postgres # build: ./postgres restart: always # 自動重新使用 environment: <name>: <value> volumes: ... # depends_on: # - <some container> volumes: # 有使用到的 # networks: # default: # external: # join a pre-existing network, # name: im-pub ``` - Container name 是獨一無二的,如果不是確定其唯一性,就不用去設定 - 若要永久儲存,要設定 volumes - server name 可以直接被 ping 到 - network 設定網路 - link 用在手動起 container ### install ```shell= sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose ``` https://github.com/yiyu0x/ncnu-course-api https://github.com/vincentinttsh/docker-compose-test ## Docker swarm ### Why need it - Scaling - 自動修復 - 當 container 的 host 死後,自動於其他台重啟 - Load balancing - Rolling updates(慢慢更新) ### 優缺點( V.S. k8s ) - 優點 - 完備的相關技術文檔 - 快速簡單的配置 - 缺點 - 不提供存儲選項 - 監控不良 - Docker Swarm只提供關於容器的基本訊息 ### How - Cluster 主要由 Master Node 與 Worker Node 兩種角色所組成 - Manager Node 負責管理 Node Worker 與協調 Container 的部署工作 - 可以由一群 Manager Node 所組成 Manager Cluster(應付單點失效) - 由單數數量的 Node 節點來組成。 - 透過一種去中心化的 [Raft 驗算法](http://thesecretlivesofdata.com/raft/) - Worker Node - 付責執行 Container - 透過 Stack Network建立虛擬網路,並且透過加密的方式進行連線。 ![](https://blog.toright.com/wp-content/uploads/2017/10/swarm-diagram-1-640x300.png) ### Use it - 修改docker-compose.yml - 增加設定 `deploy` - [doc](https://docs.docker.com/compose/compose-file/#deploy) ### [play with docker](https://labs.play-with-docker.com/) ## 參考資料 [Docker 基礎教學與介紹 ](https://medium.com/unorthodox-paranoid/docker-tutorial-101-c3808b899ac6) [Docker —— 從入門到實踐](https://philipzheng.gitbook.io/docker_practice/) [CoreOS 实战:CoreOS 及管理工具介绍](https://www.infoq.cn/article/what-is-coreos/) <style> .bt { background-color: gray; color: white; font-size: 20px; font-weight: bolder; border-radius: 50% 20% / 10% 40%; } </style>

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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