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 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 Compose ## 【前言】 前面已經學過了如何自己建立一個 Docker File,並透過 Docker File 建立 Docker Image,最後利用 Image 建立多個 Container 的流程。 但每一個服務都要輸入指令一個一個啟動的話,對於負責應用程式布署的人來說也太勞心勞力了,對此 Docker 提供了一個可以一次執行大量 Docker Container 啟用的工具 -- Docker Compose 來幫助開發人員進行多服務的布署。 ## 【Docker Compose】 Docker Compose 是 Docker 推出的一項服務套件,意在於幫助開發者們去整頓需要操作的 Container 指令,並將其檔案化,使其如同 shell 檔一樣的方式存在。 屆時開發者們可以僅透過執行該檔案,便可輕鬆完成多應用的布署,且文檔化的好處是更容易編輯、可以進入 GIT 的版控,攜帶性提升等等。 ## 【安裝】 若你是 Windows 的朋友們,在安裝了 Windows 版本的 Docker 後理當來說不需要再去安裝 Docker Compose 的部分。 但是其他環境的朋友們就需要額外去安裝這個套件了~ [Docker Official Install Page](https://docs.docker.com/compose/install/) ## 【docker-compose.yml】 這是基本 Docker Compose 執行的當下會去呼叫的檔案,不需要再執行時候特別告知檔名,若有改名稱的話則會需要多一些指令幫助 Docker Compose 知道要執行的是哪一支檔案。 Kai 這邊就把在之前啟動過的 TOMCAT 和 MYSQL 指令都寫在 docker-compose.yml 檔案中,讓他一次性幫我啟動兩個服務。 ![](https://i.imgur.com/oTscERQ.png) 由於啟動時忘記設定背景執行... 因此只好再開一個 Terminal 檢視成果,可以看到目前有兩個 Containers 已經執行了!! ![](https://i.imgur.com/9UhTtGA.png) 確認目前運行的 Docker Compose 狀況 ![](https://i.imgur.com/scaISGK.png) ## 【指令】 終於來到這個套件的指令部份了~ 在下方 Kai 會列出常用的指令與其執行動作意義 | Command | 動作 | 附註 | | ---- | ---- | ---- | | docker-compose up | 執行 Docker Compose 套件 | 預設讀取 docker-compose.yml | | docker-compose down | 停止正在運行的 Docker Compose 啟動的 Containers 和 Networks || | docker-compose [Commands] -d | 背景執行命令 || | docker-compose ps | 檢視運行中的 Docker Compose 啟動的 Containers 和 Networks || | docker-compose start | 類同 up 指令 | 差別在於 up 會建立 container 但 start 若無找到該 container 的話不會自動建立 container| | docker-compose stop | 類同 down 指令 | 差別在於 down 會刪除 container 但 stop 不會執行刪除動作| | docker-compose rm | 執行刪除沒有運行的 containers | 必須是經由 docker-compose.yml 建立的才會刪去 | | docker-compose pull | 會將需要的 images 下載至本地端 | | | docker-compose push | 會將建立的 images 上傳至 Remote Repository | | | docker-compose restart | 重新啟動該 docker-compose.yml 的所有 containers | | 其餘還有很多可執行的命令,詳見[Docker Compose 官網](https://docs.docker.com/engine/reference/commandline/compose/) ## 【.env 檔案支援】 docker-compose.yml 作為一支整理預備執行的多個 services 的檔案而言應該偏向於整體架構的檔案,因此必要的我們會把一些特殊需要放入的參數提出。 畢竟同樣的環境建置,開發者可能需要一個開發環境的參數和 product 環境的參數的調控,如果一直都在 docker-compose.yml 上處理的話會改動得十分頻繁且容易出錯。 因此最好的方式便是把這些參數抽出,而 Docker Compose 本身就支援呼叫一種稱作 **.env** 的檔案,使用者可以把參數提出後統一放置在這種檔案內,分門別類的。 後續在使用上,我們就可以針對開發環境或 product 環境直接作選擇即可,就不用將參數改來改去了~ 使用的方式非常類似 **JSTL** **.env** ``` ## for Dev TAG_DB_URL=192.168.0.1 ``` **docker-compose.yml** ``` ... environments: DB_URL:${TAG_DB_URL} ``` 若要在執行時選擇不同的環境檔案注入的話,只需要在啟動時告知 Docker Compose 使用的環境檔案即可。 ``` docker-compose --env-file [File Location] up ``` ## 【結語】 到了這邊終於差不多把 Docker 的部分學齊了~ 可喜可賀~ 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Docker`

    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