Skip to content

Commit 61bede2

Browse files
committed
fix: 解决容器端口冲突导致创建失败的问题
1 parent 1a6c45c commit 61bede2

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

backend/app/service/container.go

+35-7
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,15 @@ func (u *ContainerService) ContainerUpdate(req dto.ContainerOperate) error {
432432
hostConf := oldContainer.HostConfig
433433
var networkConf network.NetworkingConfig
434434
if err := loadConfigInfo(req, config, hostConf, &networkConf); err != nil {
435+
reCreateAfterUpdate(req.Name, client, oldContainer.Config, oldContainer.HostConfig, oldContainer.NetworkSettings)
435436
return err
436437
}
437438

438439
global.LOG.Infof("new container info %s has been update, now start to recreate", req.Name)
439440
container, err := client.ContainerCreate(ctx, config, hostConf, &networkConf, &v1.Platform{}, req.Name)
440441
if err != nil {
441-
return fmt.Errorf("recreate contianer failed, err: %v", err)
442+
reCreateAfterUpdate(req.Name, client, oldContainer.Config, oldContainer.HostConfig, oldContainer.NetworkSettings)
443+
return fmt.Errorf("update contianer failed, err: %v", err)
442444
}
443445
global.LOG.Infof("update container %s successful! now check if the container is started.", req.Name)
444446
if err := client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
@@ -481,13 +483,14 @@ func (u *ContainerService) ContainerUpgrade(req dto.ContainerUpgrade) error {
481483
}
482484

483485
global.LOG.Infof("new container info %s has been update, now start to recreate", req.Name)
484-
container, err := client.ContainerCreate(ctx, config, hostConf, &network.NetworkingConfig{}, &v1.Platform{}, req.Name)
486+
container, err := client.ContainerCreate(ctx, config, hostConf, &networkConf, &v1.Platform{}, req.Name)
485487
if err != nil {
486-
return fmt.Errorf("recreate contianer failed, err: %v", err)
488+
reCreateAfterUpdate(req.Name, client, oldContainer.Config, oldContainer.HostConfig, oldContainer.NetworkSettings)
489+
return fmt.Errorf("upgrade contianer failed, err: %v", err)
487490
}
488-
global.LOG.Infof("update container %s successful! now check if the container is started.", req.Name)
491+
global.LOG.Infof("upgrade container %s successful! now check if the container is started.", req.Name)
489492
if err := client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
490-
return fmt.Errorf("update successful but start failed, err: %v", err)
493+
return fmt.Errorf("upgrade successful but start failed, err: %v", err)
491494
}
492495

493496
return nil
@@ -781,6 +784,12 @@ func checkPortStats(ports []dto.PortHelper) (nat.PortMap, error) {
781784
}
782785

783786
func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf *container.HostConfig, networkConf *network.NetworkingConfig) error {
787+
if len(req.Network) != 0 {
788+
networkConf.EndpointsConfig = map[string]*network.EndpointSettings{req.Network: {}}
789+
} else {
790+
networkConf = &network.NetworkingConfig{}
791+
}
792+
784793
portMap, err := checkPortStats(req.ExposedPorts)
785794
if err != nil {
786795
return err
@@ -795,8 +804,6 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf
795804
config.Labels = stringsToMap(req.Labels)
796805
config.ExposedPorts = exposeds
797806

798-
networkConf.EndpointsConfig = map[string]*network.EndpointSettings{req.Network: {}}
799-
800807
hostConf.AutoRemove = req.AutoRemove
801808
hostConf.CPUShares = req.CPUShares
802809
hostConf.PublishAllPorts = req.PublishAllPorts
@@ -815,3 +822,24 @@ func loadConfigInfo(req dto.ContainerOperate, config *container.Config, hostConf
815822
}
816823
return nil
817824
}
825+
826+
func reCreateAfterUpdate(name string, client *client.Client, config *container.Config, hostConf *container.HostConfig, networkConf *types.NetworkSettings) {
827+
ctx := context.Background()
828+
829+
var oldNetworkConf network.NetworkingConfig
830+
if networkConf != nil {
831+
for networkKey := range networkConf.Networks {
832+
oldNetworkConf.EndpointsConfig = map[string]*network.EndpointSettings{networkKey: {}}
833+
break
834+
}
835+
}
836+
837+
oldContainer, err := client.ContainerCreate(ctx, config, hostConf, &oldNetworkConf, &v1.Platform{}, name)
838+
if err != nil {
839+
global.LOG.Errorf("recreate after container update failed, err: %v", err)
840+
return
841+
}
842+
if err := client.ContainerStart(ctx, oldContainer.ID, types.ContainerStartOptions{}); err != nil {
843+
global.LOG.Errorf("restart after container update failed, err: %v", err)
844+
}
845+
}

frontend/src/views/container/container/index.vue

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ const onOpenDialog = async (
297297
rowData: Partial<Container.ContainerHelper> = {
298298
cmd: [],
299299
cmdStr: '',
300+
publishAllPorts: false,
300301
exposedPorts: [],
301302
cpuShares: 1024,
302303
nanoCPUs: 0,

frontend/src/views/container/container/operate/index.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
<el-input clearable v-model.trim="dialogData.rowData!.name" />
1818
</el-form-item>
1919
<el-form-item :label="$t('container.image')" prop="image">
20-
<el-select class="widthClass" allow-create filterable v-model="dialogData.rowData!.image">
20+
<el-select
21+
class="widthClass"
22+
:placeholder="$t('commons.msg.inputOrSelect')"
23+
allow-create
24+
filterable
25+
v-model="dialogData.rowData!.image"
26+
>
2127
<el-option
2228
v-for="(item, index) of images"
2329
:key="index"
@@ -159,7 +165,7 @@
159165
v-for="(item, indexV) of volumes"
160166
:key="indexV"
161167
:value="item.option"
162-
:label="item.option"
168+
:label="item.option.substring(0, 12)"
163169
/>
164170
</el-select>
165171
</td>
@@ -301,13 +307,13 @@ const limits = ref<Container.ResourceLimit>({
301307
});
302308
303309
const handleClose = () => {
310+
emit('search');
304311
drawerVisiable.value = false;
305312
};
306313
307314
const rules = reactive({
308315
name: [Rules.requiredInput, Rules.volumeName],
309316
image: [Rules.requiredSelect],
310-
network: [Rules.requiredSelect],
311317
cpuShares: [Rules.integerNumberWith0, checkNumberRange(0, 262144)],
312318
nanoCPUs: [Rules.floatNumber],
313319
memory: [Rules.floatNumber],

frontend/src/views/container/image/index.vue

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
</template>
4242
<template #main>
4343
<ComplexTable :pagination-config="paginationConfig" :data="data" @search="search">
44-
<el-table-column label="ID" prop="id" min-width="60">
44+
<el-table-column label="ID" prop="id" min-width="50">
4545
<template #default="{ row }">
46-
<Tooltip :islink="false" :text="row.id" />
46+
<span>{{ row.id.replaceAll('sha256:', '').substring(0, 12) }}</span>
4747
</template>
4848
</el-table-column>
4949
<el-table-column :label="$t('container.tag')" prop="tags" min-width="160" fix>
@@ -82,7 +82,6 @@
8282
</template>
8383

8484
<script lang="ts" setup>
85-
import Tooltip from '@/components/tooltip/index.vue';
8685
import TableSetting from '@/components/table-setting/index.vue';
8786
import { reactive, onMounted, ref } from 'vue';
8887
import { dateFormat } from '@/utils/util';

0 commit comments

Comments
 (0)