简体   繁体   English

如何在 go SDK 中指定 docker 容器的网络

[英]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.

相关问题 Docker EC2中的容器无法通过AWS将数据写入AWS S3 SDK - Docker container in EC2 is unable to write data into AWS S3 through AWS SDK 如何从单个 docker 容器与多个 S3 存储桶进行交互? - How to interact with multiple S3 bucket from a single docker container? 无法使用 Go Docker 引擎 ZDB974238714CA81DE634A7C2Z 的容器在 Docker 中安装文件夹 - Unable to mount a folder in Docker from container with Go Docker Engine API 将 Go 服务器作为 Docker 容器运行时出现权限被拒绝错误 - Permission denied error when running Go server as Docker container 如何使用 docker 容器按要求扩展? - How to scale on request with docker container? 如何处理 Go 的 firebase admin sdk 错误? - How to handle errors with firebase admin sdk for Go? GitLab Docker-in-Docker:作业容器中的 Docker 客户端如何发现“dind”服务容器中的 Docker 守护进程? - GitLab Docker-in-Docker: how does Docker client in job container discover Docker daemon in `dind` service container? PHP AWS S3 SDK 重试 on.network 连接错误 - PHP AWS S3 SDK retry on network connections errors 如何通过 AWS SDK 为 Go 检索 ARN - How to retrieve ARN through AWS SDK for Go 如何在 docker 容器的系统日志中保存来自 Gitlab CI 的命令 output - How to save command output from Gitlab CI in docker container's syslog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM