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

How to remove images created with FromDockerfile? #303

Closed
dubonzi opened this issue Mar 29, 2021 · 10 comments
Closed

How to remove images created with FromDockerfile? #303

dubonzi opened this issue Mar 29, 2021 · 10 comments
Labels
feature New functionality or new behaviors on the existing one

Comments

@dubonzi
Copy link

dubonzi commented Mar 29, 2021

I've been creating some of my containers using FromDockerfile, is there any way to remove the built images after my tests run? The image list starts to get a bit crowded after a few test runs with all the tags testcontainers create.
image

@gianarb
Copy link
Collaborator

gianarb commented Mar 31, 2021

This is a good question, the java library has an automatic detection feature:

https://www.testcontainers.org/features/creating_images/

but Java has a trigger that can be used when a test suite ends, we do not have this luxury in Go. So we can label every image with a specific label that detects the image created by testcontainers in this way users can figure out a way to list and remove the images with a specific label. But this is not much different compared with docker system prune if the server where testcontainers runs only runs testcontainers

@gianarb gianarb added the feature New functionality or new behaviors on the existing one label Mar 31, 2021
@dubonzi
Copy link
Author

dubonzi commented Mar 31, 2021

So we can label every image with a specific label that detects the image created by testcontainers in this way users can figure out a way to list and remove the images with a specific label.

This would make removing the images easier. In my case, this is only a problem when running the tests locally, not really a problem, but would be nice to have :). Thanks for the reply!

@mdelapenya
Copy link
Collaborator

Well it's not actually Java but the test runner you use. We use godog for cucumber scenarios and the test runner provides life cycle hooks for before/after test suite, before/after scenario and before/after step, and there I destroy whatever is needed.

@hey-xico
Copy link
Contributor

hey-xico commented Apr 9, 2021

I was having this "problem" and it was kind of boring having to manually delete the containers.
I don't know if this the best way to solve this but I did it and it's working fine.

Up til now, my machine didn't explode so... 🤷

t.Cleanup(func() {
			ctx := context.Background()
			cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
			if err != nil {
				panic(err)
			}
			cli.ContainersPrune(ctx, filters.Args{})
		})

where t is *testing.T

@rzajac
Copy link
Contributor

rzajac commented Oct 31, 2021

I delete images by repo:tag something like this:

func BuildFromDockerfile(ctx context.Context, req tc.ContainerRequest) (string, func(context.Context) error, error) {
	prv, err := tc.NewDockerProvider()
	if err != nil {
		return "", nil, err
	}

	tag, err := prv.BuildImage(ctx, &req)
	if err != nil {
		return "", nil, err
	}

	cleanup := func(ctx context.Context) error {
		cli, err := client.NewClientWithOpts(
			client.FromEnv,
			client.WithAPIVersionNegotiation(),
		)
		if err != nil {
			return err
		}
		opts := types.ImageRemoveOptions{
			Force:         true,
			PruneChildren: true,
		}
		_, err = cli.ImageRemove(ctx, tag, opts)
		return err
	}

	return tag, cleanup, nil
}

@gaborszakacs
Copy link
Contributor

This custom BuildFromDockerfile function (and calling the returned cleanup function in t.Cleanup did remove the build image, thanks!

One thing I noticed though is that intermediate containers and images still hang around, I proposed this change to remove them once the build is complete: #403

What do you think about making image removal getting triggered on container.Terminate (if FromDockerfile was provided) ?

@mdelapenya
Copy link
Collaborator

I like this approach @gaborszakacs, and it's something I was discussing with @kiview the other day. If this is something you can provide in a PR, I would be more than grateful to review it. Thanks in advance!

@gaborszakacs
Copy link
Contributor

Thanks @mdelapenya, for the input, I gave it a shot: #405

@gaborszakacs
Copy link
Contributor

gaborszakacs commented Feb 25, 2022

@mdelapenya gentle ping, these two PRs addresses this issue, can you take a look at them pls?

@mdelapenya
Copy link
Collaborator

I think we can close this one, as above PRs have been merged. Thank you all for your feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

No branches or pull requests

6 participants