在 golang sdk 中为 ContainerCreate 函数设置 PortBindings 配置 docker api
Set PortBindings config for ContainerCreate function in golang sdk for docker api
基本上我需要这样的东西
docker run -p something:something --name xxxx imagename
在 docker api 的 golang sdk(这个 https://docs.docker.com/engine/api/sdks/)中,我当前的代码如下所示
exposedPorts, portBindings, _ := nat.ParsePortSpecs([]string{
"127.0.0.1:8080:2368",
})
// Running the ghost container
createdBody, err := dockerClient.ContainerCreate(context.Background(),
&container.Config{
Image: "ghost:latest",
ExposedPorts: exposedPorts,// it supposed to be nat.PortSet
},
&container.HostConfig{
PortBindings: portBindings,// it supposed to be nat.PortMap
},
&network.NetworkingConfig{},
containerName)
我正在使用这个 https://github.com/docker/go-connections/blob/master/nat/nat.go#L126 ParsePortSpecs 函数,它 return (map[Port]struct{}, map[Port][]PortBinding, error) 但由于container.Config.ExposedPorts 是 nat.PortSet(实际上是 map[Port]struct{} tho)而 containter.HostConfig.PortBindins 是 nat.PortMap
我不确定是否要使用此客户端 https://github.com/fsouza/go-dockerclient,因为我当前的 docker API 版本是 1.25,它不支持 API 1.23 以上的版本
Docker Client Go SDK 自 1 月以来可能发生了一些变化,但我刚刚开始使用它,所以我将在此处记录我所做的事情。
如果您需要暴露一个端口,在 docker ps
上的 PORTS 下看起来像 4140/tcp
,那么您可以执行以下操作:
config := &container.Config{
Image: "nginx",
ExposedPorts: nat.PortSet{
"4140/tcp": struct{}{},
},
}
hostConfig := &container.HostConfig{}
ctx := context.Background()
containerResp, err := Docker.ContainerCreate(ctx, config, hostConfig, nil, "")
if err != nil {
panic(err)
}
if err := Docker.ContainerStart(ctx, containerResp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
如果你想将该端口绑定到 0.0.0.0 上的主机,这看起来像 docker ps
上 PORTS 下的 0.0.0.0:4140->4140/tcp
,你需要将端口绑定添加到 hostConfig :
config := &container.Config{
Image: "nginx",
ExposedPorts: nat.PortSet{
"4140/tcp": struct{}{},
},
}
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"4140/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "4140",
},
},
},
}
ctx := context.Background()
containerResp, err := Docker.ContainerCreate(ctx, config, hostConfig, nil, "")
if err != nil {
panic(err)
}
if err := Docker.ContainerStart(ctx, containerResp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
希望这会节省一些时间:)
containerCfg := &container.Config {
Image: haproxyImage,
Tty: true,
OpenStdin: true,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: nat.PortSet{
nat.Port("443/tcp"): {},
nat.Port("10001/tcp"): {},
},
}
hostConfig := &container.HostConfig{
Binds: []string{
"/var/run/docker.sock:/var/run/docker.sock",
},
PortBindings: nat.PortMap{
nat.Port("443/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "443"}},
nat.Port("10001/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "10001"}},
},
}
基本上我需要这样的东西
docker run -p something:something --name xxxx imagename
在 docker api 的 golang sdk(这个 https://docs.docker.com/engine/api/sdks/)中,我当前的代码如下所示
exposedPorts, portBindings, _ := nat.ParsePortSpecs([]string{
"127.0.0.1:8080:2368",
})
// Running the ghost container
createdBody, err := dockerClient.ContainerCreate(context.Background(),
&container.Config{
Image: "ghost:latest",
ExposedPorts: exposedPorts,// it supposed to be nat.PortSet
},
&container.HostConfig{
PortBindings: portBindings,// it supposed to be nat.PortMap
},
&network.NetworkingConfig{},
containerName)
我正在使用这个 https://github.com/docker/go-connections/blob/master/nat/nat.go#L126 ParsePortSpecs 函数,它 return (map[Port]struct{}, map[Port][]PortBinding, error) 但由于container.Config.ExposedPorts 是 nat.PortSet(实际上是 map[Port]struct{} tho)而 containter.HostConfig.PortBindins 是 nat.PortMap
我不确定是否要使用此客户端 https://github.com/fsouza/go-dockerclient,因为我当前的 docker API 版本是 1.25,它不支持 API 1.23 以上的版本
Docker Client Go SDK 自 1 月以来可能发生了一些变化,但我刚刚开始使用它,所以我将在此处记录我所做的事情。
如果您需要暴露一个端口,在 docker ps
上的 PORTS 下看起来像 4140/tcp
,那么您可以执行以下操作:
config := &container.Config{
Image: "nginx",
ExposedPorts: nat.PortSet{
"4140/tcp": struct{}{},
},
}
hostConfig := &container.HostConfig{}
ctx := context.Background()
containerResp, err := Docker.ContainerCreate(ctx, config, hostConfig, nil, "")
if err != nil {
panic(err)
}
if err := Docker.ContainerStart(ctx, containerResp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
如果你想将该端口绑定到 0.0.0.0 上的主机,这看起来像 docker ps
上 PORTS 下的 0.0.0.0:4140->4140/tcp
,你需要将端口绑定添加到 hostConfig :
config := &container.Config{
Image: "nginx",
ExposedPorts: nat.PortSet{
"4140/tcp": struct{}{},
},
}
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"4140/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "4140",
},
},
},
}
ctx := context.Background()
containerResp, err := Docker.ContainerCreate(ctx, config, hostConfig, nil, "")
if err != nil {
panic(err)
}
if err := Docker.ContainerStart(ctx, containerResp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
希望这会节省一些时间:)
containerCfg := &container.Config {
Image: haproxyImage,
Tty: true,
OpenStdin: true,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: nat.PortSet{
nat.Port("443/tcp"): {},
nat.Port("10001/tcp"): {},
},
}
hostConfig := &container.HostConfig{
Binds: []string{
"/var/run/docker.sock:/var/run/docker.sock",
},
PortBindings: nat.PortMap{
nat.Port("443/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "443"}},
nat.Port("10001/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "10001"}},
},
}