容器化開發環境 - 如何斷點調試容器中的應用

上一篇容器化開發環境已經簡單的介紹瞭如何容器化開發環境。在開發過程當中,不少時候都須要經過斷點的方式來調試程序或定位BUG,那麼如何斷點調試運行在容器中的應用呢???java

容器就像一個虛擬機,就像一個遠程的服務器,要斷點調試容器中的應用,就像要斷點調試虛擬機和遠程的服務器上的應用同樣。如何配置一個能夠遠程斷點調試的應用,每一個語言,每一個框架都有不一樣的方式,接下來介紹的例子使用的技術棧以下:git

接下來會基於上一篇博客中的DEMO hello-dockerize 進行擴展。


  • clone code repodocker

    $ git clone https://github.com/aikin/hello-dockerize.git
  • 更新build.gradle文件,在文件中寫入apply plugin: "application"瀏覽器

    /*
      remote debug setup
     */
    applicationDefaultJvmArgs = [
            "-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n"
    ]
  • 更新docker-compose.yml文件bash

    web:
      build: ./dockerize
      environment:
        - LANG=C.UTF-8
        - TERM=xterm
        - DEBUG=true
      ports:
        - "8080:8080"
        - "5005:5005"
      volumes:
        - .:/hello-dockerize
  • 啓動容器服務器

    $ docker-compose run --service-ports web
  • 配置 IntelliJ IDEAapp

    • 打開工具欄Run --> Edit Configurations

    config

    • 添加新配置 點擊加號,選擇Remote,將 Host localhost 修改成docker-machine ip machine-name獲取的IP,在/etc/hosts文件中綁定的本地域名dockerhost

    add

    • 在容器中啓動服務:

    root@b73c06ebe59f:/hello-dockerize# ./gradlew bootRun --debug-jvm --stacktrace
    • 啓動 IntelliJ IDEA 的 debugger 模式

      • 點擊debug按鈕

    debugger

    • 打開瀏覽器訪問http://dockerhost:8080/hello-dockerize,若是你也看到下面的畫面,恭喜你成功啦。

    debugging  


  
學會斷點調試容器中的應用,不再用爲我那烏黑亮麗的頭髮着急啦!!!

原文連接

相關文章
相關標籤/搜索