How-to: Create Docker container

Overview

This example illustrates how the Docker plugin can be used to create a Docker container.

Part of the default configuration

This example named docker-002-container-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.

Installation of Docker

This example requires the presence of a Docker daemon. The example How-to: Install latest Docker version in a Vagrant box with CentOS 7.6 using SSH. describes how Pineapple can be used to install Docker on a Vagrant box for the purpose of the example.

For the remaining part of this example is assumed that Docker is installed using the above example. The assumed configuration is:

  • The Vagrant box is running CentOS 7.6 and has IP address 192.168.34.10.
  • The Docker daemon is accessible at 192.168.34.10:8082.

This is relevant since Pineapple will access the Docker daemon to create the container.

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-002-container-centos there. The module for this example will end up with the structure:

docker-002-container-centos
 |
 +--- models     
       +--- linux-vagrant.xml 

Define the module model

The model file for definition of the image and the container:

<?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" 
        target-operation="deploy-configuration" description="Define Docker image: centos:latest" >
        <mmd:content>
            <dkp:docker>
              <dkp:image repository="centos" tag="latest" />            
            </dkp:docker>                       
        </mmd:content>
    </mmd:model>
    <mmd:model target-resource="docker-node" description="Define Docker container" >
        <mmd:content>
            <dkp:docker>
               <dkp:container name="alpha01" >
                   <dkp:image repository="centos" tag="latest" />                    
               </dkp:container>
            </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 defines a centos:latest image.
  • The second models defines a container using the centos:latest image.
Targeting the models

The target-resource attribute defined on each model element declares 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 at 192.168.34.10:8082.

The Docker root element

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

Definition of the image

The first model is defined with target-operation="deploy-configuration" which specifies that the model should only be executed when it is invoked with the deploy-configuration operation. When the model is invoked with another operation then this model is ignored, i.e. the image isn't deleted.

The dkp:image element defines a centos:latest image which pulled when the deploy-configuration operation is invoked.

Definition of the container

The second model contains the dkp:container element which defines a Docker container. When the model is invoked with the deploy-configuration operation then the container is created. When the model is invoked with the undeploy-configuration operation then the container is deleted.

The container name

The container element has a name attribute which is a mandatory in Pineapple. After the container is created by Pineapple then it is renamed to the name specified by the attribute. By default containers are primarily identified by ID in Docker, but since Pineapple is stateless, then there is no way for Pineapple to maintain the container ID across invocation of a model. Usage of a container name solves that problem.

The container base image

The container element contains a mandatory image element. The element defines from which image the container is created. The image used by container, is the container defined in the first model.

Invoke Pineapple to execute model

Start your Pineapple client of choice:

  • Select the module named docker-002-container-centos
  • Select the linux-vagrant model.
  • Invoke the deploy-configuration to create the container.