Container deployment
Deploy containers either on Docker or on Podman
- Connectivity to the docker/podman backend is done by using the python packages docker and podman and by setting the environment variables
PODMAN_URL
andDOCKER_URL
- Once these are set, you can perform the operations
- By setting
POD_TYPE
you indicate either 'PODMAN' or 'DOCKER' to connect either with docker/podman on the specified connection
from rlike import *
environ = {
"PODMAN_URL": "http://localhost:2375",
"DOCKER_URL": "unix:///var/run/docker.sock",
"POD_TYPE": "XXXXXXXXXX"}
Sys_setenv(environ)
Sys_setenv("POD_TYPE", "PODMAN")
Sys_setenv("POD_TYPE", "DOCKER")
Basic interactivity with the docker/podman daemon
blackbar.containers.pod_info()
Get general information about a podman client. You can set the client information by setting environment varialbles PODMAN_URL and PODMAN_IDENTITY
Returns:
Type | Description |
---|---|
dict
|
a dictionary with elements url, version, platform_name, api, pod_api |
Examples:
>>> from blackbar import *
>>> x = pod_info()
blackbar.containers.pod_ls(type='images')
Get the list of images or containers
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type
|
str
|
either 'images' or 'containers' |
'images'
|
Returns:
Type | Description |
---|---|
DataFrame
|
For type: 'images': a pandas data frame with columns 'image_id', 'image_short_id', 'image_tags', 'image_labels', 'image_created', 'image_attrs' |
DataFrame
|
For type: 'containers': a pandas data frame with columns 'container_id', 'container_short_id', 'container_name', 'container_ports', 'container_image_id', 'container_state', 'container_created', 'container_started', 'container_cmd' |
Examples:
>>> from blackbar import *
>>> x = pod_ls('images')
>>> x = pod_ls('containers')
blackbar.containers.pod_pull(repository, tag=None, **kwargs)
Pull a docker container from a repository
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repository
|
str
|
character string with the name of the repository |
required |
tag
|
str
|
character string with the tag of the repository |
None
|
kwargs
|
a named set of arguments, in particular auth_config |
{}
|
Returns:
Type | Description |
---|---|
Image
|
An object type podman.domain.images.Image which is a container image |
Examples:
>>> from blackbar import *
>>> x = pod_pull('busybox')
>>> x = pod_pull('busybox', tag = 'latest')
>>> x = pod_pull('ghcr.io/bnosac/blackbar-base')
>>> x = pod_pull('docker.io/library/busybox:latest')
>>> auth = {"username": "XXXXXXXXXX", "password": "XXXXXXXXXX"}
>>> x = pod_pull("registry.datatailor.be/blackbar-anonymization:latest", auth_config = auth)
>>> x = pod_pull("registry.datatailor.be/blackbar-pseudonymization:latest", auth_config = auth)
>>> x = pod_pull("registry.datatailor.be/blackbar-apps:latest", auth_config = auth)
>>> x = pod_pull("registry.datatailor.be/blackbar-api:latest", auth_config = auth)
Build images, start/stop containers, get logs
blackbar.containers.pod_build(path, dockerfile, tag='blackbar/default', cache=True, rm=True, pull=False, args={}, trace=False)
Build a container
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
full path to the directory containing the dockerfile |
required |
dockerfile
|
str
|
full path to the Dockerfile |
required |
tag
|
str
|
string with the tag to assign to the image which will be constructed |
'blackbar/default'
|
cache
|
bool
|
Use the cache. Default True. |
True
|
rm
|
bool
|
Remove intermediate containers. Default True. |
True
|
pull
|
bool
|
Downloads any updates to the FROM image in Dockerfile. Default: False. |
False
|
args
|
dict
|
a dictionary of build-time arguments |
{}
|
trace
|
bool
|
boolean indicating to print the log |
False
|
Returns:
Type | Description |
---|---|
Image
|
An dictionary with elements 'image' of type podman.domain.images.Image which is a container image and log which is a list of log strings |
blackbar.containers.pod_container(type, name, image=None, command=None, stdout=True, stderr=True, remove=False, detach=True, environment={}, restart_policy={'Name': 'always', 'MaximumRetryCount': 5}, **kwargs)
Launch a command in a container
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type
|
str
|
string with the type of action to perform. Either 'run', 'start', 'restart', 'stop', 'remove' |
required |
name
|
str
|
the name to assign to the container |
required |
image
|
str
|
string with the name of the image |
None
|
command
|
Union[str, List[str]]
|
The command to execute in the container. Defaults to None. |
None
|
stdout
|
bool
|
Include stdout. Default: True |
True
|
stderr
|
bool
|
Include stderr. Default: True |
True
|
remove
|
bool
|
Delete container when the container's processes exit. Default: False. |
False
|
detach
|
bool
|
Run container in the background and return a Container object. Default: True. |
True
|
environment
|
dict
|
a dictionary of environment variables to pass on to the container |
{}
|
restart_policy
|
dict
|
a dictionary of environment variables to pass on to the container. Defaults to {"Name": "always", "MaximumRetryCount": 5} |
{'Name': 'always', 'MaximumRetryCount': 5}
|
kwargs
|
further arguments passed on to pod_client().containers.run for type 'run'. Documentation of arguments - see https://podman-py.readthedocs.io/en/stable/podman.domain.containers_run.html and https://podman-py.readthedocs.io/en/stable/podman.domain.containers_create.html |
{}
|
Returns:
Type | Description |
---|---|
Union[Container, None]
|
an object of class podman.domain.containers.Container |
Examples:
>>> from blackbar import *
>>> x = pod_pull('busybox')
>>> msg = pod_container("run", name = "blackbar-test", image = "busybox", command = "env", remove = True)
>>> msg = pod_container_log("blackbar-test")
>>> msg = pod_remove("blackbar-test", type = "containers")
blackbar.containers.pod_container_log(container, stdout=True, stderr=True, timestamps=True, tail='all', trace=False)
Get the log of a container
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container
|
str
|
string with the name of the container |
required |
stdout
|
bool
|
Include stdout. Default: True |
True
|
stderr
|
bool
|
Include stderr. Default: True |
True
|
timestamps
|
bool
|
Show timestamps in output. Default: True |
True
|
tail
|
Union[str, int]
|
integer representing the number of lines to at the end of logs to extract, or the string 'all' |
'all'
|
trace
|
bool
|
boolean indicating to print the log or return the log. Defaults to returning |
False
|
Returns:
Type | Description |
---|---|
Union[None, List[str]]
|
prints the log or returns a list of strings of the log |
Examples:
>>> from blackbar import *
>>> x = pod_pull('busybox')
>>> msg = pod_container("run", name = "blackbar-test", image = "busybox", command = "env", remove = True)
>>> msg = pod_container_log("blackbar-test")
>>> msg = pod_remove("blackbar-test", type = "containers")
blackbar.containers.pod_remove(id, type='images', force=True)
Removes an image or a container
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
str
|
the id or name of the image or repository |
required |
type
|
str
|
either 'images' or 'containers' |
'images'
|
force
|
bool
|
indicates to delete image even if in use or to kill a running container before deleting |
True
|