为Docker API的golang SDK中的ContainerCreate函数设置PortBindings配置。

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

Set PortBindings config for ContainerCreate function in golang sdk for docker api

问题

基本上,我需要像这样的东西

docker run -p something:something --name xxxx imagename

在golang sdk中实现(这个sdk是https://docs.docker.com/engine/api/sdks/),用于docker api,我的当前代码如下

exposedPorts, portBindings, _ := nat.ParsePortSpecs([]string{
    "127.0.0.1:8080:2368",
})
// 运行ghost容器
createdBody, err := dockerClient.ContainerCreate(context.Background(),
    &container.Config{
        Image:        "ghost:latest",
        ExposedPorts: exposedPorts, // 应该是nat.PortSet
    },
    &container.HostConfig{
        PortBindings: portBindings, // 应该是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{}),而container.HostConfig.PortBindings是nat.PortMap

我不确定是否要使用https://github.com/fsouza/go-dockerclient这个客户端,因为我的当前docker API版本是1.25,不支持1.23以上的API版本。

英文:

Basically i need to something like this

docker run -p something:something --name xxxx imagename

in golang sdk (this one https://docs.docker.com/engine/api/sdks/) for docker api, my current code looks like this

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)

I'm using this https://github.com/docker/go-connections/blob/master/nat/nat.go#L126 ParsePortSpecs function which return (map[Port]struct{}, map[Port][]PortBinding, error) but fail since the container.Config.ExposedPorts is nat.PortSet (it's actually map[Port]struct{} tho) and containter.HostConfig.PortBindins is nat.PortMap

I'm not sure if i want to use this client https://github.com/fsouza/go-dockerclient since my current version of docker API is 1.25 and it doesn't support API version above 1.23

答案1

得分: 29

Docker Client Go SDK 可能在一月份以后有所改变,但我刚刚让它工作了,所以我会在这里记录我所做的事情。

如果你需要暴露一个端口,就像在 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)
}

希望这能为某人节省一些时间 为Docker API的golang SDK中的ContainerCreate函数设置PortBindings配置。

英文:

The Docker Client Go SDK might have changed a bit since Jan, but I just got this working so I'll document what I did here.

If you need a port exposed, which would look like 4140/tcp under PORTS on docker ps then you can do the following:

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)
}

If you'd like to bind that port to the host on 0.0.0.0, which would look like 0.0.0.0:4140->4140/tcp under PORTS on docker ps you need to add in the port bindings to the 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)
}

Hopefully this'll save somebody some time 为Docker API的golang SDK中的ContainerCreate函数设置PortBindings配置。

答案2

得分: 5

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

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

huangapple
  • 本文由 发表于 2017年1月22日 16:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/41789083.html
匿名

发表评论

匿名网友

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

确定