MaDi
    • 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 New
    • 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 Note Insights Versions and GitHub Sync 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Docker基本概念與操作 ###### tags: `DevOps` 白話來說虛擬化要解決的問題就是: >「我寫好了一支程式,在我的電腦上可以正常運作,但搬到你的電腦上可能就爆掉」。 ## Docker三大概念 1. **映像檔(Image)**: **是一個模板,用來重複產生容器實體** `(做蛋糕的模具)` 2. **容器(Container)**: **映像檔建立出來的執行實體**,它可以被啟動、開始、停止、刪除。每個容器都是相互隔離、保證安全的平台。 `(模具烤出來的蛋糕)` 3. **倉庫(Repository)**: **集中存放映像檔的場所** `(存模具的收納櫃)` 例如:一個映像檔裡可以包含一個完整的 MySQL 服務、一個 Golang 的編譯環境、或是一個 Ubuntu 作業系統。 * 透過 Docker 映像檔,我們可以快速的產生可以執行應用程式的容器。而 **Docker 映像檔可以透過撰寫由命令行構成的 `Dockerfile` 輕鬆建立**,或甚至可以從公開的地方下載已經做好的映像檔來使用。 * 另外要注意的是,**Docker 映像檔是唯讀(read-only)的,而容器在啟動的時候會建立一層可以被修改的可寫層作為最上層,讓容器的功能可以再擴充** * 最大的公開倉庫註冊伺服器是上面提到過的 [Docker Hub](https://hub.docker.com/),存放了數量龐大的映像檔供使用者下載。而 Docker 倉庫註冊伺服器的概念就跟 Github 類似,你可以在上面建立多個倉庫,然後透過 push、pull 的方式上傳、存取。 ### 映像檔操作 1. 取得映像檔: ``` $ docker pull NAME[:TAG] // NAME: 映像檔名稱 // TAG: 標籤,預設是latest ``` 舉例來說,下載ubuntu這個Images ![](https://i.imgur.com/TDGiHQ4.png) * 未明確指明TAG時,預設會是latest(最新版) * 映像檔一般由數個資料層(layer)組成,行首的亂碼代表各層的ID Docker Desktop裏頭的Images會多出剛剛下載好的ubuntu ![](https://i.imgur.com/Eavb6Cd.png) 2. 執行映像檔: ``` $ docker run -d -p 4000:80 docker/getting-started // -d: (daemonized)以背景狀態執行 // -p 參數將 Docker 內部應用程式的 80 連接埠對應到 host 機器上的 4000 連接埠 // -t: 終端機 // -i: 交互式使用,可以透過終端機來輸入命令 ``` 3. 查看映像檔資訊: ``` $ docker images ``` ![](https://i.imgur.com/3ulumFx.png) 取得映像檔的詳細資訊,回傳一大包 json ``` $ docker inspect 映像檔 ``` 獲取映像檔的某資訊,ex: 取得 Architecture 資訊 ``` $ docker inspect ubuntu -f {{".Architecture"}} ``` 4. 搜尋映像檔: ``` $ docker search 映像檔 ``` * 搜尋遠端倉庫中共用的映像檔,預設是 Docker Hub * 預設的輸出結果是按照評價星星數量進行排序 搜尋星評10以上的映像檔 ``` $ docker search -s 10 映像檔 ``` 5. 刪除映像檔: ``` $ docker rmi IMAGE // IMAGE: 標籤名稱 or 映像檔ID(只輸入前幾碼也行) ``` * 以標籤(tag)方式刪除,則只會刪掉該tag的映像檔,當某個映像檔有多個tag的時候,映像檔本身不受影響 * 以ID方式刪除,會先刪除所有指向該映像檔的標籤,然後刪除該映像檔的本身 * 當有該映像檔建立的容器仍存在時,映像檔預設是無法被刪除的 6. 基於**現有映像檔產生的容器**來建立映像檔: ``` $ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] // OPTIONS: -a 作者資訊 | -m commit資訊 | -p commit時暫停容器 ``` 舉例來說,啟動busybox映像檔,並在其中新增一個test檔,再建立一個新的映像檔 ``` $ docker run -ti busybox / # touch test / # exit ``` 查看該容器的id ``` $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c50500b3328b busybox "sh" 5 minutes ago Exited (0) 4 minutes ago xenodochial_ganguly ``` 以此建立一個新的映像檔 ``` $ docker commit -m "Added a new file" -a "madi" c50500 test sha256:ac065fb5e74ebcc1ec02b55b236c04e6f32a352fc3d7919d582b8956c3ce6471 ``` ![](https://i.imgur.com/2K74xAn.png) 7. 上傳映像檔 ``` $ docker push NAME[:TAG] ``` 舉例來說,將剛剛新建立好的映像檔加上tag,通常是 **username/映像檔名稱**,**username是Docker Hub上的ID**,需要先從 [這裡](https://hub.docker.com/) 註冊,再透過 `docker login`輸入帳密登入,接著就可以上傳映像檔到自己的倉庫 ``` $ docker tag test:latest dysonma/test:latest ``` ``` $ docker push dysonma/test:latest The push refers to repository [docker.io/dysonma/test] ac85325b787a: Pushed 2983725f2649: Pushed latest: digest: sha256:fbd9f149b62834f1426f0c25275d160bb0dd2eb74f762f99e80e0d9313f82831 size: 734 ``` 連到 Docker Hub網站上,進入My Profile,看到剛建立的映像檔已經上傳成功 ![](https://i.imgur.com/R1XoOSE.png) ### 容器操作 1. 建立容器 ``` $ docker create -it 映像檔 ``` 2. 查看本機上存在的所有容器 ``` $ docker ps -a ``` 3. 停止容器 ``` $ docker stop 容器 ``` 4. 啟動容器 ``` $ docker start 容器 ``` 5. 刪除容器 ``` $ docker rm 容器 ```

    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