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

[Enhancement]: Cache FromDockerfile builds to allow multiple containers using the same image #1485

Closed
turt2live opened this issue Aug 10, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@turt2live
Copy link

Proposal

When using FromDockerfile, the image is built every time the container is requested, but the contents of that image are unlikely to change. Being able to specify a cache key or something to avoid a secondary build would be appreciated.

See #1484 for how I've worked around this already. The following being the code I've used:

var mmrCachedImage string
var mmrCachedContext *os.File

func reuseMmrBuild(ctx context.Context) (string, error) {
	if mmrCachedImage != "" {
		return mmrCachedImage, nil
	}
	log.Println("[Test Deps] Building MMR image...")
	cr, err := createDockerContext()
	if err != nil {
		return "", err
	}
	mmrCachedContext = cr
	buildReq := testcontainers.GenericContainerRequest{
		ContainerRequest: testcontainers.ContainerRequest{
			FromDockerfile: testcontainers.FromDockerfile{
				Dockerfile:     "Dockerfile",
				Context:        ".",
				ContextArchive: cr,
				PrintBuildLog:  true,
			},
		},
		Started: false,
	}
	provider, err := buildReq.ProviderType.GetProvider(testcontainers.WithLogger(testcontainers.Logger))
	if err != nil {
		return "", err
	}
	c, err := provider.CreateContainer(ctx, buildReq.ContainerRequest)
	if err != nil {
		return "", err
	}
	if dockerC, ok := c.(*testcontainers.DockerContainer); !ok {
		return "", errors.New("failed to convert built MMR container to a DockerContainer")
	} else {
		mmrCachedImage = dockerC.Image
	}
	log.Println("[Test Deps] Cached build as ", mmrCachedImage)
	return mmrCachedImage, nil
}

Note how it effectively gutwrenches the image name out of the container so it can reuse it later as testcontainers.ContainerRequest{Image: mmrImage}.

@turt2live turt2live added the enhancement New feature or request label Aug 10, 2023
@lefinal
Copy link
Contributor

lefinal commented Oct 25, 2023

Would be fixed by #1785 I guess

@mdelapenya
Copy link
Member

@turt2live could you try with the latest release? #1785 was merged in v0.26.0, so it should be possible now.

Thanks!

@turt2live
Copy link
Author

oooh, absolutely will give it a go. Thank you!

@turt2live
Copy link
Author

KeepImage: true seems to do that job - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants