英文:
How do I use map[string]struct{}
问题
所以我正在忙着使用http://godoc.org/github.com/samalba/dockerclient
使用CreateContainer(http://godoc.org/github.com/samalba/dockerclient#DockerClient.CreateContainer)
来设置一个新的容器
containerConfig := &dockerclient.ContainerConfig{
Image: imageName,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true}
containerID, err = docker.CreateContainer(containerConfig, containerName)
工作正常,我得到了一个容器,但是没有暴露的端口。查看docker API(https://docs.docker.com/reference/api/docker_remote_api_v1.15/),我需要设置
"ExposedPorts - 将端口映射到空对象的对象,形式为:"ExposedPorts": { "
查看我正在使用的Go dockclient库的godoc,我看到可以将其传递为
ExposedPorts map[string]struct{}
但是我不知道在这里该怎么做,从docker api示例中传递:
"ExposedPorts":{
"22/tcp": {}
}
就足够了,那么我该如何在我的containerConfig中进行结构设置?
英文:
So I am busy using http://godoc.org/github.com/samalba/dockerclient
Using CreateContainer (http://godoc.org/github.com/samalba/dockerclient#DockerClient.CreateContainer)
To setup a new container
containerConfig := &dockerclient.ContainerConfig{
Image: imageName,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true}
containerID, err = docker.CreateContainer(containerConfig, containerName)
works correctly and I get a container, however, there is no exposed ports. Looking at the docker API(https://docs.docker.com/reference/api/docker_remote_api_v1.15/), I need to set
"ExposedPorts - An object mapping ports to an empty object in the form of: "ExposedPorts": { "<port>/<tcp|udp>: {}" }"
Looking at the godoc for the Go dockclient lib I am using, I see you can pass it as
ExposedPorts map[string]struct{}
But I have no idea on what to do here, from the docker api example passing :
"ExposedPorts":{
"22/tcp": {}
}
is enough, so how do I do the struct bit in the my containerConfig?
答案1
得分: 2
将以下内容放入你的containerConfig中:
ExposedPorts: map[string]struct{}{
"22/tcp": {},
}
例如:
containerConfig := &dockerclient.ContainerConfig{
Image: imageName,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: map[string]struct{}{
"22/tcp": {},
},
}
英文:
Put this in your containerConfig
ExposedPorts: map[string]struct{}{
"22/tcp": {},
}
Eg
containerConfig := &dockerclient.ContainerConfig{
Image: imageName,
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
ExposedPorts: map[string]struct{}{
"22/tcp": {},
},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论