How do I use map[string]struct{}

huangapple go评论100阅读模式
英文:

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": { "/<tcp|udp>: {}" }"

查看我正在使用的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 := &amp;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 :

 &quot;ExposedPorts&quot;:{
             &quot;22/tcp&quot;: {}
     }

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": {},
	},
}

Playground

英文:

Put this in your containerConfig

ExposedPorts: map[string]struct{}{
	&quot;22/tcp&quot;: {},
}

Eg

containerConfig := &amp;dockerclient.ContainerConfig{
    Image:        imageName,
    AttachStdin:  true,
    AttachStdout: true,
    AttachStderr: true,
	ExposedPorts: map[string]struct{}{
		&quot;22/tcp&quot;: {},
	},
}

Playground

huangapple
  • 本文由 发表于 2014年12月2日 20:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/27248931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定