-
Notifications
You must be signed in to change notification settings - Fork 300
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
feat(core)!: add support for tc.host
and de-prioritise docker:dind
#388
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bac3937
WIP Add support for tc.host
kiview c0db3d7
Merge branch 'main' into tc-host-support
kiview 745b1d4
Merge branch 'main' into tc-host-support
kiview 4f8693c
Merge branch 'main' into tc-host-support
totallyzen 9115f61
fix: small cleanup of the properties file fetching
balint-backmaker 6314374
from os.path import exists
alexanderankin 09775f9
fix: remove multiple sources, just leave user-wide settings
balint-backmaker b22c0f9
Merge branch 'main' into tc-host-support
totallyzen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
import functools as ft | ||
import os | ||
import urllib | ||
from os.path import exists | ||
from pathlib import Path | ||
from typing import Optional, Union | ||
|
||
import docker | ||
|
@@ -23,15 +25,8 @@ | |
from .utils import default_gateway_ip, inside_container, setup_logger | ||
|
||
LOGGER = setup_logger(__name__) | ||
|
||
|
||
def _stop_container(container: Container) -> None: | ||
try: | ||
container.stop() | ||
except NotFound: | ||
pass | ||
except Exception as ex: | ||
LOGGER.warning("failed to shut down container %s with image %s: %s", container.id, container.image, ex) | ||
Comment on lines
-26
to
-34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private function -> moved to the bottom of this file |
||
TC_FILE = ".testcontainers.properties" | ||
TC_GLOBAL = Path.home() / TC_FILE | ||
|
||
|
||
class DockerClient: | ||
|
@@ -40,7 +35,13 @@ class DockerClient: | |
""" | ||
|
||
def __init__(self, **kwargs) -> None: | ||
self.client = docker.from_env(**kwargs) | ||
docker_host = read_tc_properties().get("tc.host") | ||
|
||
if docker_host: | ||
LOGGER.info(f"using host {docker_host}") | ||
self.client = docker.DockerClient(base_url=docker_host) | ||
else: | ||
self.client = docker.from_env(**kwargs) | ||
|
||
@ft.wraps(ContainerCollection.run) | ||
def run( | ||
|
@@ -123,3 +124,33 @@ def host(self) -> str: | |
if ip_address: | ||
return ip_address | ||
return "localhost" | ||
|
||
|
||
@ft.cache | ||
def read_tc_properties() -> dict[str, str]: | ||
""" | ||
Read the .testcontainers.properties for settings. (see the Java implementation for details) | ||
Currently we only support the ~/.testcontainers.properties but may extend to per-project variables later. | ||
|
||
:return: the merged properties from the sources. | ||
""" | ||
tc_files = [item for item in [TC_GLOBAL] if exists(item)] | ||
if not tc_files: | ||
return {} | ||
settings = {} | ||
|
||
for file in tc_files: | ||
tuples = [] | ||
with open(file) as contents: | ||
tuples = [line.split("=") for line in contents.readlines() if "=" in line] | ||
settings = {**settings, **{item[0]: item[1] for item in tuples}} | ||
return settings | ||
|
||
|
||
def _stop_container(container: Container) -> None: | ||
try: | ||
container.stop() | ||
except NotFound: | ||
pass | ||
except Exception as ex: | ||
LOGGER.warning("failed to shut down container %s with image %s: %s", container.id, container.image, ex) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to do it, to get things working with Testcontainers Cloud within Codespaces (which runs inside a container). I also thought part of this code duplicate what is done in
docker_client.py
.