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

Basically I need something like this

docker run -p something:something --name xxxx imagename 

in golang sdk (this https://docs.docker.com/engine/api/sdks/ ) for docker api, my current code looks like

 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 use this function https://github.com/docker/go-connections/blob/master/nat/nat.go#L126 ParsePortSpecs, which returns (map [Port] struct {}, map [Port] [] PortBinding, error), but the failure due to the .Config.ExposedPorts container is nat.PortSet (it actually displays [Port] struct {} tho) and contains .HostConfig.PortBindins - nat.PortMap

I am 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

+5
source share
2 answers

The Docker Client Go SDK may change a bit from January, but I just got this working, so I will document what I did here.

If you need an open port that looks like 4140/tcp in the PORTS on docker ps section, 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 want to associate this port with a host on 0.0.0.0, which will look like 0.0.0.0:4140->4140/tcp in the PORTS section on docker ps , you need to add port bindings to 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) } 

Hope this saves a little time :)

+7
source
 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"}}, }, } 
0
source

Source: https://habr.com/ru/post/1263159/


All Articles