From 24a98b0552c2972b550d7f6a8a903888033d8b94 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Wed, 18 Dec 2013 11:37:51 +0000 Subject: [PATCH] Pull images if they do not exist --- plum/service.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plum/service.py b/plum/service.py index 4f2a1083b..58dc8294d 100644 --- a/plum/service.py +++ b/plum/service.py @@ -1,3 +1,4 @@ +from docker.client import APIError import logging import re @@ -34,8 +35,19 @@ class Service(object): self.stop_container() def create_container(self, **override_options): + """ + Create a container for this service. If the image doesn't exist, attempt to pull + it. + """ container_options = self._get_container_options(override_options) - return self.client.create_container(**container_options) + try: + return self.client.create_container(**container_options) + except APIError, e: + if e.response.status_code == 404 and e.explanation and 'No such image' in e.explanation: + log.info('Pulling image %s...' % container_options['image']) + self.client.pull(container_options['image']) + return self.client.create_container(**container_options) + raise def start_container(self, container=None, **override_options): container_options = self._get_container_options(override_options)