Docker: Pull Image Before Container Create

The image needs to be pulled so that it's available for
subsequent inspect and create calls.

Change-Id: I7f2731911a3fc71fbbf3b408b39e57b7521db81f
Closes-bug: #1408101
This commit is contained in:
Andrew Melton 2015-01-06 13:20:59 -08:00
parent c22e2fa328
commit 46ebff53e6
3 changed files with 74 additions and 2 deletions

View File

@ -0,0 +1,25 @@
# Copyright 2015 Rackspace All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
def parse_docker_image(image_id):
image_parts = image_id.split(':', 1)
image_repo = image_parts[0]
image_tag = None
if len(image_parts) > 1:
image_tag = image_parts[1]
return image_repo, image_tag

View File

@ -15,6 +15,7 @@
from docker import errors
from oslo.config import cfg
from magnum.common import docker_utils
from magnum.conductor.handlers.common import docker_client
from magnum.openstack.common import log as logging
@ -74,11 +75,14 @@ class Handler(object):
# Container operations
def container_create(self, ctxt, name, container_uuid, container):
image_id = container.image_id
LOG.debug('Creating container with image %s name %s'
% (container.image_id, name))
% (image_id, name))
try:
image_repo, image_tag = docker_utils.parse_docker_image(image_id)
self.docker.pull(image_repo, tag=image_tag)
self.docker.inspect_image(self._encode_utf8(container.image_id))
self.docker.create_container(container.image_id, name=name,
self.docker.create_container(image_id, name=name,
hostname=container_uuid)
return container
except errors.APIError as api_error:

View File

@ -0,0 +1,43 @@
# Copyright 2015 Rackspace All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from magnum.conductor.handlers import docker_conductor
from magnum.tests import base
class TestDockerConductor(base.BaseTestCase):
def setUp(self):
super(TestDockerConductor, self).setUp()
with mock.patch.object(docker_conductor,
'docker_client') as mock_client:
mock_client.DockerHTTPClient.return_value = mock.MagicMock()
self.conductor = docker_conductor.Handler()
self.mock_client = self.conductor.docker
def test_container_create(self):
mock_container = mock.MagicMock()
mock_container.image_id = 'test_image:some_tag'
self.conductor.container_create(None, 'some-name', 'some-uuid',
mock_container)
utf8_image_id = self.conductor._encode_utf8(mock_container.image_id)
self.mock_client.pull.assert_called_once_with('test_image',
tag='some_tag')
self.mock_client.inspect_image.assert_called_once_with(utf8_image_id)
self.mock_client.create_container(mock_container.image_id,
name='some-name',
hostname='some-uduid')