Skip to content

Provide generic DockerContainer with init attributes #236

Open

Description

@vikahl

First I want to thank you for this very nice and convenient library.

Background

I am using the library for smoke tests for microservices. In these tests I spin up a container and make sure I can e.g., fetch an endpoint and see that the container is built and starts correctly.

The library provides all convenient methods, except for a way to set e.g., ports and env variables in the context manager. To solve this I have subclassed testcontainers.core.container.DockerContainer into a class as below:

from typing import Any

import testcontainers.core.container

class ContainerContext(testcontainers.core.container.DockerContainer):
    def __init__(
        self, image: str, ports: list[int], env_vars: dict[str, str], **kwargs: Any
    ) -> None:
        super().__init__(image, **kwargs)

        self.with_exposed_ports(*ports)
        for env, value in env_vars.items():
            self.with_env(env, value)

and use it as

import requests

def test_smoke():
    with container_context(
        "my_service_image",
        ports=[8000],
        env_vars={"SOME_ENV_SETTINGS": "some_value", "SOME_ENV_SETTINGS2": "other_value"},
    ) as con:
        r = requests.get(
            f"http://{con.get_container_host_ip()}:{con.get_exposed_port(8000)}"
        )
        assert r

Proposal

  • Include a generic container class where all name, ports, env, volumes, … can be set as init variables.

I saw #114 that removed the previous GenericContainer but I don't see a motivation why it was done and why it could not be extended.

Activity

  1. tillahoffmann commented on Aug 24, 2022

    @tillahoffmann
    Contributor

    Yes, that's probably a good idea because we already support all the other (less commonly used) arguments through keyword arguments. Are you open to submitting a PR?

    Regarding GenericContainer, I deprecated it because it's the same as DockerContainer just with a different name.

  2. vikahl commented on Aug 24, 2022

    @vikahl
    Author

    I'm happy to submit a PR!

  3. vikahl commented on Aug 25, 2022

    @vikahl
    Author

    Submitted a PR, happy for a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions