普通网友 2017-01-22 08:57
浏览 332
已采纳

在golang sdk中为docker api的ContainerCreate函数设置PortBindings配置

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

  • 写回答

2条回答 默认 最新

  • douci1918 2017-07-27 23:23
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题