Container Examples

Installing a container image and deploying it

This example shows how to get install a container image, deploy and start that container.

Note

This example works with Libcloud version 0.21.0 and above.

from libcloud.container.providers import get_driver
from libcloud.container.types import Provider

CREDS = ('user', 'api key')

Cls = get_driver(Provider.DOCKER)
driver = Cls(*CREDS)

image = driver.install_image('tomcat:8.0')
container = driver.deploy_container('tomcat', image)

container.restart()

Working with cluster supported providers

This example shows listing the clusters, find a specific named cluster and deploying a container to it.

from libcloud.container.base import ContainerImage
from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

cls = get_driver(Provider.ECS)

conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')

for cluster in conn.list_clusters():
    print(cluster.name)
    if cluster.name == 'my-cluster':
        conn.list_containers(cluster=cluster)
        container = conn.deploy_container(
            name='my-simple-app',
            image=ContainerImage(
                id=None,
                name='simple-app',
                path='simple-app',
                version=None,
                driver=conn
            ),
            cluster=cluster)

Working with docker hub

Docker Hub Client HubClient is a shared utility class for interfacing to the public Docker Hub Service.

You can use this class for fetching images to deploy to services like ECS

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver
from libcloud.container.utils.docker import HubClient

cls = get_driver(Provider.ECS)

conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')
hub = HubClient()

image = hub.get_image('ubuntu', 'latest')

for cluster in conn.list_clusters():
    print(cluster.name)
    if cluster.name == 'default':
        container = conn.deploy_container(
            cluster=cluster,
            name='my-simple-app',
            image=image)