daemon.dockerize module
class daemon.dockerize.Dockerizer[source]
Bases: object
Helper class to interact with docker client & dockerd
logger = <jina.logging.logger.JinaLogger object>
client: DockerClient = <docker.client.DockerClient object>
raw_client: APIClient = <docker.api.client.APIClient object>
networks = []
images = []
containers = []
classmethod network(workspace_id)[source]
Create a docker bridge network with name workspace_id using a predefined ipam config. All containers under workspace_id would use this network. :type workspace_id: DaemonID :param workspace_id: workspace id :raises DockerNetworkException: if there are issues during network creation :rtype:
str
:return: id of the networkclassmethod build(workspace_id, daemon_file, logger)[source]
Build docker image using daemon file & tag it with workspace_id :type workspace_id: DaemonID :param workspace_id: workspace id :type daemon_file: DaemonFile :param daemon_file: daemon file describing content inside the workdir :type logger: JinaLogger :param logger: logger to be used :raises DockerImageException: if there are issues during image building :rtype:
str
:return: id of the imageclassmethod run_custom(workspace_id, daemon_file)[source]
Run a custom container during workspace creation. .. note:
This invalidates the default entrypint (mini-jinad) & uses the entrypoint provided
mentioned in the .jinad file (`run` section)
Parameters
workspace_id (DaemonID) – workspace id
daemon_file (DaemonFile) – daemon file describing content inside the workdir
Return type
Tuple
[ForwardRef
,str
,Dict
]Returns
tuple of container object, network id & ports
classmethod run(workspace_id, container_id, entrypoint, ports, envs={}, device_requests=None)[source]
Runs a container using an existing image (tagged with workspace_id). Maps ports to local dockerhost & tags the container with name container_id .. note:
This uses the default entrypoint (mini-jinad) & appends `command` for execution.
Parameters
workspace_id (DaemonID) – workspace id
container_id (DaemonID) – name of the container
entrypoint (
str
) – entrypoint for the containerports (
Dict
) – ports to be mapped with localenvs (
Optional
[Dict
]) – dict of env vars to be set in the containerdevice_requests (
Optional
[List
]) – docker device requests
Raises
DockerImageException – if image is not found locally
DockerContainerException – if container creation fails
Return type
Tuple
[ForwardRef
,str
,Dict
]Returns
tuple of container object, network id & ports
classmethod logs(id)[source]
Get all logs of a container
Parameters
id (
str
) – container idReturn type
str
Returns
logs as str
dockersock = ‘//var/run/docker.sock’
classmethod volume(workspace_id)[source]
Local volumes to be mounted inside the container during run. .. note:
Local workspace should always be mounted to default WORKDIR for the container (/workspace).
docker sock on dockerhost should also be mounted to make sure DIND works
Parameters
workspace_id (DaemonID) – workspace id
Return type
Dict
[str
,Dict
]Returns
dict of volume mappings
classmethod environment(envs)[source]
Environment variables to be set inside the container during run
Parameters
envs (
Dict
[str
,str
]) – dict of env vars to be set in the containerReturn type
Dict
[str
,str
]Returns
dict of env vars
classmethod remove(id)[source]
Determines type of jinad object & removes that from dockerd :type id: DaemonID :param id: DaemonID describing local docker object
classmethod containers_in_network(id)[source]
Get all containers currently connected to network :type id:
str
:param id: network id :rtype:List
:return: list of containers connected to network idclassmethod rm_network(id)[source]
Remove network from local if no containers are connected :type id:
str
:param id: network id :rtype:bool
:return: True if deletion is successful else Falseclassmethod rm_image(id)[source]
Remove image from local :type id:
str
:param id: image id :raises KeyError: if image is not found on local :raises DockerImageException: error during image removalclassmethod rm_container(id)[source]
Remove container from local :type id:
str
:param id: container id :raises KeyError: if container is not found on local :raises DockerContainerException: error during container removalclassmethod exposed_ports()[source]
Checks the currently running docker containers for ports exposed to the host :return: A set of ports exposed by all currently running docker containers