Kai Chen
    • 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
    # 六角鼠年鐵人賽 Week 7 - Docker Container 操作指令 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ### 望海潮 洛陽懷古 秦觀 :::info 梅英疏淡,冰澌溶泄,東風暗換年華。金谷俊遊,銅駝巷陌,新晴細履平沙。長記誤隨車。正絮翻蝶舞,芳思交加。柳下桃蹊,亂分春色到人家。 西園夜飲鳴笳。有華燈礙月,飛蓋妨花。蘭苑未空,行人漸老,重來是事堪嗟。煙暝酒旗斜。但倚樓極目,時見棲鴉。無奈歸心,暗隨流水到天涯。 ::: 進入重頭戲,前幾篇文章針對 Image、DockerFile 都講了很多設定與操作上的種種。 這篇就來告訴大家,關於 Container 的操作以及一些小技巧~ ## 列出所有 Container ``` docker ps -a ``` ![](https://i.imgur.com/eA6DUsD.png) :::info 列出關於 ID、Image、建立日期和 **狀態** 等資訊。 其中最重要的狀態,可以讓開發者知道目前 Container 是否在運行 ::: ## 使用 Image 建置並啟動 Container ``` docker run [Image Name] ``` ![](https://i.imgur.com/I9yBFVr.png) :::info 使用上述指令後,Docker 會自動建立一個新的 Container,並運行這個 Container (狀態) 在使用這個指令的同時還有許多額外的參數可以帶入,詳細可參考 [官方說明](https://docs.docker.com/engine/reference/commandline/run/) 以下列出幾個比較常用的參數 |參數|說明| |---|---| |--add-host|增加一個host對應IP的設定| |-d|會在背景執行 Container,並秀出 Container ID| |-p|指定與主機關聯的 port,e.g. -p 8080:8080| |-P|開啟所有 Image 設定的 port| |--rm|當有來自相同 Image 的 Container,會刪除既存在的(舊的),執行新的| ::: ## 進入 Container ``` docker exec -it [Container ID] /bin/bash docker exec -it [Container ID] /bash ``` ![](https://i.imgur.com/6zDz7Ie.png) :::info 兩個指令皆可以進入 Container。 ::: ## 查看 Container IP ``` (已進入 Container 中) cat /etc/hosts ``` ![](https://i.imgur.com/4XNWqM0.png) :::info Docker Container 的 default IP 是 172.17.0.2 但如果是使用 Windows 開的開發者們要注意,你運行的 Container,如果要從本機端連線的話,要使用 Docker Machine 的 IP 位置去連。 ::: ## 查看 Docker Machine IP ``` docker-machine ip ``` ![](https://i.imgur.com/ZPU2jyh.png) :::info 使用此句可以查詢到目前 Docker Machine 是使用哪一個虛擬 IP ::: ## 啟用/停止 Container ``` docker start [Container ID] docker stop [Container ID] ``` ![](https://i.imgur.com/GBZ3y3P.png) :::info start 啟用 stop 停止 非常好記的兩個指令,其中 start 部分不需要再去加入 IP 等參數,這些設定會在第一次建立 Container 的時候設定保存在 Container 中 ::: ## 重啟 Container ``` docker restart [Container ID] ``` ![](https://i.imgur.com/5YmtDO4.png) :::info 有了啟動和停止,當然會有重啟,指令一樣很好記 ::: ## 刪除 Container ``` docker rm [Container ID] docker rm --force [Container] docker rm $(docker ps -a -q) ``` ![](https://i.imgur.com/j5mKGPa.png) :::info 第一句語法可以刪除個別的 Container 第二句語法可以強制刪除正在運行的 Container (非常不建議) 第三句語法可以刪除那些狀態是 stop 的 Containers ::: ## 殺死 Container ``` docker kill --signal [Container ID] ``` ![](https://i.imgur.com/4aR60Na.png) :::info 如字面意義上的說法,rm 會把 Container 刪除,但 kill 僅是殺死,Container 本身還在,但會變得無法運行,可以透過 rm 方式刪除掉 ::: ## 儲存 Container 變動為新的 Image ``` docker commit container [Container ID] [New Image Name:TAG] ``` :::info 這個指令可以保存現有的 Container,並直接轉為儲存一個新的 Image ::: ## 查看 Container 資訊 ``` docker inspect [Container ID] docker stats [Container ID] or nothing docker port [Container ID] docker ps -a docker logs [Container ID] ``` :::info 因為操作多種功能就不一一列出截圖了~ 第一句會列出所有底層資訊,是開發人員常用的指令,來去確認 Container 的設定無誤 第二句會列出目前所有或單獨的 Container 的CPU、記憶體及網路使用量 第三句會秀出目前 Container 設定與主機連線的 port 第四句也是很常用的,列出所有 Container 與顯示其基本狀態資訊 第五句會秀出該 Container 內部的 Logs 資訊 ::: ## 結語 :::danger 未來如果有學到更多操作,會再更新回來~ 下篇將會分享更多關於 **Docker 效能調整與匯入匯出功能** 的學習心得 [六角鼠年鐵人賽 Week 8 - Docker 容器效能調整與Image匯入匯出](/SOPt184HT7qE6SaQa8KHnQ) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Docker`,`w3HexSchool`

    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