Created
June 18, 2019 08:57
-
-
Save frikky/e2efcea6c733ea8d8d015b7fe8a91bf6 to your computer and use it in GitHub Desktop.
Stop and remove a docker container with Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"fmt" | |
"context" | |
"github.com/docker/docker/api/types" | |
"github.com/docker/docker/client" | |
) | |
// Stop and remove a container | |
func stopAndRemoveContainer(client *client.Client, containername string) error { | |
ctx := context.Background() | |
if err := client.ContainerStop(ctx, containername, nil); err != nil { | |
log.Printf("Unable to stop container %s: %s", containername, err) | |
} | |
removeOptions := types.ContainerRemoveOptions{ | |
RemoveVolumes: true, | |
Force: true, | |
} | |
if err := client.ContainerRemove(ctx, containername, removeOptions); err != nil { | |
log.Printf("Unable to remove container: %s", err) | |
return err | |
} | |
return nil | |
} | |
func main() { | |
client, err := client.NewEnvClient() | |
if err != nil { | |
fmt.Printf("Unable to create docker client: %s", err) | |
} | |
// Stops and removes a container | |
stopAndRemoveContainer(client, "containername") | |
} |
Thank you for sharing!
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot,
Really useful !