testcontainers / testcontainers/testcontainers-python

Provide generic DockerContainer with init attributes

Open
#236 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

✅ close on merge 📦 package: core 🚀 enhancement
Dominant language
Python
Stars
2.3k
Forks
386
Avg merge
4h 40m
Merged PRs (30d)
1

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review testcontainers.core.container.DockerContainer and the previous GenericContainer removal discussed in #114. Compare the requested init attributes—name, ports, env, and volumes—with the existing container API, and consider the proposal complete when a generic container can configure these values at initialization without subclassing.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
devops, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.