Docker

class dna.utils.Docker[source]

Various utilities to interface with Docker

network_exists(name)[source]

Return whether the internal network called name exists

Parameters

name (str) – the network name to search for

Returns

a boolean representing whether the network exists

create_network(name)[source]

Return an internal network called name, creating it if it doesn’t already exist

Parameters

name (str) – the name of the network to create

Returns

the created Network

get_network(name, low_level=False)[source]

Return the requested network, or its details

Parameters
  • name (str) – the name of the network to return

  • low_level – flag to return a details dictionary instead of a Network object

Returns

the requested Network, or a dictionary representing it

make_mount(host, internal)[source]

Create a mount object representing a binding between host and internal

Parameters
  • host (str) – the path on the host machine to bind

  • internal (str) – the path inside the container to bind to

Returns

a relevant Mount object

image_exists(name)[source]

Return whether the image tagged name exists

Parameters

name (str) – the image tag to search for

Note

If name does not contain a tag, the :latest tag is automatically used.

Returns

a boolean representing whether the image exists

pull_image(image, tag=None)[source]

Pull and save a Docker image by name or URL

Parameters
  • image (str) – the name or url of the image to pull

  • tag (str) – the initial tag to assign to the image (defaults to None, which assigns that tag latest)

pull_image_stream(image, tag=None)[source]

Pull and save a Docker image by name or URL and stream the output

Parameters
  • image (str) – the name or url of the image to pull

  • tag (str) – the initial tag to assign to the image (defaults to None, which assigns that tag latest)

Yields

dictionaries representing each streamed output line

build_image(**options)[source]

Build a Docker image using the given options

Parameters

options (kwargs) – options to use to the build the image (ideally contains at least a path to a build context, as well as a Dockerfile)

build_image_stream(**options)[source]

Build a Docker image using the given options and stream the output

Parameters

options (kwargs) – options to use to the build the image (ideally contains at least a path to a build context, as well as a Dockerfile)

Yields

dictionaries representing each streamed output line

prune_images()[source]

Remove all dangling images

container_exists(name)[source]

Return whether the container called name exists

Parameters

name (str) – the container name to search for

Returns

a boolean representing whether the container exists

run_image(img, name, **options)[source]

Run the requested image as a container

Parameters
  • img (str) – the name of the image to run

  • name (str) – the name to assign to the target container

  • options (kwargs) – additional options to pass into Docker

Returns

the created Container

start_container(name)[source]

Start the requested container, if it is not running

Parameters

name (str) – the name of the container to start

Returns

whether the container was started successfully

stop_container(name)[source]

Stop the requested container, if it is not stopped

Parameters

name (str) – the name of the container to stop

Returns

whether the container was stopped successfully

wipe_container(name)[source]

Kill and remove the container named name, if needed, and return it if it existed

Parameters

name (str) – the name of the container to act on

Returns

the removed Container if it existed, else the string “not found”

exec_command(con, command, **options)[source]

Run a command on the given container

Parameters
  • con (str or Container) – the (name of the) container to run the command on

  • command (str) – the command to run

  • options (kwargs) – additional options to pass into Docker

Returns

namedtuple('ExecResult', 'exit_code,output')

service_logs(con)[source]

Get the most recent 100 logs for con

Parameters

con (str) – the name of the container

Returns

a string of log messages