Models & Data Structures
This outline provides insights into the models and data structures used within the Docker SDK, helping developers understand the representation of Docker objects within the library.
Why Use Models?
Models offer a structured approach to representing Docker objects, enhancing readability and simplifying interactions with the Docker API. By encapsulating data and providing methods for accessing and manipulating it, they streamline the process of working with Docker components.
Key Models and Their Purpose
Container: Represents a Docker container. It encapsulates attributes like image name, command, ports, and environment variables. Source: docker/models/containers.py
# Example of using Container model from docker.models.containers import Container container = Container.create(image='ubuntu:latest', command=['echo', 'Hello, Docker!']) container.start() container.wait() container.logs()
Image: Represents a Docker image. It stores information about the image, including its tag, repository, and size. Source: docker/models/images.py
# Example of using Image model from docker.models.images import Image image = Image.build(path='.', tag='my-app:latest') image.push(repository='my-docker-hub-repo', tag='latest')
Network: Represents a Docker network. It encapsulates network configuration details, including name, driver, and connectivity settings. Source: docker/models/networks.py
# Example of using Network model from docker.models.networks import Network network = Network.create(name='my-network', driver='bridge') container = Container.create(image='ubuntu:latest', network=network.id) container.start()
Volume: Represents a Docker volume. It holds data that persists even after containers using it are removed. Source: docker/models/volumes.py
# Example of using Volume model from docker.models.volumes import Volume volume = Volume.create(name='my-volume') container = Container.create(image='ubuntu:latest', volumes={'my-volume': {'bind': '/data'}}) container.start()
Benefits of Using Models
- Abstraction: Models provide a higher-level abstraction, shielding users from the intricacies of the underlying Docker API. This abstraction simplifies interactions, enhancing code readability.
- Data Consistency: Models ensure consistent data representation across the SDK, making data handling more reliable.
- Code Reusability: Models promote code reusability by encapsulating common Docker operations, reducing redundancy and improving maintainability.
Accessing Model Data
Model data is accessible through various methods:
- Attributes: Accessing data through attributes, such as
container.image
orimage.tag
. - Methods: Using methods to retrieve or manipulate data. For example,
container.logs()
orimage.history()
.
Example:
from docker.models.containers import Container
# Create a container
container = Container.create(image='ubuntu:latest', command=['echo', 'Hello, Docker!'])
# Access container attributes
print(container.image) # Output: ubuntu:latest
print(container.command) # Output: ['echo', 'Hello, Docker!']
# Start and wait for the container
container.start()
container.wait()
# Get container logs
logs = container.logs()
print(logs.decode())
Note: The docker-py
project is an active one, and models may evolve over time. Refer to the official documentation for up-to-date information on available models and their attributes.