Examples¶
Run a container¶
For running a container, you need to have image initialized. Image holds information of repository and tag and provides other methods. To run image using docker binary (as user would) use conu.apidefs.image.Image.run_via_binary()
method with conu.backend.docker.container.DockerRunBuilder
as parameter.
from conu import DockerBackend
from conu.helpers import get_container_output
with DockerBackend() as backend:
# This will run the container using the supplied command, collects output and
# cleans the container
output = get_container_output(backend, "fedora", ["ls", "-1", "/etc"],
image_tag="27")
assert "passwd" in output
Wait for service to be ready¶
conu.backend.docker.container.DockerContainer.wait_for_port()
tries to reach 8080 till it’s opened. You can use your own timeout to limit time spent on waiting.
from conu import DockerBackend, DockerRunBuilder
with DockerBackend() as backend:
image = backend.ImageClass('centos/httpd-24-centos7')
additional_opts = ["-p", "8080:8080"]
container = image.run_via_binary(additional_opts=additional_opts)
container.wait_for_port(port=8080, timeout=-1)
container.stop()
container.delete()
Extend image using source-to-image¶
Extends acts as s2i binary. It extends builder image in form of conu.backend.docker.image.S2IDockerImage
using provided source and desired name of resulting image.
from conu import S2IDockerImage, DockerBackend
# to make sure that temporary directory is cleaned
with DockerBackend():
source = 'https://github.com/dbarnett/python-helloworld'
image = S2IDockerImage("centos/python-35-centos7")
extended_image = image.extend(source, "myapp")
container = image.run_via_binary()
container.stop()
container.delete()