Skip to content

docker

dev_tool.config.docker

log = logging.getLogger(__name__) module-attribute

DockerConfig

A class for managing Docker configuration settings.

This class provides methods for creating default Docker configurations based on environment variables and user settings.

create_default_docker_config

A method that creates a default Docker configuration dictionary.

This method combines user-provided configuration with environment variables to create a complete Docker container configuration.

Parameters:

  • container_name (str) –

    The name to use for the Docker container.

  • docker_config (dict) –

    The user-provided Docker configuration dictionary.

Returns:

  • dict

    A complete Docker configuration dictionary.

Source code in dev_tool/config/docker.py
def create_default_docker_config(self, container_name: str, docker_config: dict) -> dict:
    """
    A method that creates a default Docker configuration dictionary.

    This method combines user-provided configuration with environment variables
    to create a complete Docker container configuration.

    :param container_name: The name to use for the Docker container.
    :param docker_config: The user-provided Docker configuration dictionary.
    :return: A complete Docker configuration dictionary.
    """

    version = docker_config.get('postgres-version', '14')
    host_port = os.getenv('DATABASE_PORT', '5432')
    container_port = '5432'
    postgres_user = os.getenv('DATABASE_USER', 'stratus')
    postgres_password = os.getenv('DATABASE_PASSWORD', 'stratus')
    shm_size = docker_config.get('container-size', '8g')

    return {
        'container_name': container_name,
        'container': f'postgres:{version}',
        'host_port': int(host_port),
        'container_port': int(container_port),
        'environment': {
            'POSTGRES_USER': postgres_user,
            'POSTGRES_PASSWORD': postgres_password,
        },
        'shm_size': shm_size,
        **{
            k: v
            for k, v in docker_config.items()
            if k not in ('postgres-version', 'container-size')
        },
    }

get_dockerfile_path

Get the Dockerfile path - project Dockerfile takes precedence over default.

Returns:

  • Path | None

    Path to Dockerfile to use, or None if using standard postgres image

Source code in dev_tool/config/docker.py
def get_dockerfile_path(self) -> Path | None:
    """
    Get the Dockerfile path - project Dockerfile takes precedence over default.

    :return: Path to Dockerfile to use, or None if using standard postgres image
    """

    project = BASE / 'Dockerfile'

    if project.exists():
        return project

    from dev_tool.context import CONTEXT  # noqa: PLC0415
    default = CONTEXT.configuration.get_resource_path('templates/docker/Dockerfile')

    if default.exists():
        return default

    return None

should_build_custom_image

Determine if we should build a custom image.

Returns:

  • bool

    True if we should build custom image

Source code in dev_tool/config/docker.py
def should_build_custom_image(self) -> bool:
    """
    Determine if we should build a custom image.

    :return: True if we should build custom image
    """

    return self.get_dockerfile_path() is not None