liveBook · Manning

Docker stop

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

ContainerStop is a Docker client API operation that stops a running container by identifier.

docker stop is the operational action of stopping a running container. At the command line, stopping changes the container state from running to exited; the container remains available for inspection unless it is removed separately. section 3.1

In the Docker Go SDK, ContainerStop sends Docker Engine a request to stop a running container. It lets orchestration code control container lifetime without handling HTTP methods, status codes, or request serialization directly. section 3.2

Listing 3.14 Stopping a container
func (d *Docker) Stop(id string) DockerResult {
   log.Printf("Attempting to stop container %v", id)
   ctx := context.Background()
   err := d.Client.ContainerStop(ctx, id, nil)
   if err != nil {
       log.Printf("Error stopping container %s: %v\n", id, err)
       return DockerResult{Error: err}
   }

   err = d.Client.ContainerRemove(ctx, id, types.ContainerRemoveOptions{
       RemoveVolumes: true,
       RemoveLinks:   false,
       Force:         false,
   })
   if err != nil {
       log.Printf("Error removing container %s: %v\n", id, err)
       return DockerResult{Error: err}
   }

   return DockerResult{Action: "stop", Result: "success", Error: nil}
}

A small Stop method makes the lifecycle explicit: create a context, stop by container ID, check the returned error, then optionally remove the stopped container with chosen removal options. section 3.4

A practical stop path separates two operations:

  • Call ContainerStop with the context and container identifier; return an error result if the call fails. section 3.4
  • Call ContainerRemove only when cleanup is intended; removal options can remove volumes while leaving links and avoiding forced removal. section 3.4
  • Return a result that records the stop action and success, so callers can report or react to the outcome. section 3.4

Stop a short-lived Postgres task

Create and run a Postgres container, retain the ContainerId returned by the run operation, wait briefly, and pass that ID to Stop. The wrapper stops the container and removes it after successful shutdown; command-line inspection shows that stopping alone leaves a container in the exited state. section 3.4 section 3.1

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

How deep it goes
Implements a Go Stop wrapper with context, error returns, removal options, and result reporting, while deferring SDK HTTP details.
Examples
Uses a Postgres container, docker inspect state output, and Go helpers that create, wait, stop, and remove a container.
What it assumes
Assumes a Docker client and container identifier while shielding callers from low-level HTTP request handling.
What you can do after
Apply it via the Docker Go SDK to stop a container by ID and optionally remove it after successful shutdown.
How it connects
Connects ContainerStop to ContainerCreate, ContainerStart, ContainerRemove, Docker Engine, and task orchestration.

Also explained in

Compare how 2 books explain docker stop →

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.