container
Container
Container(image: Image, client: Optional[DockerClient] = None)
A class to manage a Docker container.
This class provides methods to create a container with specified runtime configurations, manage network connections, and control the container's lifecycle (start, stop, remove, logs, wait).
Attributes:
-
_image(Image) –The Docker image object to be used.
-
_client(DockerClient) –The Docker client instance.
-
_container(Optional[Container]) –The underlying Docker container object.
Initialize a Container instance.
Parameters:
-
image(Image) –The Image object that provides the Docker image details.
-
client(Optional[DockerClient], default:None) –An optional Docker client instance. If not provided, docker.from_env() is used.
Examples:
>>> from ures.docker.image import Image
>>> img = Image("myapp")
>>> container = Container(img)
Source code in ures/docker/container.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
image_name
property
image_name: str
Retrieve the full image name (including tag).
Returns:
-
str(str) –The full image name.
Examples:
>>> container.image_name
'myapp:latest'
is_created
property
is_created: bool
Check whether the container has been created.
Returns:
-
bool(bool) –True if the container exists, False otherwise.
Examples:
>>> container.is_created
False
status
property
status: str
Get the current status of the container.
Returns:
-
str(str) –The container status. If not found, returns "removed".
Examples:
>>> status = container.status
>>> status in ["created", "running", "exited", "removed"]
True
exit_code
property
exit_code: int | None
Retrieve the exit code of the container's last run.
Returns:
-
int | None–The exit code if Docker still knows the container, otherwise None.
Examples:
>>> code = container.exit_code
>>> isinstance(code, int) or code is None
True
is_running
property
is_running: bool
Check if the container is currently running.
Returns:
-
bool(bool) –True if running, False otherwise.
Examples:
>>> container.is_running
True
create
create(config: RuntimeConfig, tag: Optional[str] = None)
Create a Docker container using the provided runtime configuration.
Parameters:
-
config(RuntimeConfig) –The runtime configuration for the container.
-
tag(Optional[str], default:None) –An optional image tag override. Defaults to None.
Examples:
>>> container.create(runtime_config)
>>> container.is_created
True
Source code in ures/docker/container.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
stop
stop()
Stop the running container.
Examples:
>>> container.stop()
Source code in ures/docker/container.py
283 284 285 286 287 288 289 290 291 292 | |
remove
remove()
Remove the container.
Examples:
>>> container.remove()
Source code in ures/docker/container.py
294 295 296 297 298 299 300 301 302 303 304 | |
logs
logs() -> bytes
Retrieve logs from the container.
Returns:
-
bytes–Raw log output from the Docker SDK.
Examples:
>>> logs = container.logs()
>>> isinstance(logs, bytes)
True
Source code in ures/docker/container.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
wait
wait()
Wait for the container to finish execution.
Blocks until the Docker container stops. The Docker SDK result is not
returned; inspect exit_code afterwards if you need the status.
Examples:
>>> container.wait()
Source code in ures/docker/container.py
322 323 324 325 326 327 328 329 330 331 332 333 334 | |
run
run()
Start the container if it has been created and is not already running.
Raises:
-
RuntimeError–If the container has not been created or is already running.
Examples:
>>> container.run()
>>> container.is_running
True
Source code in ures/docker/container.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |