File size: 1,610 Bytes
105b369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Optional, Any

from phi.utils.log import logger


class DockerApiClient:
    def __init__(self, base_url: Optional[str] = None, timeout: int = 30):
        super().__init__()
        self.base_url: Optional[str] = base_url
        self.timeout: int = timeout

        # DockerClient
        self._api_client: Optional[Any] = None
        logger.debug("**-+-** DockerApiClient created")

    def create_api_client(self) -> Optional[Any]:
        """Create a docker.DockerClient"""
        import docker

        logger.debug("Creating docker.DockerClient")
        try:
            if self.base_url is None:
                self._api_client = docker.from_env(timeout=self.timeout)
            else:
                self._api_client = docker.DockerClient(base_url=self.base_url, timeout=self.timeout)
        except Exception as e:
            logger.error("Could not connect to docker. Please confirm docker is installed and running")
            logger.error(e)
            logger.info("Fix:")
            logger.info("- If docker is running, please check output of `ls -l /var/run/docker.sock`.")
            logger.info(
                '- If file does not exist, please run: `sudo ln -s "$HOME/.docker/run/docker.sock" /var/run/docker.sock`'
            )
            logger.info("- More info: https://docs.phidata.com/faq/could-not-connect-to-docker")
            exit(0)
        return self._api_client

    @property
    def api_client(self) -> Optional[Any]:
        if self._api_client is None:
            self._api_client = self.create_api_client()
        return self._api_client