Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : Remove Images Associated with Stopped Containers (Issue : #4) #10

Merged

Conversation

deexithparand
Copy link
Contributor

Code Updates

  • Main functionality : *CleanupStoppedContainerImages(cli client.Client)
  • Additional Fix 01 : Updated .gitignore, made it more generalized
  • Additional Fix 02 : Updated the structure of cmd/cleaner/main.go, made it more consistent

Implementation Details

The function first lists all Docker images and initializes them as "unreferenced". It then lists all containers and updates the state of each image based on its associated containers:

  • If all associated containers are stopped, the image is marked for potential removal.
  • If any associated container is running, the image is marked as in use.

Images marked for potential removal are then removed using the Docker API. After successful image removal, associated stopped containers are also removed.

Purpose of This Functionality

This ensures that even though the images are deleted, the associated stopped containers are not left behind. If the stopped containers remain without their associated images, it could cause errors during future runs of the --remove-stopped command, such as "the associated image of the stopped container is not present." This functionality prevents those errors by cleaning up both the images and their stopped containers together.

Tested Example
image

@mmuazam98
Copy link
Owner

mmuazam98 commented Oct 3, 2024

@deexithparand

Thanks for your contribution! The current implementation works, but we can refactor it to improve maintainability and modularity. Instead of passing the Docker client around as a parameter, we can encapsulate the client in a struct and add methods to that struct. This will clean up the main logic and make it easier to extend the functionality in the future.

Here’s what I suggest:

  • Create a DockerClient struct that holds the Docker client. (Similar to what I have done)
  • Add methods like RemoveUnusedImages, CleanupStoppedContainerImages, and PrintUnusedImages on this struct.
  • This will simplify the logic in main.go and make it easier to unit test the Docker interactions.

I can push an example refactor if you'd like! 😄

@mmuazam98
Copy link
Owner

type DockerClient struct {
	cli *client.Client
}

func (d *DockerClient) RemoveUnusedImages() {...}

func (d *DockerClient) CleanupStoppedContainerImages() {...}

func (d *DockerClient) PrintUnusedImages() {...}

@deexithparand
Copy link
Contributor Author

Hi @mmuazam98, I've updated the code as requested and also modified the folder structure little bit to add more best practices. Please check and merge the PR if its satisfactory.

@mmuazam98
Copy link
Owner

@deexithparand Looks good mostly. Have you tested all the scenarios?

@deexithparand
Copy link
Contributor Author

@mmuazam98 after refactoring, I've tested all the usecases which were previously tested for the old code. Works fine for me.

@mmuazam98 mmuazam98 merged commit 173e470 into mmuazam98:main Oct 7, 2024
@deexithparand deexithparand deleted the issue-4-rmi-stopped-containers branch October 19, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants