Golang Docker SDK: How to Rename a Container

To modify a container's name, you can utilize the ContainerRename method within the Docker SDK. Here's an example code snippet:

package main

import (
    "context"
    "fmt"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
)

func main() {
    ctx := context.Background()
    cli, err := client.NewEnvClient()
    if err != nil {
        panic(err)
    }

    oldName := 'old_container_name'
    newName := 'new_container_name'

    err = cli.ContainerRename(ctx, oldName, newName)
    if err != nil {
        panic(err)
    }

    containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
    if err != nil {
        panic(err)
    }

    for _, container := range containers {
        fmt.Println(container.Names)
    }
}

In the code above, we first establish a client instance. Next, we define the old container name and the new container name. We then employ the ContainerRename method to change the container name from the old name to the new one. Finally, we list all container names to verify that the renaming operation was successful.

Golang Docker SDK: How to Rename a Container