基本上我需要像这样的东西
docker run -p something:something --name xxxx imagename
在 Golang SDK(即此处链接 https://docs.docker.com/engine/api/sdks/)中使用 Docker API 的当前代码如下:
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函数,它返回(map[Port]struct{},map[Port][]PortBinding,error),但是因为container.Config.ExposedPorts是nat.PortSet(实际上是map[Port]struct{}),而containter.HostConfig.PortBindings是nat.PortMap,所以失败了。
我不确定是否要使用这个客户端https://github.com/fsouza/go-dockerclient,因为我的当前版本的Docker API是1.25,不支持1.23以上的API版本。
config:=&container.Config { Image:“nginx”, ExposedPorts:nat.PortSet { nat.Port(“80:tcp”):struct {} {}, }, }
会抛出错误cannot use (nat.PortSet literal) (value of type nat.PortSet) as nat.PortSet value in struct literal
。 - ZacSketches