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

时间:2019-02-15 00:05:53

标签: docker go

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

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

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

nil

很明显,我缺少了某物,但是我看不到那是什么。因为我指定了网络模式,我是否需要网络配置({{1}的ContainerCreate参数)?

1 个答案:

答案 0 :(得分:2)

我在发布问题时就抓住了我的问题,所以我将分享它,因为在任何文档中都没有明确指出。使用host网络模式时,您的容器配置中不应包含主机名。

更改此内容

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

...对此:

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

花了全部时间。

相关问题