MCPcopy Index your code
hub / github.com/ddev/ddev / Cleanup

Function Cleanup

pkg/ddevapp/utils.go:95–146  ·  view source on GitHub ↗

Cleanup will remove DDEV containers and volumes even if docker-compose.yml has been deleted.

(app *DdevApp)

Source from the content-addressed store, hash-verified

93// Cleanup will remove DDEV containers and volumes even if docker-compose.yml
94// has been deleted.
95func Cleanup(app *DdevApp) error {
96 ctx, apiClient, err := dockerutil.GetDockerClient()
97 if err != nil {
98 return err
99 }
100
101 // Find all containers which match the current site name.
102 labels := map[string]string{
103 "com.ddev.site-name": app.GetName(),
104 }
105
106 // remove project network
107 // "docker-compose down" - removes project network and any left-overs
108 // There can be awkward cases where we're doing an app.Stop() but the rendered
109 // yaml does not exist, all in testing situations.
110 if fileutil.FileExists(app.DockerComposeFullRenderedYAMLPath()) {
111 _, _, err := dockerutil.ComposeCmd(&dockerutil.ComposeCmdOpts{
112 ComposeFiles: []string{app.DockerComposeFullRenderedYAMLPath()},
113 Profiles: []string{`*`},
114 Action: []string{"down"},
115 })
116 if err != nil {
117 util.Warning("Failed to docker-compose down: %v", err)
118 }
119 }
120
121 // If any leftovers or lost souls, find them as well
122 containers, err := dockerutil.FindContainersByLabels(labels)
123 if err != nil {
124 return err
125 }
126 // First, try stopping the listed containers if they are running.
127 for i := range containers {
128 containerName := dockerutil.ContainerName(&containers[i])
129 removeOpts := client.ContainerRemoveOptions{
130 RemoveVolumes: true,
131 Force: true,
132 }
133 output.UserOut.Printf("Removing container: %s", containerName)
134 if _, err = apiClient.ContainerRemove(ctx, containers[i].ID, removeOpts); err != nil {
135 return fmt.Errorf("could not remove container %s: %v", containerName, err)
136 }
137 }
138 // Always kill the temporary volumes on ddev delete
139 vols := []string{"ddev-" + app.Name + "-snapshots", app.Name + "-ddev-config"}
140
141 for _, volName := range vols {
142 _ = dockerutil.RemoveVolume(volName)
143 }
144
145 return nil
146}
147
148// CheckForConf checks for a config.yaml at the cwd or parent dirs.
149func CheckForConf(confPath string) (string, error) {

Callers 1

StopMethod · 0.85

Calls 10

GetDockerClientFunction · 0.92
FileExistsFunction · 0.92
ComposeCmdFunction · 0.92
WarningFunction · 0.92
FindContainersByLabelsFunction · 0.92
ContainerNameFunction · 0.92
RemoveVolumeFunction · 0.92
GetNameMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected