@@ -723,31 +723,45 @@ func CheckPodmanComposeError(b string) error {
723723 return lastErr
724724}
725725
726- func (c * ContainerClient ) ComposeDown (ctx context.Context , w io.Writer , projectName string ) error {
726+ func (c * ContainerClient ) ComposeDown (ctx context.Context , w io.Writer , projectName string , defaultWorkingDir string ) error {
727727 // TODO: Read setting from configuration
728728 manualCleanup := false
729729 errs := make ([]error , 0 )
730730
731- workingDir := ""
732-
733731 projectFilter := filters .NewArgs (
734732 filters .Arg ("label" , "com.docker.compose.project=" + projectName ),
735733 )
734+ slog .Info ("Searching for project containers." , "project" , projectName )
736735 projectContainers , err := c .Client .ContainerList (ctx , container.ListOptions {
737736 Filters : projectFilter ,
738737 })
739738 if err != nil {
740739 errs = append (errs , err )
741740 }
741+ slog .Info ("Found project containers." , "count" , len (projectContainers ))
742+
743+ workingDir := ""
744+
745+ // Prefer using the working_dir on the container rather than
746+ // the default project working dir as the project could
742747 for _ , item := range projectContainers {
743748 if v , ok := item .Labels ["com.docker.compose.project.working_dir" ]; ok {
744- workingDir = v
745- break
749+ if utils .PathExists (v ) {
750+ slog .Info ("Using project working dir found on container label." , "project" , projectName , "working_dir" , workingDir , "container_id" , item .ID , "container_name" , item .Names )
751+ workingDir = v
752+ break
753+ }
746754 }
747755 }
748756
757+ // Fallback to the default project dir
758+ if workingDir == "" {
759+ slog .Info ("Using default project working dir." , "path" , defaultWorkingDir )
760+ workingDir = defaultWorkingDir
761+ }
762+
749763 // Find
750- if workingDir != "" && utils .PathExists (workingDir ) {
764+ if utils .PathExists (workingDir ) {
751765 command , args , err := prepareComposeCommand ("down" , "--remove-orphans" , "--volumes" )
752766 if err != nil {
753767 return err
@@ -760,15 +774,16 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
760774
761775 if err == nil {
762776 slog .Info ("Removing project directory." , "dir" , workingDir )
763- if err := os .RemoveAll (workingDir ); err != nil {
777+ if removeErr := os .RemoveAll (workingDir ); removeErr != nil {
764778 // non critical error
765- slog .Warn ("Failed to remove project directory." , "err" , err )
779+ slog .Warn ("Failed to remove project directory." , "err" , removeErr )
766780 }
767- return nil
768781 }
769782
770783 errs = append (errs , err )
771784 slog .Warn ("compose failed." , "err" , err )
785+ } else {
786+ errs = append (errs , fmt .Errorf ("compose project working directory does not exist. dir=%s" , workingDir ))
772787 }
773788
774789 if ! manualCleanup {
@@ -780,6 +795,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
780795
781796 // Stop containers
782797 for _ , item := range projectContainers {
798+ slog .Info ("Manually stopping container." , "id" , item .ID , "names" , item .Names )
783799 if err := c .Client .ContainerStop (ctx , item .ID , container.StopOptions {}); err != nil {
784800 slog .Warn ("Failed to stop container." , "err" , err )
785801 errs = append (errs , err )
@@ -788,6 +804,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
788804
789805 // Remove containers
790806 for _ , item := range projectContainers {
807+ slog .Info ("Manually removing container." , "id" , item .ID , "names" , item .Names )
791808 if err := c .Client .ContainerRemove (ctx , item .ID , container.RemoveOptions {
792809 RemoveVolumes : true ,
793810 RemoveLinks : true ,
@@ -808,6 +825,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
808825
809826 // Remove networks
810827 for _ , item := range projectNetworks {
828+ slog .Info ("Manually removing network." , "name" , item .ID )
811829 if err := c .Client .NetworkRemove (ctx , item .ID ); err != nil {
812830 slog .Warn ("Failed to remove network." , "err" , err )
813831 errs = append (errs , err )
@@ -823,6 +841,7 @@ func (c *ContainerClient) ComposeDown(ctx context.Context, w io.Writer, projectN
823841 }
824842
825843 for _ , item := range projectVolumes .Volumes {
844+ slog .Info ("Manually removing volume." , "name" , item .Name )
826845 if err := c .Client .VolumeRemove (ctx , item .Name , true ); err != nil {
827846 slog .Warn ("Failed to remove volume." , "err" , err )
828847 errs = append (errs , err )
0 commit comments