普通网友 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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用