Docker 容器网络主机模式可以通过 CLI 运行,但不能通过 Go SDK 运行

sub*_*ris 3 go docker

运行 Docker 18.09.1、API 1.39,并尝试将容器的网络置于主机模式,以便蓝牙正常工作。当我从 CLI 启动容器时,一切都运行良好:

docker run --rm --name mycontainer --net=host imageName my-command
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 Go API 启动此容器时,网络似乎设置不正确,导致我的容器死机。

config := &container.Config{
    Cmd:      []string{"my-command"},
    Hostname: "mycontainer",
    Image:    imageName,
}

hostConfig := &container.HostConfig{
    AutoRemove: true,
    NetworkMode: "host",
}


container, err := cli.ContainerCreate(*ctx, config, hostConfig, nil, "mycontainer")
Run Code Online (Sandbox Code Playgroud)

很明显我错过了一些东西,但我看不到那是什么。由于我指定了网络模式,我是否需要网络配置(nil的参数)?ContainerCreate

sub*_*ris 5

我在准备发布问题时发现了我的问题,因此我将分享它,因为任何文档中都没有明确说明这一点。使用host网络模式时,您的容器配置不应包含主机名。

改变这个:

config := &container.Config{
    Cmd:      []string{"my-command"},
    Hostname: "mycontainer",
    Image:    imageName,
}
Run Code Online (Sandbox Code Playgroud)

...对此:

config := &container.Config{
    Cmd:      []string{"my-command"},
    Image:    imageName,
}
Run Code Online (Sandbox Code Playgroud)

就这样了。