ContainerStart is the Docker client operation that turns an already created container into a running container. It is the execution step in a container lifecycle: pull an image, create a configured container, start it, then inspect logs or later stop and remove it. Using the Docker Go SDK keeps HTTP methods, status codes, and request serialization behind the client interface. section 3.2 section 3.5
Starting is distinct from creating. ContainerCreate assembles a container from image, environment, resource, restart-policy, port, and related configuration, then returns a response containing its ID. ContainerStart receives that existing ID, so creation must succeed before a start request can be issued. section 3.4 section 3.4
A minimal start path has four responsibilities:
- Create a context for the Docker client call. section 3.4
- Create the container and retain the returned response and ID. section 3.4
- Call ContainerStart with the context, response ID, and start options; empty options are sufficient when no start-specific behavior is needed. section 3.4
- Check its sole error result and return a failure result immediately; on success, record the container ID and report a successful start. section 3.4 section 3.4
Starting a newly created container
After ContainerCreate returns resp, start the container with err = d.Client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}). If err is non-nil, log and return it. Otherwise, save resp.ID as the runtime container ID. A following ContainerLogs call can copy standard output and error to the host, while the result records Action "start" and Result "success". section 3.4 section 3.4
In an orchestration workflow, this operation makes a task actually run after its configuration has been translated into a container. It provides the same basic capability used beneath command-line container execution, while leaving higher-level components to choose work, track state, and coordinate machines. section 3.4 section 3.5
Listing 3.12 The penultimate phase
func (d *Docker) Run() DockerResult {
// previous code not listed
resp, err := d.Client.ContainerCreate(ctx, &cc, &hc, nil, nil,
d.Config.Name)
if err != nil {
log.Printf("Error creating container using image %s: %v\n",
d.Config.Image, err)
return DockerResult{Error: err}
}
err = d.Client.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
log.Printf("Error starting container %s: %v\n", resp.ID, err)
return DockerResult{Error: err}
}
The invocation shows the boundary between creation and execution: the creation response supplies the ID, and a failed start returns immediately. section 3.4
