本文整理汇总了Python中docker.models.containers.Container方法的典型用法代码示例。如果您正苦于以下问题:Python containers.Container方法的具体用法?Python containers.Container怎么用?Python containers.Container使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.models.containers
的用法示例。
在下文中一共展示了containers.Container方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def __init__(self, username: str, labbook: Optional[LabBook] = None, path: Optional[str] = None,
override_image_name: Optional[str] = None):
"""Set up local Docker access and proceed to standard superclass init
Args:
username: the logged-in Gigantum username (or a special-purpose username)
labbook: if provided, it is assumed that labbook.name and labbook.owner are populated (including from a path)
path: (ignored if labbook is not None) a path where we can load a LabBook from
override_image_name: should be set programmatically if we can obtain labbok owner and name, and there's no
"override" image "tag"
"""
self._client = get_docker_client()
# We can populate _image_id with .build_image() or .image_available(),
# the latter doesn't check the environment hash and is fast
self._image_id: Optional[str] = None
self._container: Optional[Container] = None
ContainerOperations.__init__(self, username, labbook=labbook, path=path,
override_image_name=override_image_name)
# Manipulating images
示例2: _create_oef_docker_image
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def _create_oef_docker_image(oef_addr_, oef_port_) -> Container:
client = docker.from_env()
logger.info(ROOT_DIR + "/tests/common/oef_search_pluto_scripts")
ports = {
"20000/tcp": ("0.0.0.0", 20000), # nosec
"30000/tcp": ("0.0.0.0", 30000), # nosec
"{}/tcp".format(oef_port_): ("0.0.0.0", oef_port_), # nosec
}
volumes = {
ROOT_DIR
+ "/tests/common/oef_search_pluto_scripts": {"bind": "/config", "mode": "rw"},
ROOT_DIR + "/data/oef-logs": {"bind": "/logs", "mode": "rw"},
}
c = client.containers.run(
"fetchai/oef-search:0.7",
"/config/node_config.json",
detach=True,
ports=ports,
volumes=volumes,
)
return c
示例3: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container, response_text: str) -> None:
config_data = get_config(container)
assert config_data["workers_per_core"] == 1
assert config_data["host"] == "0.0.0.0"
assert config_data["port"] == "80"
assert config_data["loglevel"] == "info"
assert config_data["workers"] >= 2
assert config_data["bind"] == "0.0.0.0:80"
logs = get_logs(container)
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert (
"Running inside /app/prestart.sh, you could add migrations to this file" in logs
)
response = requests.get("http://127.0.0.1:8000")
assert response.text == response_text
示例4: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container) -> None:
process_names = get_process_names(container)
config_data = get_config(container)
assert config_data["workers"] == 1
assert len(process_names) == 2 # Manager + worker
assert config_data["host"] == "127.0.0.1"
assert config_data["port"] == "80"
assert config_data["loglevel"] == "warning"
assert config_data["bind"] == "127.0.0.1:80"
assert config_data["graceful_timeout"] == 120
assert config_data["timeout"] == 120
assert config_data["keepalive"] == 5
assert config_data["errorlog"] == "-"
assert config_data["accesslog"] == "-"
logs = get_logs(container)
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert (
"Running inside /app/prestart.sh, you could add migrations to this file" in logs
)
assert "loglevel: debug" in logs
assert "Using worker: uvicorn.workers.UvicornH11Worker" in logs
示例5: test_defaults
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def test_defaults() -> None:
name = os.getenv("NAME")
image = f"tiangolo/uvicorn-gunicorn:{name}"
response_text = get_response_text1()
sleep_time = int(os.getenv("SLEEP_TIME", 1))
remove_previous_container(client)
container: Container = client.containers.run(
image,
name=CONTAINER_NAME,
ports={"80": "8000"},
detach=True,
command="/start-reload.sh",
)
time.sleep(sleep_time)
verify_container(container, response_text)
container.exec_run(
"sed -i 's|Uvicorn with Gunicorn|Uvicorn with autoreload|' /app/main.py"
)
new_response_text = response_text.replace(
"Uvicorn with Gunicorn", "Uvicorn with autoreload"
)
time.sleep(sleep_time)
verify_container(container, new_response_text)
container.stop()
container.remove()
示例6: _containers_by_role
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def _containers_by_role(self, role: Role) -> Set[Container]:
"""
Return all containers in this cluster of a particular node type.
"""
node_types = {
Role.MASTER: NODE_TYPE_MASTER_LABEL_VALUE,
Role.AGENT: NODE_TYPE_AGENT_LABEL_VALUE,
Role.PUBLIC_AGENT: NODE_TYPE_PUBLIC_AGENT_LABEL_VALUE,
}
client = docker_client()
filters = {
'label': [
self._cluster_id_label,
'{key}={value}'.format(
key=NODE_TYPE_LABEL_KEY,
value=node_types[role],
),
],
}
return set(client.containers.list(filters=filters))
示例7: to_node
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def to_node(self, node_representation: Container) -> Node:
"""
Return the ``Node`` that is represented by a given ``container``.
"""
container = node_representation
networks = container.attrs['NetworkSettings']['Networks']
network_name = 'bridge'
if len(networks) != 1:
[network_name] = list(networks.keys() - set(['bridge']))
address = IPv4Address(networks[network_name]['IPAddress'])
return Node(
public_ip_address=address,
private_ip_address=address,
default_user=self._ssh_default_user,
ssh_key_path=self.ssh_key_path,
default_transport=self._transport,
)
示例8: loopback_sidecars_by_name
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def loopback_sidecars_by_name(name: str) -> List[Container]:
"""
Return all loopback sidecar containers with the given sidecar ``name``.
"""
client = docker_client()
filters = {
'label': [
'{key}={value}'.format(
key=NODE_TYPE_LABEL_KEY,
value=NODE_TYPE_LOOPBACK_SIDECAR_LABEL_VALUE,
),
'{key}={value}'.format(
key=SIDECAR_NAME_LABEL_KEY,
value=name,
),
],
}
return list(client.containers.list(filters=filters))
示例9: run
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def run(self, image: str,
command: str = None,
environment: dict = None,
ports: dict = None,
detach: bool = False,
stdout: bool = True,
stderr: bool = False,
remove: bool = False, **kwargs) -> Container:
return self.client.containers.run(image,
command=command,
stdout=stdout,
stderr=stderr,
remove=remove,
detach=detach,
environment=environment,
ports=ports,
**kwargs)
示例10: start
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def start(self):
print("")
print("{} {}".format(crayons.yellow("Pulling image"),
crayons.red(self.image)))
with blindspin.spinner():
docker_client = self.get_docker_client()
self._container = docker_client.run(self.image,
command=self._command,
detach=True,
environment=self.env,
ports=self.ports,
name=self._name,
volumes=self.volumes,
**self._kargs
)
print("")
print("Container started: ",
crayons.yellow(self._container.short_id, bold=True))
return self
示例11: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container, response_text: str) -> None:
response = requests.get("http://127.0.0.1:8000")
assert response.text == response_text
config_data = get_config(container)
assert config_data["workers_per_core"] == 2
assert config_data["host"] == "0.0.0.0"
assert config_data["port"] == "80"
assert config_data["loglevel"] == "info"
assert config_data["workers"] > 2
assert config_data["bind"] == "0.0.0.0:80"
logs = get_logs(container)
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert (
"Running inside /app/prestart.sh, you could add migrations to this file" in logs
)
示例12: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container, response_text: str) -> None:
response = requests.get("http://127.0.0.1:8080")
assert response.text == response_text
nginx_config = get_nginx_config(container)
assert "client_max_body_size 0;" not in nginx_config
assert "worker_processes 2;" in nginx_config
assert "listen 8080;" in nginx_config
assert "worker_connections 2048;" in nginx_config
assert "worker_rlimit_nofile;" not in nginx_config
assert "daemon off;" in nginx_config
assert "include uwsgi_params;" in nginx_config
assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config
assert "keepalive_timeout 300;" in nginx_config
logs = get_logs(container)
assert "getting INI configuration from /app/uwsgi.ini" in logs
assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs
assert "ini = /app/uwsgi.ini" in logs
assert "ini = /etc/uwsgi/uwsgi.ini" in logs
assert "socket = /tmp/uwsgi.sock" in logs
assert "chown-socket = nginx:nginx" in logs
assert "chmod-socket = 664" in logs
assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs
assert "need-app = true" in logs
assert "die-on-term = true" in logs
assert "show-config = true" in logs
assert "wsgi-file = /app/main.py" in logs
assert "processes = 16" in logs
assert "cheaper = 2" in logs
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert (
"Running inside /app/prestart.sh, you could add migrations to this file" in logs
)
assert "spawned uWSGI master process" in logs
assert "spawned uWSGI worker 1" in logs
assert "spawned uWSGI worker 2" in logs
assert "spawned uWSGI worker 3" not in logs
assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs
assert "success: nginx entered RUNNING state, process has stayed up for" in logs
assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs
示例13: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container, response_text: str) -> None:
response = requests.get("http://127.0.0.1:8000")
assert response.text == response_text
nginx_config = get_nginx_config(container)
assert "client_max_body_size 0;" in nginx_config
assert "worker_processes 1;" in nginx_config
assert "listen 80;" in nginx_config
assert "worker_connections 1024;" in nginx_config
assert "worker_rlimit_nofile;" not in nginx_config
assert "daemon off;" in nginx_config
assert "include uwsgi_params;" in nginx_config
assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config
logs = get_logs(container)
assert "getting INI configuration from /app/uwsgi.ini" in logs
assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs
assert "ini = /app/uwsgi.ini" in logs
assert "ini = /etc/uwsgi/uwsgi.ini" in logs
assert "socket = /tmp/uwsgi.sock" in logs
assert "chown-socket = nginx:nginx" in logs
assert "chmod-socket = 664" in logs
assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs
assert "need-app = true" in logs
assert "die-on-term = true" in logs
assert "show-config = true" in logs
assert "wsgi-file = /app/main.py" in logs
assert "processes = 16" in logs
assert "cheaper = 2" in logs
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert (
"Running inside /app/prestart.sh, you could add migrations to this file" in logs
)
assert "spawned uWSGI master process" in logs
assert "spawned uWSGI worker 1" in logs
assert "spawned uWSGI worker 2" in logs
assert "spawned uWSGI worker 3" not in logs
assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs
assert "success: nginx entered RUNNING state, process has stayed up for" in logs
assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs
示例14: verify_container
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def verify_container(container: Container, response_text: str) -> None:
response = requests.get("http://127.0.0.1:8000")
assert response.text == response_text
nginx_config = get_nginx_config(container)
assert "client_max_body_size 0;" in nginx_config
assert "worker_processes 1;" in nginx_config
assert "listen 8080;" in nginx_config
assert "worker_connections 1024;" in nginx_config
assert "worker_rlimit_nofile;" not in nginx_config
assert "daemon off;" in nginx_config
assert "include uwsgi_params;" in nginx_config
assert "uwsgi_pass unix:///tmp/uwsgi.sock;" in nginx_config
logs = get_logs(container)
assert "getting INI configuration from /application/custom_app/uwsgi.ini" in logs
assert "getting INI configuration from /etc/uwsgi/uwsgi.ini" in logs
assert "ini = /application/custom_app/uwsgi.ini" in logs
assert "ini = /etc/uwsgi/uwsgi.ini" in logs
assert "socket = /tmp/uwsgi.sock" in logs
assert "chown-socket = nginx:nginx" in logs
assert "chmod-socket = 664" in logs
assert "hook-master-start = unix_signal:15 gracefully_kill_them_all" in logs
assert "need-app = true" in logs
assert "die-on-term = true" in logs
assert "show-config = true" in logs
assert "wsgi-file = /application/custom_app/main.py" in logs
assert "processes = 16" in logs
assert "cheaper = 2" in logs
assert "Checking for script in /app/prestart.sh" in logs
assert "Running script /app/prestart.sh" in logs
assert "custom prestart.sh running" in logs
assert "spawned uWSGI master process" in logs
assert "spawned uWSGI worker 1" in logs
assert "spawned uWSGI worker 2" in logs
assert "spawned uWSGI worker 3" not in logs
assert 'running "unix_signal:15 gracefully_kill_them_all" (master-start)' in logs
assert "success: nginx entered RUNNING state, process has stayed up for" in logs
assert "success: uwsgi entered RUNNING state, process has stayed up for" in logs
示例15: get_logs
# 需要导入模块: from docker.models import containers [as 别名]
# 或者: from docker.models.containers import Container [as 别名]
def get_logs(container: Container) -> str:
logs = container.logs()
return logs.decode("utf-8")