[英]How to specify a docker container's network in go SDK
I'm trying to start a docker container in my app written in Golang.我正在尝试在我用 Golang 编写的应用程序中启动一个 docker 容器。
In this question , thanks for the answers, I know how to create my customized network with在这个问题中,感谢您的回答,我知道如何创建我的自定义网络
resp, err := dn.cl.NetworkCreate(context.Background(), "mynet", types.NetworkCreate{
Driver: "bridge",
IPAM: &network.IPAM{
Config: []network.IPAMConfig{{
Subnet: "128.0.1.9/16",
}},
},
})
They should be the equivalent of docker network create -d bridge --subnet=128.0.1.9/16 mynet
它们应该相当于docker network create -d bridge --subnet=128.0.1.9/16 mynet
And I'd like to use the golang SDK to do the equivalent of我想使用 golang SDK 来做相当于
docker run -d \
--name=mydocker \
--ip=128.0.1.9 \
--hostname=roach1 \
--net=mynet \
-p 1234:1234 -p 8080:8080 \
myimage/myimage-ci:latest
And I'm writing this我正在写这个
containerConfig := container.Config{
Hostname: containerName,
Image: imageName,
Env: envSetting,
ExposedPorts: nat.PortSet{"8080": struct{}{}, "1234": struct{}{}},
Cmd: cmd,
}
hostConfig := container.HostConfig{
Binds: volSetting,
PortBindings: map[nat.Port][]nat.PortBinding{
nat.Port("8080"): {{HostIP: "128.0.1.9", HostPort: "8080"}},
nat.Port("1234"): {{HostIP: "128.0.1.9", HostPort: "1234"}},
},
}
resp, err := dn.cl.ContainerCreate(
ctx,
&containerConfig,
&hostConfig,
nil,
nil,
containerName,
)
But it's not working.但它不起作用。 I got the following error:我收到以下错误:
cannot start container: Error response from daemon: Ports are not available: exposing port TCP 128.0.1.9:1234 -> 0.0.0.0:0: listen tcp 128.0.1.9:1234: bind: can't assign requested address
I also inspected the network I created我还检查了我创建的网络
[
{
"Name": "mynet",
"Id": "d009c6f68492abfa0257c2980dcfaaa425338740327ef811b209xxxxxxxxx",
"Created": "2022-08-1T16:42:14.880121815Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "128.0.1.9/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
I suspect it's because I didn't specify that the container should join the mynet
network.我怀疑这是因为我没有指定容器应该加入mynet
网络。 But how to do that with the SDK?但是如何用 SDK 做到这一点?
The HostIP
fields specify an IP address on the host machine to use for publishing ports. HostIP
字段指定主机上的 IP 地址以用于发布端口。 That has to be one of the IP addresses configured on the host system, you can't make up an address there.那必须是主机系统上配置的 IP 地址之一,你不能在那里组成一个地址。 The most common thing you'd set it to is "127.0.0.1"
, if you want a published port to be accessible from the host system from outside of Docker space but not from other hosts.如果您希望从 Docker 空间之外的主机系统可以访问已发布的端口,但不能从其他主机访问,那么您最常见的设置是"127.0.0.1"
。 This is equivalent to the occasionally-used IP address part of the docker run -p
option, like docker run -p 127.0.0.1:8080:8080
.这相当于docker run -p
选项中偶尔使用的 IP 地址部分,例如docker run -p 127.0.0.1:8080:8080
The immediate cause of your error message is that the HostIP
address isn't one configured on the host, and the easiest solution is to just delete that option (it will default to "0.0.0.0"
).错误消息的直接原因是HostIP
地址不是在主机上配置的,最简单的解决方案是删除该选项(默认为"0.0.0.0"
)。
Separately, the fourth parameter to (*Client).ContainerCreate()
is a network.NetworkingConfig
structure.另外, (*Client).ContainerCreate()
的第四个参数是一个network.NetworkingConfig
结构。 You should be able to use this to attach the container to your custom network您应该能够使用它来将容器附加到您的自定义网络
networkConfig := network.NetworkingConfig{
"mynet": &network.EndpointSettings{},
}
resp, err := dn.cl.ContainerCreate(
ctx,
&containerConfig,
&hostConfig,
&networkConfig,
nil,
containerName,
)
The EndpointSettings
includes an EndpointIPAMConfig
structure. EndpointSettings
包含一个EndpointIPAMConfig
结构。 If you needed to override Docker's setup and manually assign IP addresses here, you could.如果您需要覆盖 Docker 的设置并在此处手动分配 IP 地址,您可以。 However, since the Docker-internal IP addresses aren't especially useful, I'd let Docker pick an address for you (and, similarly, I'd skip the docker run --ip
option).但是,由于 Docker 内部的 IP 地址并不是特别有用,我会让 Docker 为您选择一个地址(同样,我会跳过docker run --ip
选项)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.