How-to: Create Docker image from Dockerfile

Overview

This example illustrates how the Docker plugin can be used to create a tagged Docker image from an existing image.

Part of the default configuration

This example named docker-003-image-from-dockerfile-centos, including all configuration files, is included in the default configuration which is created by Pineapple, so there is no need to create it by hand.

Define the module

Pineapple's unit of work is modules. A module is a self contained unit which can contain models, scripts and binaries. Models serves to specify test cases, deployment of applications, configuration of devices or execution of scripts.

The default directory for modules is ${user.home}/.pineapple/modules so we will create a module named docker-003-image-from-dockerfile-centos there. The module for this example will end up with the structure:

docker-003-image-from-dockerfile-centos
 |
 +--- models     
 |     +--- linux-vagrant.xml 
 +--- dockersrc  
       +--- Dockerfile

Define the module model

The model file for definition of the image:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mmd:models xmlns:mmd="http://pineapple.dev.java.net/ns/module_model_1_0"       
    xmlns:dkp="http://pineapple.dev.java.net/ns/plugin/docker_1_0" >
    <mmd:model target-resource="docker-node" description="Define tagged Docker image: pineapple/centos from Dockerfile" >
        <mmd:content>
            <dkp:docker>
              <dkp:image-from-dockerfile source-directory="modulepath:dockersrc" pull-image="false" >
                <dkp:target-image repository="pineapple/centos" tag="latest" />
              </dkp:image-from-dockerfile>
            </dkp:docker>                       
        </mmd:content>
    </mmd:model>
</mmd:models>

The configuration details

The model file

The used schemas

Two schema are used in the model file. The http://pineapple.dev.java.net/ns/module_model_1_0 is used to define the namespace mmd which defines the general infrastructure for models. The http://pineapple.dev.java.net/ns/plugin/docker_1_0 schema is used to define the namespace dkp which is used to define the model for the Docker plugin. Since multiple schemas are used to define the model file, the elements are qualified.

The model file contains two models

The model file contains two model elements each of which defines a model. Both models defines usage of the Docker plugin:

  • The first model creates an image from a Dockerfile. The image is created from a centos:latest image.
  • The second models defines a container using the image create from the Dockerfile.
Targeting the models

The target-resource attribute defines a reference to the resource which is targeted when the model executed. In this case, the value docker-node is a reference to a resource which defines a Docker daemon.

The Docker root element

The dkp:docker element defines the root of model for the Docker plugin.

Definition of the image

The dkp:image-from-dockerfile element defines a tagged Docker image which can be used for creation or deletion depending on the invoked operation:

When the model is invoked with the deploy-configuration operation, then target image is created from the source image. The source image is defined by the DockerFile (detail below).

When the image is created then source directory, defined by the source-directory attribute on element dkp:image-from-dockerfile, is compressed into a TAR archive and uploaded to Docker. Please notice that source directory is defined using the variable modulepath:dockersrc which is resolved at runtime to the directory dockersrc within the module.

Docker unpacks the archive and looks for a Dockerfile in the root of the archive. The Dockerfile along with other file material in the archive is used to create the image.

The name of the target image is defined by the dkp:target-image element.

The Dockerfile

The Dockerfile contains:

# Pull base image
# ---------------
FROM centos:latest

# Maintainer
# ----------
MAINTAINER Allan Thrane Andersen <einheriii@gmail.com>

The FROM command specifies that the image centos:latest should be pulled from DockerHub.

The MAINTAINER command declares the proud author.

Invoke Pineapple to execute model

Start Pineapple:

  • Select the module named docker-003-image-from-dockerfile-centos
  • Select the linux-vagrant model.
  • Invoke the deploy-configuration to create the tagged Docker Image named pineapple/centos:latest from centos:latest defined in the Dockerfile.