🆕 update

This commit is contained in:
李寻欢 2025-03-27 20:04:49 +08:00
parent 12df2cec94
commit d550420af8
4 changed files with 44 additions and 22 deletions

View File

@ -8,12 +8,16 @@ database:
type: "sqlite"
dbname: "./debug/dev.db"
docker:
host: "unix:///var/run/docker.sock"
apiVersion: "1.41"
imageName: "wechat-bot:latest"
imageName: "lxh01/xybotv2:latest"
network: "bridge"
volumePath: "./data/dev-bots"
redis:
host: "10.0.0.31"
password: "pGhQKwj7DE7FbFL1"
db: 2
auth:
secretKey: "dev-secret-key"

View File

@ -31,9 +31,12 @@ database:
docker:
host: "unix:///var/run/docker.sock"
apiVersion: "1.41"
imageName: "wechat-bot:latest"
imageName: "lxh01/xybotv2:latest"
network: "bridge"
volumePath: "./data/wechat-bots"
redis:
host: "10.0.0.31"
password: "pGhQKwj7DE7FbFL1"
db: 2
auth:
secretKey: "your-secret-key-change-me"

View File

@ -4,13 +4,21 @@ import (
"fmt"
)
// RedisConfig 是Redis相关配置
type RedisConfig struct {
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
// DockerConfig Docker配置
type DockerConfig struct {
Host string `mapstructure:"host"` // Docker daemon 主机地址
APIVersion string `mapstructure:"apiVersion"` // Docker API版本
ImageName string `mapstructure:"imageName"` // 微信机器人Docker镜像名称
Network string `mapstructure:"network"` // 容器网络
VolumePath string `mapstructure:"volumePath"` // 容器数据卷路径
Host string `mapstructure:"host"` // Docker daemon 主机地址
APIVersion string `mapstructure:"apiVersion"` // Docker API版本
ImageName string `mapstructure:"imageName"` // 微信机器人Docker镜像名称
Network string `mapstructure:"network"` // 容器网络
VolumePath string `mapstructure:"volumePath"` // 容器数据卷路径
Redis RedisConfig `mapstructure:"redis"` // Redis配置
}
// Validate 验证Docker配置

View File

@ -9,7 +9,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
@ -30,18 +29,27 @@ type ContainerInfo struct {
func CreateContainer(ctx context.Context, cfg *config.DockerConfig, name string, env []string, labels map[string]string) (string, error) {
cli := GetClient()
// 端口映射
portBindings := nat.PortMap{}
exposedPorts := nat.PortSet{}
// 端口映射 - 将容器的9000端口映射到主机的端口
hostPort := nat.Port("9000/tcp")
exposedPorts := nat.PortSet{
hostPort: struct{}{},
}
// 创建挂载点
var mounts []mount.Mount
if cfg.VolumePath != "" {
mounts = append(mounts, mount.Mount{
Type: mount.TypeBind,
Source: fmt.Sprintf("%s/%s", cfg.VolumePath, name),
Target: "/data",
})
// 设置主机端口映射
portBindings := nat.PortMap{
hostPort: []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "9001", // 主机端口可配置
},
},
}
// 添加Redis环境变量
if cfg.Redis.Host != "" {
env = append(env, fmt.Sprintf("REDIS_HOST=%s", cfg.Redis.Host))
env = append(env, fmt.Sprintf("REDIS_PASSWORD=%s", cfg.Redis.Password))
env = append(env, fmt.Sprintf("REDIS_DB=%d", cfg.Redis.DB))
}
// 设置容器配置
@ -55,7 +63,6 @@ func CreateContainer(ctx context.Context, cfg *config.DockerConfig, name string,
// 设置主机配置
hostConfig := &container.HostConfig{
PortBindings: portBindings,
Mounts: mounts,
RestartPolicy: container.RestartPolicy{
Name: "unless-stopped",
},