This example illustrates how the Docker plugin can be used to create a Docker container.
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.
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:
This is relevant since Pineapple will access the Docker daemon to create the container.
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
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>
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 model elements each of which defines a model. Both models defines usage of the Docker plugin:
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.
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 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.