Docker Deployment¶
DockerResourceManager is a utility for managing Docker resources, including containers and images. It provides a command-line interface (CLI) for various Docker operations, such as listing, inspecting, creating, starting, and stopping containers, as well as managing images.
This class uses the Docker SDK for Python to interact with the Docker daemon, offering a convenient way to manage Docker containers and images from the command line.
CLI Usage
genius docker sub-command
Sub-commands
- list_containers: List all containers, with an option to include stopped containers.
genius docker list_containers [--all]
- inspect_container: Inspect a specific container by its ID.
genius docker inspect_container <container_id>
- create_container: Create a new container with specified image, command, and other parameters.
genius docker create_container <image> [options]
- start_container: Start a container by its ID.
genius docker start_container <container_id>
- stop_container: Stop a container by its ID.
genius docker stop_container <container_id>
- list_images: List all Docker images available on the local system.
genius docker list_images
- inspect_image: Inspect a specific image by its ID.
genius docker inspect_image <image_id>
- pull_image: Pull an image from a Docker registry.
genius docker pull_image <image>
- push_image: Push an image to a Docker registry.
genius docker push_image <image>
Each sub-command supports various options to specify the details of the container or image operation, such as environment variables, port mappings, volume mappings, and more.
Attributes:
Name | Type | Description |
---|---|---|
client |
The Docker client connection to interact with the Docker daemon. |
|
log |
Logger for the class to log information, warnings, and errors. |
|
console |
Rich console object to print formatted and styled outputs. |
Methods
- connect: Method to establish a connection to the Docker daemon.
- list_containers: Method to list all containers, with an option to include stopped ones.
- inspect_container: Method to inspect details of a specific container.
- create_container: Method to create a new container with given parameters.
- start_container: Method to start a specific container.
- stop_container: Method to stop a specific container.
- list_images: Method to list all Docker images.
- inspect_image: Method to inspect a specific image.
- pull_image: Method to pull an image from a Docker registry.
- push_image: Method to push an image to a Docker registry.
Note
- Ensure that the Docker daemon is running and accessible at the specified URL.
- Make sure to have the necessary permissions to interact with the Docker daemon and manage containers and images.
__init__()
¶
Initialize the Docker Resource Manager.
connect(base_url='unix://var/run/docker.sock')
¶
Connect to the Docker daemon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_url |
str
|
URL to the Docker daemon. |
'unix://var/run/docker.sock'
|
create_container(image, command=None, name=None, env_vars=None, ports=None, volumes=None, **kwargs)
¶
Create a new container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str
|
Name of the image to create the container from. |
required |
command |
Optional[str]
|
Command to run in the container. |
None
|
name |
Optional[str]
|
Name of the container. |
None
|
env_vars |
Optional[Dict[str, str]]
|
Environment variables. |
None
|
ports |
Optional[Dict[str, str]]
|
Port mappings. |
None
|
volumes |
Optional[Dict[str, Dict[str, str]]]
|
Volume mappings. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
ID of the created container. |
create_parser(parser)
¶
Create a parser for CLI commands.
Returns:
Name | Type | Description |
---|---|---|
ArgumentParser |
ArgumentParser
|
The parser for Docker operations. |
inspect_container(container_id)
¶
Inspect a specific container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container_id |
str
|
ID of the container to inspect. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Container details. |
inspect_image(image_id)
¶
Inspect a specific image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_id |
str
|
ID of the image to inspect. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Image details. |
list_containers(all_containers=False)
¶
List all containers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
all_containers |
bool
|
Flag to list all containers, including stopped ones. |
False
|
Returns:
Type | Description |
---|---|
List[Any]
|
List[Any]: List of containers. |
list_images()
¶
List all Docker images.
Returns:
Type | Description |
---|---|
List[Any]
|
List[Any]: List of images. |
pull_image(image)
¶
Pull an image from a Docker registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str
|
Name of the image to pull. |
required |
push_image(image)
¶
Push an image to a Docker registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str
|
Name of the image to push. |
required |
run(args)
¶
Run the Docker Resource Manager based on the parsed CLI arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
Namespace
|
The parsed CLI arguments. |
required |
start_container(container_id)
¶
Start a container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container_id |
str
|
ID of the container to start. |
required |
stop_container(container_id)
¶
Stop a container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container_id |
str
|
ID of the container to stop. |
required |