liveBook · Manning

ContainerStart

As explained in Build and Orchestrator in Go (From Scratch)

ContainerStart is a Docker client API operation that starts an existing container by identifier.

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

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Explains the call’s context, container ID, empty options, error result, and post-start bookkeeping without detailing Docker Engine internals.
Examples
Demonstrates starting a created PostgreSQL container, saving its ID, reading logs, and later stopping and removing it.
What it assumes
Requires a Docker client, context, and an existing container ID returned by ContainerCreate.
What you can do after
Apply it via the Docker Go SDK to start a created container and handle startup failure.
How it connects
Connects ContainerStart with ContainerCreate, ImagePull, ContainerLogs, ContainerStop, task, and Docker SDK.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.