Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

Commit

Permalink
Add dockerfile to image, support labels, add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
andsens committed Dec 13, 2015
1 parent 7393d7c commit 5dada60
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
53 changes: 53 additions & 0 deletions bootstrapvz/providers/docker/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Docker
======

The `Docker <https://www.docker.com/>`__ provider creates a docker image
from scratch, creates a Dockerfile for it and imports the image to a repo
specified in the manifest.

In order to reduce the size of the image, it is highly recommend
to make use of the `minimize_size <../../plugins/minimize_size>`__ plugin.
With optimal settings a 64-bit jessie image can be whittled down to 81.95 MB
(built on Dec 13th 2015 with ``manifests/examples/docker/minimized-jessie.yml``).


Manifest settings
-----------------

Provider
~~~~~~~~

- ``repository``: Repository to which the image should be imported.
``required``

- ``tag``: Name with which the image should be tagged.
``required``

- ``dockerfile``: Inline dockerfile that should be appended to
the one created by the bootstrapper.
``optional``

- ``labels``: Labels that should be added to the dockerfile.
The image name specified at the top of the manifest
will be added as the label ``name``.
Check out the `docker docs <https://docs.docker.com/engine/userguide/labels-custom-metadata/>`__
for more information about custom labels.
`Project atomic <http://www.projectatomic.io/>`__
also has some `useful recommendations <https://github.com/projectatomic/ContainerApplicationGenericLabels>`__
for generic container labels.
``optional``
``manifest vars``

Example:

.. code:: yaml
---
provider:
name: docker
repository: bootstrap-vz
tag: latest
dockerfile: >
CMD /bin/bash
labels:
description: Debian {system.release} {system.architecture}
2 changes: 2 additions & 0 deletions bootstrapvz/providers/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def resolve_tasks(taskset, manifest):
taskset.update(task_groups.cleanup_group)

taskset.update([tasks.commands.AddRequiredCommands,
tasks.image.PopulateDockerfileLabels,
tasks.image.CreateDockerfile,
tasks.image.CreateImage,
])

Expand Down
15 changes: 15 additions & 0 deletions bootstrapvz/providers/docker/manifest-schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ properties:
type: string
tag:
type: string
labels:
type: object
properties:
# https://github.com/projectatomic/ContainerApplicationGenericLabels
distribution-scope:
type: string
enum:
- private
- authoritative-source-only
- restricted
- public
patternProperties:
^.+$: {type: string}
dockerfile:
type: string
required: [repository, tag]
system:
type: object
Expand Down
48 changes: 47 additions & 1 deletion bootstrapvz/providers/docker/tasks/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,62 @@
from bootstrapvz.common.tools import log_check_call


class PopulateDockerfileLabels(Task):
description = 'Populating dockerfile labels'
phase = phases.image_registration

@classmethod
def run(cls, info):
import pyrfc3339
from datetime import datetime
import pytz
labels = {}
labels['name'] = info.manifest.name.format(**info.manifest_vars)
# Inspired by https://github.com/projectatomic/ContainerApplicationGenericLabels
# See here for the discussion on the debian-cloud mailing list
# https://lists.debian.org/debian-cloud/2015/05/msg00071.html
labels['architecture'] = info.manifest.system['architecture']
labels['build-date'] = pyrfc3339.generate(datetime.utcnow().replace(tzinfo=pytz.utc))
if 'labels' in info.manifest.provider:
for label, value in info.manifest.provider['labels'].items():
labels[label] = value.format(**info.manifest_vars)
info._docker['dockerfile_labels'] = labels


class CreateDockerfile(Task):
description = 'Creating dockerfile'
phase = phases.image_registration
predecessors = [PopulateDockerfileLabels]

@classmethod
def run(cls, info):
# pipes.quote converts newlines into \n rather than just prefixing
# it with a backslash, so we need to escape manually
def escape(value):
value = value.replace('"', '\\"')
value = value.replace('\n', '\\\n')
value = '"' + value + '"'
return value
labels = []
for label, value in info._docker['dockerfile_labels'].items():
labels.append(label + '=' + escape(value))
# Add some nice newlines and indentation
info._docker['dockerfile'] = 'LABEL ' + ' \\\n '.join(labels) + '\n'
if 'dockerfile' in info.manifest.provider:
info._docker['dockerfile'] += info.manifest.provider['dockerfile'] + '\n'


class CreateImage(Task):
description = 'Creating docker image'
phase = phases.image_registration
predecessors = [CreateDockerfile]

@classmethod
def run(cls, info):
from pipes import quote
tar_cmd = ['tar', '--create', '--numeric-owner',
'--directory', info.volume.path, '.']
docker_cmd = ['docker', 'import', '-',
docker_cmd = ['docker', 'import', '--change', info._docker['dockerfile'], '-',
info.manifest.provider['repository'] + ':' + info.manifest.provider['tag']]
cmd = ' '.join(map(quote, tar_cmd)) + ' | ' + ' '.join(map(quote, docker_cmd))
[info._docker['container_id']] = log_check_call(cmd, shell=True)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ provider:
name: docker
repository: bootstrap-vz
tag: latest
labels:
description: Debian {system.release} {system.architecture}
dockerfile: >
CMD /bin/bash
bootstrapper:
workspace: /target
variant: minbase
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def find_version(path):
'pyyaml >= 3.10',
'boto >= 2.14.0',
'docopt >= 0.6.1',
'pyrfc3339 >= 1.0',
],
license='Apache License, Version 2.0',
description='Bootstrap Debian images for virtualized environments',
Expand Down

0 comments on commit 5dada60

Please sign in to comment.