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
    # 六角鼠年鐵人賽 Week 6 - 使用 DockerFile 建置 Tomcat + MySQL 環境 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ### 人月圓 山中書事 張可久 :::info 興亡千古繁華夢,詩眼倦天涯。 孔林喬木,吳宮蔓草,楚廟寒鴉。 數間茅舍,藏書萬卷,投老村家。 山中何事?松花釀酒,春水煎茶。 ::: 這篇將會簡單編寫兩個建置各包著 **Tomcat** 與 **MySQL** 軟體環境的 DockerFile 出來 **Tomcat** 是放置 Java 開發網頁包 War 的 Apache,**MySQL** 則是家喻戶曉的 DB 軟體。 ## 實際建置 DockerFile for Tomcat ### 準備 用 touch 的指令建立一個空白的 DockerFile,並準備一個測試用的 war 包在同一個位置。 新建一個資料夾來放置這些檔案,建置的時候,若沒有下 ignore 參數,則 Docker 會將所有同在資料夾底下的物件全部進行打包建置,這樣會造成 Image 徒增不必要的容量。盡可能避免不必要的檔案被建置進來。 ### DockerFile 內容 #### 檔案名稱: tomcat-8.0_DockerFile ``` # from tomcat:8.0 FROM tomcat:8.0 # set Maintainer MAINTAINER Kai # copy war COPY test.war /usr/local/tomcat/webapps/ # run CMD ["/usr/local/tomcat/bin/catalina.sh","run"] ``` ### 執行 Docker build ``` docker build -t tomcat8_demo -f tomcat-8.0_DockerFile . --no-cache ``` ![](https://i.imgur.com/Bbb2Ybq.png) 如果繼承的 Image 是 Local 沒有的話,那便會花幾分鐘或是數十分鐘從網路上下載 (依照 Image 大小),建置好後可以在 Image List 中看到我們所建置的新的 Image **tomcat8_demo** ![](https://i.imgur.com/59Lr9m1.png) ### 簡單執行並檢視成果 ``` docker run -d -p 8080:8080 tomcat8_demo ``` ![](https://i.imgur.com/EGM7BnT.png) ![](https://i.imgur.com/raqKCsj.png) ### 雜談 **Windows** 的版本因 Docker-machine IP 在 192.168.99.100 故對此才能連通運行中的 Container 而 **Linux** 環境可以直接使用 172.17.0.2:8080 看到成果 ## 實際建置 DockerFile for MySQL ### 準備 建置一個給 MySQL DockerFile 的資料夾 (e.m. mysql_dockerFile),並在裏頭建立一個 sql_scripts 資料夾,後續我們要存放兩個 SQL 檔案。 建立 SQL 檔案 createTable.sql ```sql CREATE TABLE items ( item_no int, item_name varchar(30), item_class_no int, item_price int ); ``` 建立 SQL 檔案 insertData.sql ```sql insert into items values(1001,'candy',1,10); insert into items values(1002,'cookie',1,5); insert into items values(1003,'sugar',1,100); insert into items values(2001,'cake',2,500); insert into items values(3001,'sandwitch',3,150); ``` ### DockerFile 內容 #### 檔案名稱: mysql_DockerFile ``` $ cat mysql_DockerFile # set From FROM mysql:5.7 # add a DB ENV MYSQL_DATABASE store # add the path sql_scripts/ to your image, the scripts in this folder will execute automatically COPY ./sql_scripts/ /docker-entrypoint-initdb.d/ ``` ### 執行 Docker build ``` docker build -t mysql_demo -f mysql_DockerFile . --no-cache ``` ![](https://i.imgur.com/hg4ii6G.png) ![](https://i.imgur.com/H9P0iGl.png) ### 簡單執行並檢視成果 ``` docker run -d -p 3306:3306 --name mysql_demo -e MYSQL_ROOT_PASSWORD=supersecret mysql_demo ``` ![](https://i.imgur.com/jE3wOEC.png) ``` # 進入 container docker exec -it mysql_demo bash # 進入 mysql mysql -u root -p # 輸入密碼 (輸入過程不會看到任何字元) 但請打正確後按 Enter supersecret # 進入後下指令秀出所有 DB show databases; 可以看到我們剛才用 scripts 建立的 store,其餘都是預設 DB # 離開 mysql exit # 離開 container exit ``` ![](https://i.imgur.com/LUcQGFc.png) ## 結語 :::danger PS1. 關於 DockerFile 的文檔,如果檔名就是 DockerFile 則可以省略掉 -f [DockerFile Name] 這一項指令,只是因為是教學,希望可以藉機讓大家知道更多關於設定上的大小事,未來如果還有發現其他細節,我會再一一補充回來~ PS2. 關於 Tomcat 環境的建立可以自己將準備好的 jdk 和 tomcat 包放在與 DockerFile 同一資料夾內,在 build 的時候進行 JDK 與 tomcat 的解壓縮與布署,以此更彈性化去客製個人產品環境。 下篇將會分享更多關於 **Container** 的操作與學習心得 [六角鼠年鐵人賽 Week 7 - Docker Container 操作指令](/llU_a500RHO3dHPH1rhwQw) ::: 首頁 [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