Testcontainers-java: Is there a way to set the memory capacity for a `GenericContainer`?

Created on 24 Feb 2017  路  11Comments  路  Source: testcontainers/testcontainers-java

I have the following GenericContainer:

/**
 * A docker container that can be used with an @ClassRule to start the container before
 * and tear it down after a test class. This container exposes port 9200 as 9201 and port 9300 as 9301.
 * It also copies over the elasticsearch.yml file from the test resources.
 */
public class ElasticsearchTestContainer extends GenericContainer {

    private static final ImageFromDockerfile IMAGE_FROM_DOCKERFILE =
            new ImageFromDockerfile("test-es-image", false)
                    .withDockerfileFromBuilder(builder -> builder
                            .from("elasticsearch:2.2")
                            .run("/usr/share/elasticsearch/bin/plugin install analysis-icu")
                            .run("/usr/share/elasticsearch/bin/plugin install delete-by-query")
                            .copy("elasticsearch.yml", "/usr/share/elasticsearch/config/elasticsearch.yml")
                            .build()
                    ).withFileFromClasspath("elasticsearch.yml", "docker/elasticsearch.yml");

    public ElasticsearchTestContainer() {
        super(IMAGE_FROM_DOCKERFILE);
        this.setPortBindings(Arrays.asList("9201:9200", "9301:9300"));
        this.setNetworkMode("devhost.hostname.com");
        this.setWaitStrategy(Wait.forHttp("/"));
    }
}

Normally, I would specify the memory using the -m flag to docker. How can I do this with this container?

Most helpful comment

@kiview of course.
In our development build, we use testcontainers to simulate our production environment to run E2E tests. So during our build testcontainers spins up about 10 docker images that must be tested together.

At this moment we are having some problems that our build server is running out of resources when running multiple builds simultaneously. The obvious way to prevent this from happening is to limit the max resources that can be used by each docker container.
We have all the docker images that are being started on our build server limited, except the ones the are spun up by testcontainers. We can also limit it using parameter of the java process inside the docker testcontainer, but it would be nice to be able to limit cpu usage when starting the container.

All 11 comments

So you want to set a memory limit? AFAIK testcontainers doesn't expose an API for this yet.

I also only found a fitting API in docker-java inside the UpdateContainerCmd class, not inside CreateContainerCmd.

+1
I was also looking for this feature.

@benhinssen could you please give some context for use cases for which this feature is needed?

It looks like it's there on CreateContainerCmd. It's worth supporting, but we might be opening a can of worms if we add this directly to the GenericContainer API.

Perhaps the more maintainable approach is to use some upcoming preconfig/postconfig hooks that @bsideup was working on that allow you to modify the CreateContainerCmd directly before it's created. How does that sound?

+1 from me on hooks :)

Something like:

new GenericContainer(...)
    .withCreateCmdHook(cmd -> cmd.withMemory(1024))

(naming is not final)

@kiview of course.
In our development build, we use testcontainers to simulate our production environment to run E2E tests. So during our build testcontainers spins up about 10 docker images that must be tested together.

At this moment we are having some problems that our build server is running out of resources when running multiple builds simultaneously. The obvious way to prevent this from happening is to limit the max resources that can be used by each docker container.
We have all the docker images that are being started on our build server limited, except the ones the are spun up by testcontainers. We can also limit it using parameter of the java process inside the docker testcontainer, but it would be nice to be able to limit cpu usage when starting the container.

@macks22 @benhinssen since 1.2.0 is out, does https://www.testcontainers.org/usage/options.html#customizing-the-container sound good enough for you? :)

@bsideup yes this meets the need; thank you! @benhinssen feel free to close if your use case is satisfied.

Looks good and fits the requirement! Thanks!
@macks22, for me you can close the issue, it seems as I can't as I did not open the issue.

Great, thanks @benhinssen, @macks22!

I'll close the ticket then 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ParafeniukMikalaj picture ParafeniukMikalaj  路  3Comments

oneiros-de picture oneiros-de  路  3Comments

rnorth picture rnorth  路  3Comments

micheal-swiggs picture micheal-swiggs  路  4Comments

aniketbhatnagar picture aniketbhatnagar  路  3Comments