51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gofiber/fiber/v2/log"
|
|
)
|
|
|
|
const (
|
|
// WechatBotLabelKey 微信机器人容器标签键
|
|
WechatBotLabelKey = "app.type"
|
|
// WechatBotLabelValue 微信机器人容器标签值
|
|
WechatBotLabelValue = "wechat-bot"
|
|
)
|
|
|
|
// CreateRobotContainer 创建微信机器人容器
|
|
func CreateRobotContainer(ctx context.Context, robotName string, port int) (string, string, error) {
|
|
// 创建容器标签
|
|
labels := map[string]string{
|
|
WechatBotLabelKey: WechatBotLabelValue,
|
|
"robot.name": robotName,
|
|
}
|
|
|
|
// 创建环境变量
|
|
env := []string{
|
|
"BOT_NAME=" + robotName,
|
|
}
|
|
|
|
// 创建容器
|
|
containerID, err := CreateContainer(ctx, "wechat-bot-"+robotName, env, labels, port)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
// 启动容器
|
|
err = StartContainer(ctx, containerID)
|
|
if err != nil {
|
|
// 如果启动失败,尝试删除容器
|
|
_ = RemoveContainer(ctx, containerID, true)
|
|
return "", "", err
|
|
}
|
|
|
|
// 获取容器访问地址
|
|
containerHost, err := GetContainerHost(ctx, containerID)
|
|
if err != nil {
|
|
log.Warnf("警告: 无法获取容器访问地址: %v", err)
|
|
containerHost = "localhost:9000" // 使用默认值
|
|
}
|
|
|
|
return containerID, containerHost, nil
|
|
}
|