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