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

