dockerContainersList.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <link rel="stylesheet" href="../script/semantic/semantic.min.css" />
  6. <script src="../script/jquery-3.6.0.min.js"></script>
  7. <script src="../script/semantic/semantic.min.js"></script>
  8. </head>
  9. <body>
  10. <br />
  11. <div class="ui container">
  12. <div class="ui header">
  13. <div class="content">
  14. List of Docker Containers
  15. <div class="sub header">
  16. Below is a list of all detected Docker containers currently running
  17. on the system.
  18. </div>
  19. </div>
  20. </div>
  21. <div id="containersList" class="ui middle aligned divided list active">
  22. <div class="ui loader active"></div>
  23. </div>
  24. <div class="ui horizontal divider"></div>
  25. <div id="containersAddedListHeader" class="ui header" hidden>
  26. Already added containers:
  27. </div>
  28. <div
  29. id="containersAddedList"
  30. class="ui middle aligned divided list"
  31. ></div>
  32. </div>
  33. <script>
  34. const lines = {};
  35. const linesAded = [];
  36. function getDockerContainers() {
  37. const hostRequest = $.get("/api/proxy/list?type=host");
  38. const dockerRequest = $.get("/api/docker/containers");
  39. // Wait for both requests to complete
  40. Promise.all([hostRequest, dockerRequest])
  41. .then(([hostData, dockerData]) => {
  42. if (
  43. dockerData.error === undefined &&
  44. hostData.error === undefined
  45. ) {
  46. const { containers, network } = dockerData;
  47. const bridge = network.find(({ Name }) => Name === "bridge");
  48. const {
  49. IPAM: {
  50. Config: [{ Gateway: gateway }],
  51. },
  52. } = bridge;
  53. const existedDomains = hostData.map(({ Domain }) => Domain);
  54. for (const container of containers) {
  55. const {
  56. Ports,
  57. Names: [name],
  58. } = container;
  59. for (const portObject of Ports.filter(
  60. ({ IP: ip }) => ip === "::"
  61. )) {
  62. const { IP: ip, PublicPort: port } = portObject;
  63. const key = `${name}-${port}`;
  64. if (
  65. existedDomains.some((item) => item === `${gateway}:${port}`)
  66. ) {
  67. linesAded.push({
  68. name: name.replace(/^\//, ""),
  69. ip: gateway,
  70. port,
  71. });
  72. } else if (!lines[key]) {
  73. lines[key] = {
  74. name: name.replace(/^\//, ""),
  75. ip: gateway,
  76. port,
  77. };
  78. }
  79. }
  80. }
  81. for (const [key, line] of Object.entries(lines)) {
  82. $("#containersList").append(
  83. `<div class="item">
  84. <div class="right floated content">
  85. <div class="ui button" onclick="addContainerItem('${key}');">Add</div>
  86. </div>
  87. <div class="content">
  88. <div class="header">${line.name}</div>
  89. <div class="description">
  90. ${line.ip}:${line.port}
  91. </div>
  92. </div>`
  93. );
  94. }
  95. for (const line of linesAded) {
  96. $("#containersAddedList").append(
  97. `<div class="item">
  98. <div class="content">
  99. <div class="header">${line.name}</div>
  100. <div class="description">
  101. ${line.ip}:${line.port}
  102. </div>
  103. </div>`
  104. );
  105. }
  106. linesAded.length &&
  107. $("#containersAddedListHeader").removeAttr("hidden");
  108. $("#containersList .loader").removeClass("active");
  109. } else {
  110. parent.msgbox(
  111. `Error loading data: ${dockerData.error || hostData.error}`,
  112. false
  113. );
  114. }
  115. })
  116. .catch((error) => {
  117. parent.msgbox("Error loading data: " + error.message, false);
  118. });
  119. }
  120. getDockerContainers();
  121. function addContainerItem(item) {
  122. if (lines[item]) {
  123. parent.addContainerItem(lines[item]);
  124. }
  125. }
  126. </script>
  127. </body>
  128. </html>