Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set PortBindings config for ContainerCreate function in golang sdk for docker api

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

like image 940
Adit Avatar asked Jan 22 '17 08:01

Adit


2 Answers

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

like image 65
Tom Mertz Avatar answered Nov 14 '22 11:11

Tom Mertz


containerCfg := &container.Config {
    Image: haproxyImage,
    Tty: true,
    OpenStdin: true,
    AttachStdout: true,
    AttachStderr: true,
    ExposedPorts: nat.PortSet{
        nat.Port("443/tcp"): {},
        nat.Port("10001/tcp"): {},
    },
}

hostConfig := &container.HostConfig{
    Binds: []string{
        "/var/run/docker.sock:/var/run/docker.sock",
    },
    PortBindings: nat.PortMap{
        nat.Port("443/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "443"}},
        nat.Port("10001/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: "10001"}},
    },
}
like image 36
Tamil Avatar answered Nov 14 '22 11:11

Tamil