Merge pull request #4308 from chouseknecht/devel

Add explicit image build
reviewable/pr18780/r1
Chris Houseknecht 8 years ago committed by GitHub
commit db66758125

@ -108,13 +108,14 @@ options:
default: smart default: smart
build: build:
description: description:
- Whether or not to build images before starting containers. - Use with state I(present) to always build images prior to starting the application.
- Missing images will always be built. - Same as running docker-compose build with the pull option.
- If an image is present and C(build) is false, the image will not be built. - Images will only be rebuilt if Docker detects a change in the Dockerfile or build directory contents.
- If an image is present and C(build) is true, the image will be built. - Use the C(nocache) option to ignore the image cache when performing the build.
- If an existing image is replaced, services using the image will be recreated unless C(recreate) is I(never).
type: bool type: bool
required: false required: false
default: true default: false
pull: pull:
description: description:
- Use with state I(present) to always pull images prior to starting the application. - Use with state I(present) to always pull images prior to starting the application.
@ -124,6 +125,13 @@ options:
required: false required: false
default: false default: false
version_added: "2.2" version_added: "2.2"
nocache:
description:
- Use with the build option to ignore the cache during the image build process.
type: bool
required: false
default: false
version_added: "2.2"
remove_images: remove_images:
description: description:
- Use with state I(absent) to remove the all images or only local images. - Use with state I(absent) to remove the all images or only local images.
@ -392,9 +400,35 @@ actions:
returned: always returned: always
type: complex type: complex
contains: contains:
pulled_image:
description: Provides image details when a new image is pulled for the service.
returned: on image pull
type: complex
contains:
name:
description: name of the image
returned: always
type: string
id:
description: image hash
returned: always
type: string
built_image:
description: Provides image details when a new image is built for the service.
returned: on image build
type: complex
contains:
name:
description: name of the image
returned: always
type: string
id:
desription: image hash
returned: always
type: string
action: action:
description: A descriptive name of the action to be performed on the set of containers description: A descriptive name of the action to be performed on the service's containers.
within the service.
returned: always returned: always
type: list type: list
contains: contains:
@ -475,6 +509,7 @@ class ContainerManager(DockerBaseClass):
self.scale = None self.scale = None
self.debug = None self.debug = None
self.pull = None self.pull = None
self.nocache = None
for key, value in client.module.params.items(): for key, value in client.module.params.items():
setattr(self, key, value) setattr(self, key, value)
@ -569,7 +604,7 @@ class ContainerManager(DockerBaseClass):
up_options = { up_options = {
u'--no-recreate': False, u'--no-recreate': False,
u'--build': self.build, u'--build': True,
u'--no-build': False, u'--no-build': False,
u'--no-deps': False, u'--no-deps': False,
u'--force-recreate': False, u'--force-recreate': False,
@ -589,6 +624,9 @@ class ContainerManager(DockerBaseClass):
if self.pull: if self.pull:
result.update(self.cmd_pull()) result.update(self.cmd_pull())
if self.build:
result.update(self.cmd_build())
for service in self.project.services: for service in self.project.services:
if not service_names or service.name in service_names: if not service_names or service.name in service_names:
plan = service.convergence_plan(strategy=converge) plan = service.convergence_plan(strategy=converge)
@ -607,11 +645,13 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode and result['changed']: if not self.check_mode and result['changed']:
try: try:
do_build = build_action_from_opts(up_options)
self.log('Setting do_build to %s' % do_build)
self.project.up( self.project.up(
service_names=service_names, service_names=service_names,
start_deps=start_deps, start_deps=start_deps,
strategy=converge, strategy=converge,
do_build=build_action_from_opts(up_options), do_build=do_build,
detached=detached, detached=detached,
remove_orphans=self.remove_orphans) remove_orphans=self.remove_orphans)
except Exception as exc: except Exception as exc:
@ -717,6 +757,34 @@ class ContainerManager(DockerBaseClass):
) )
return result return result
def cmd_build(self):
result = dict(
changed=False,
actions=dict(),
)
if not self.check_mode:
for service in self.project.get_services(self.services, include_deps=False):
self.log('Building image for service %s' % service.name)
if service.can_be_built():
# store the existing image ID
image = service.image()
old_image_id = None
if image and image.get('Id'):
old_image_id = image['Id']
# build the image
new_image_id = service.build(pull=True, no_cache=self.nocache)
if new_image_id not in old_image_id:
# if a new image was built
result['changed'] = True
result['actions'][service.name] = dict()
result['actions'][service.name]['built_image'] = dict(
name=service.image_name,
id=service.image()['Id']
)
return result
def cmd_down(self): def cmd_down(self):
result = dict( result = dict(
changed=False, changed=False,
@ -824,7 +892,7 @@ def main():
definition=dict(type='dict'), definition=dict(type='dict'),
hostname_check=dict(type='bool', default=False), hostname_check=dict(type='bool', default=False),
recreate=dict(type='str', choices=['always','never','smart'], default='smart'), recreate=dict(type='str', choices=['always','never','smart'], default='smart'),
build=dict(type='bool', default=True), build=dict(type='bool', default=False),
remove_images=dict(type='str', choices=['all', 'local']), remove_images=dict(type='str', choices=['all', 'local']),
remove_volumes=dict(type='bool', default=False), remove_volumes=dict(type='bool', default=False),
remove_orphans=dict(type='bool', default=False), remove_orphans=dict(type='bool', default=False),
@ -834,6 +902,7 @@ def main():
services=dict(type='list'), services=dict(type='list'),
dependencies=dict(type='bool', default=True), dependencies=dict(type='bool', default=True),
pull=dict(type='bool', default=False), pull=dict(type='bool', default=False),
nocache=dict(type='bool', default=False),
debug=dict(type='bool', default=False) debug=dict(type='bool', default=False)
) )

Loading…
Cancel
Save