Cleanup will remove DDEV containers and volumes even if docker-compose.yml has been deleted.
(app *DdevApp)
| 93 | // Cleanup will remove DDEV containers and volumes even if docker-compose.yml |
| 94 | // has been deleted. |
| 95 | func 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. |
| 149 | func CheckForConf(confPath string) (string, error) { |
no test coverage detected