2017-02-23 31 views
2

我想將Spring Boot應用程序配置到Windows Docker容器中,該容器託管在Windows Docker主機上(這是在Mac上虛擬化的,但這是另一回事;))和Ansible。我已經成功使用Ansible Windows Modulesprovision a Spring Boot app to Windows如何做一個健康檢查運行在Windows Docker Windows容器與Ansible的Spring Boot應用程序?

我在最後一章,只是想在最後加上健康檢查。如無泊塢博客帖子概述這很簡單:

- name: Wait until our Spring Boot app is up & running 
    win_uri: 
     url: "http://localhost:8080/health" 
     method: GET 
    register: result 
    until: result.status_code == 200 
    retries: 5 
    delay: 5 
與泊塢窗口的容器 there´s a known limitation讓您不容使用 localhost現在

現在。我們必須使用Windows Docker Containers內部Hyper-V IP地址(在NetworkSettings.Networks.nat.IPAddress的JSON輸出中運行docker inspect <yourContainerIdHere>後,您可以看到IP)。

我的問題是:我如何才能獲得Windows多克爾Container's Hyper-V的內部IP地址,其輸出調試語句中,做一個健康檢查類似我介紹的一個?

回答

2

經過漫長的旅程才做了簡單的健康檢查,我找到了解決方案。

首先,我們要獲得多克爾Container's IP地址,可以在一個PowerShell可以輕鬆完成這個命令:

docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' <yourInstanceIdHere> 

我們只需要使用win_shell module

但是因爲這個使用Docker模板機制,所以Jinja2模板不知道,它不應該這次解釋這些命令。我們必須正確地擺脫大括號,這在this so q&a already中列出。您可以使用建議的解決方案之一:

"{%raw%}"{{ .NetworkSettings.Networks.nat.IPAddress }}"{%endraw%}"

"{{'{{'}} .NetworkSettings.Networks.nat.IPAddress {{'}}'}}" - 都將爲我們在這裏工作。

現在獲得從這個輸出的IP地址,我想剛剛註冊的結果,使健康檢查。可悲的是這個doesn't工作,因爲返回stdoutstdout_lines確實包含您的IP,同時也再次泊塢窗模板 - 但這次沒有逃逸序列,這將反過來讓任務this so answer失敗(如評論從Davide Guerri已經報道)。

lanwen這樣的評論給了一個建議救援:我們能管的第一win_shell輸出到一個臨時文本文件container_ip.txt,然後 - 在第二win_shell任務 - 我們剛纔讀的文件的內容,並註冊一個輸出 - 變量。

這似乎很容易,我們再次使用win_shell

win_shell: cat container_ip.txt 
register: win_shell_txt_return 

但是,嘿,that's並不是故事的全部 - >因爲在Windows中,有nice carriage return line feeds :),這將\r\n污染我們的IP地址最終會讓我們的健康檢查再次失敗。

但同樣,there's幫助:Ansible有一個很好的splitlines功能(這是稍微未記錄的...)我們只是有一個尾隨[0]做到這一點,以獲得IP:

"{{ win_shell_txt_return.stdout.splitlines()[0] }}" 

現在我們能夠做我們的健康檢查,因爲我們想要擺在首位。這是完整的解決方案:

- name: Obtain the Docker Container´s internal IP address (because localhost doesn´t work for now https://github.com/docker/for-win/issues/458) 
    win_shell: "docker inspect -f {% raw %}'{{ .NetworkSettings.Networks.nat.IPAddress }}' {% endraw %} {{spring_boot_app_name}} {{ '>' }} container_ip.txt" 

    - name: Get the Docker Container´s internal IP address from the temporary txt-file (we have to do this because of templating problems, see https://stackoverflow.com/a/32279729/4964553) 
    win_shell: cat container_ip.txt 
    register: win_shell_txt_return 

    - name: Define the IP as variable 
    set_fact: 
     docker_container_ip: "{{ win_shell_txt_return.stdout.splitlines()[0] }}" 

    - debug: 
     msg: "Your Docker Container has the internal IP {{ docker_container_ip }} --> Let´s do a health-check against this URI: 'http://{{ docker_container_ip }}:{{spring_boot_app.port}}/{{spring_boot_app.health_url_ending}}'" 

    - name: Wait until our Spring Boot app is up & running 
    win_uri: 
     url: "http://{{ docker_container_ip }}:8080/health" 
     method: GET 
    register: health_result 
    until: health_result.status_code == 200 
    retries: 5 
    delay: 5 
相關問題