From 6edec2626c7621e0f9c631dd080b5cbd1ea42834 Mon Sep 17 00:00:00 2001
From: llody_55 <745719408@qq.com>
Date: Fri, 18 Oct 2024 13:38:36 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=95=9C=E5=83=8F=E7=89=88?=
=?UTF-8?q?=E6=9C=AC=E5=B1=95=E7=A4=BA=E5=86=85=E5=AE=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.github/workflows/docker-image.yml | 6 ++--
apps/views.py | 29 +++++++++++++++++--
.../container/docker_container_list.html | 15 +++++++++-
3 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml
index 9646f91..91392d7 100644
--- a/.github/workflows/docker-image.yml
+++ b/.github/workflows/docker-image.yml
@@ -11,7 +11,7 @@ permissions:
env:
PYTHON_VERSION: '3.9.13'
- RELEASE_VERSION: 'v1.1.4' # 发布版本
+ RELEASE_VERSION: 'v1.1.5' # 发布版本
DOCKER_IMAGE_NAME: 'udocker'
DOCKER_NAMESPACE: 'llody'
DOCKER_REGISTRY_HUAWEI: 'swr.cn-southwest-2.myhuaweicloud.com'
@@ -116,8 +116,8 @@ jobs:
body: |
- **新版本发布**: ${{ env.RELEASE_VERSION }}
- **更新内容**:
- - * 容器管理 - 新增镜像回滚功能,可以对容器正在使用的镜像进行镜像回滚,切换镜像并重新创建容器。
- - * 镜像仓库 - 新增dockerhub代理,(docker.llody.cn)。
+ - * 容器管理 - 修复镜像管理占用判断逻辑。
+ - * 镜像仓库 - 新增compose应用判断。
- * 页面优化。
- * 流水线优化 - 新增同步国内镜像仓库镜像。
draft: false
diff --git a/apps/views.py b/apps/views.py
index 85f51f5..d83c2e5 100644
--- a/apps/views.py
+++ b/apps/views.py
@@ -205,6 +205,13 @@ def docker_container_api(request):
isrunning = container.attrs['State']['Running']
status = container.status
ports_data = []
+
+ # 检查容器是否是通过docker-compose部署的
+ labels = container.attrs.get('Config', {}).get('Labels', {})
+ is_docker_compose = 'com.docker.compose.project' in labels
+ compose_project = labels.get('com.docker.compose.project', 'N/A') if is_docker_compose else None
+ compose_service = labels.get('com.docker.compose.service', 'N/A') if is_docker_compose else None
+
for i in container.attrs['NetworkSettings']['Ports']:
ports = i
#该变量用于获取是否有未映射端口,没有为false,有为true
@@ -246,7 +253,21 @@ def docker_container_api(request):
time_obj = parser.isoparse(time_str)
time_obj += time_offset
create_time = time_obj.strftime('%Y-%m-%d %H:%M:%S')
- dat = {"id":id,"name":name,"image":image,"isrunning":isrunning,"restart_switch":restart_switch,"restart_count":restart_count,"status":status,"create_time":create_time,"ports_data":ports_data,"health_status":health_status}
+ dat = {
+ "id": id,
+ "name": name,
+ "image": image,
+ "isrunning": isrunning,
+ "restart_switch": restart_switch,
+ "restart_count": restart_count,
+ "status": status,
+ "create_time": create_time,
+ "ports_data": ports_data,
+ "health_status": health_status,
+ "is_docker_compose": is_docker_compose,
+ "compose_project": compose_project,
+ "compose_service": compose_service
+ }
# 根据查询关键字返回数据
if search_key:
if search_key in name:
@@ -852,14 +873,14 @@ def docker_images_api(request):
# 获取所有容器的镜像ID
container_image_ids = {container.image.id for container in containers}
def process_image(image):
+ image_ids = image.id # 使用完整的image id
image_id = image.short_id
image_tags = image.tags
image_size = humanize.naturalsize(image.attrs['Size'], binary=True)
time_str = image.attrs['Created']
time_obj = parser.isoparse(time_str)
image_create_time = time_obj.strftime('%Y-%m-%d %H:%M:%S')
- image_in_use = image_id in container_image_ids
-
+ image_in_use = image_ids in container_image_ids
for tag in image_tags:
dat = {
"image_id": image_id,
@@ -957,6 +978,8 @@ def process_image(image):
code = 2
if e.status_code == 409 and "image has dependent child images" in str(e):
msg = "删除失败:存在多个依赖的子镜像。"
+ elif e.status_code == 409 and "image is being used by running container" in str(e):
+ msg = "删除失败:容器正在运行,请先停止容器。"
else:
msg = "删除报错:%s" % e
result = {'code': code, 'msg': msg}
diff --git a/templates/container/docker_container_list.html b/templates/container/docker_container_list.html
index acc38f9..422a158 100644
--- a/templates/container/docker_container_list.html
+++ b/templates/container/docker_container_list.html
@@ -128,6 +128,7 @@
{ type: 'checkbox', fixed: 'left' } //全选按钮
, { field: 'id', title: translations.containerId, sort: true ,hide:true}
, { field: 'name', title: translations.containerName +'', fieldTitle: '容器名称', hide: 0, sort: true, edit: 'text' } //hide:true 隐藏某一列的信息,edit:'text' 表示开启了编辑
+ , { field: 'compose_project', title:"compose", sort: true,templet: composeFormat}
, { field: 'image', title: translations.containerImage, sort: true}
//, { field: 'isrunning', title: '就绪状态', width: 100, templet: isrunningFormat }
, { field: 'health_status', title: translations.healthCheck +'', width: 110, templet: health_statusFormat }
@@ -159,7 +160,19 @@
})
}
});
-
+
+ // 健康状态格式化
+ function composeFormat(d) {
+ result = "";
+ if (d.is_docker_compose == true) {
+ //给返回值加个样式
+ result = d.compose_project
+ return result
+ } else {
+ result = '-'
+ return result
+ }
+ }
// 健康状态格式化
function health_statusFormat(d) {
result = "";