liveBook · Manning

Context

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

A Go context carries request-scoped values, deadlines, and cancellation signals across API and process boundaries.

A Go context carries values across API and process boundaries and commonly carries request deadlines or cancellation signals. A task runner creates one with `context.Background()` and passes it to Docker client calls. section 3.4

The same context accompanies image pulling, container creation, startup, log retrieval, stopping, and removal. This makes the context an explicit argument at the boundary where the task runner asks the Docker client to act. section 3.4

Use an empty context for a container operation

`ctx := context.Background()` supplies a context to `ImagePull`; the runner then handles an error or copies the pull output to standard output. section 3.4

Listing 3.9 The start of our Run() method
func (d *Docker) Run() DockerResult {
   ctx := context.Background()
   reader, err := d.Client.ImagePull(
       ctx, d.Config.Image, types.ImagePullOptions{})
   if err != nil {
       log.Printf("Error pulling image %s: %v\n", d.Config.Image, err)
       return DockerResult{Error: err}
   }
   io.Copy(os.Stdout, reader)
}

Shows a minimal `Run` method creating a background context and passing it to `ImagePull`. section 3.4

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

How deep it goes
Defines context and demonstrates `context.Background()` passed through Docker calls, without implementing deadlines, cancellation, or values.
Examples
Uses `context.Background()` for image pulling, container lifecycle calls, and a PostgreSQL container task.
What it assumes
Assumes Go and Docker client calls while introducing context as a boundary-crossing argument.
What you can do after
Apply it via the Docker client by creating a background context and supplying it to container API calls.
How it connects
Connects context to Docker clients, container creation, image pulling, logging, deadlines, and cancellation signals.

Also explained in

Compare how 7 books explain context →

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.