added api reference, fixed tech review comments

This commit is contained in:
usha-mandya 2019-05-14 11:52:27 +01:00
parent 879b1fd213
commit 41759784ed
2 changed files with 328 additions and 80 deletions

View File

@ -0,0 +1,161 @@
---
title: Docker Template API reference
description: Docker Template API reference
keywords: application, template, API, definition
---
This page contains information about the Docker Template API reference.
## Service template definition
The following section provides information about the valid parameters that you can use when you create a service template definition.
```
apiVersion: v1alpha1
kind: ServiceTemplate
metadata:
name: angular
platforms:
- linux
spec:
title: Angular
description: Angular service
icon: https://cdn.worldvectorlogo.com/logos/angular-icon-1.svg
source:
image: docker.io/myorg/myservice:version
parameters:
- name: node
description: Node version
type: enum
defaultValue: "9"
values:
- value: "10"
description: "10"
- value: "9"
description: "9"
- value: "8"
description: "8"
- name: externalPort
description: External port
defaultValue: "8080"
type: hostPort
```
### root
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| apiVersion |yes | The api format version. Current latest is v1alpha1|
|kind| yes|The kind of object. Must be `ServiceTemplate` For services templates.|
### metadata
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
|name |yes | The identifier for this service. Must be unique within a given library. |
|platform| yes|A list of allowed target platforms. Possible options are `windows` and `linux`|
### spec
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| title |yes |The label for this service, as displayed when listed in `docker template` commands or in the `application-designer`|
|description| no|A short description for this service|
|icon|no|An icon representing the service. Only used in the Application Designer|
### spec/source
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| image |yes| The name of the image associated with this service template. Must be in full `repo/org/service:version` format|
### spec/parameters
The parameters section allows to specify the input parameters that are going to be used by the service.
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
|name |yes| The identifier for this parameter. Must be unique within the service parameters. |
|description| no|A short description of the parameter. Will be used as label in the Application Designer|
|type| yes|The type of the parameter. Possible options are:<ul><li>`string` - The default type, with no validation or specific features.</li><li>`enum` - Allow the user to choose a value included in a specific list of options. Must specify the values parameter.</li><li>`hostPort` - Specify that this parameter is a port that is going to be exposed. Use port format regexp validation, and avoid duplicate ports within an application.</li></ul>|
|defaultValue| yes|The default value for this parameter. For enum type, must be a valid value from the values list.|
|values| no|For enum type, specify a list of value with a value/description tuple.|
## Application template definition
The following section provides information about the valid parameters that you can use when you create a application template definition.
```
apiVersion: v1alpha1
kind: ApplicationTemplate
metadata:
name: nginx-flask-mysql
platforms:
- linux
spec:
title: Flask / NGINX / MySQL application
description: Sample Python/Flask application with an Nginx proxy and a MySQL database
services:
- name: back
serviceId: flask
parameters:
externalPort: "80"
- name: db
serviceId: mysql
- name: proxy
serviceId: nginx
```
### root
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| apiVersion |yes | The api format version. Current latest is v1alpha1|
|kind| yes|The kind of object. Must be `ApplicationTemplate` For application templates.|
### metadata
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
|name |yes | The identifier for this application template. Must be unique within a given library.|
|platform| yes|A list of allowed target platforms. Possible options are `windows` and `linux`|
### spec
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| title |yes |The label for this application template, as displayed when listed in `docker template` commands or in `application-designer` |
|description| no|A short description for this service|
### spec/services
This section lists the service templates used in the application.
| Parameter |Required? | Description |
| :----------------------|:----------------------|:----------------------------------------|
| name |yes|The name of the service. It will be used for image name and for subfolder within the application structure. |
|serviceId |yes|The id of the service to use (equivalent to the metadata/name field of the service) |
| parameters |no|A map (string to string) that can be used to override the default values of the service parameters.|
## Service configuration file
The file is mounted at `/run/configuration` in every service template container and contains the template context in a JSON format.
| Parameter |Description |
| :----------------------|:----------------------|
|ServiceId |The service id|
| name |The name of the service as specified by the application template or overridden by the user|
|parameters |A map (string to string) containing the services parameter values.|
| targetPath |The destination folder for the application on the host machine.|
|namespace |The service images namespace (org and user)|
|services |A list containing all the services of the application (see below)|
### Attributes
The items in the services list contains the following attributes:
| Parameter |Description |
| :----------------------|:----------------------|
|serviceId |The service id|
| name |The name of the service as specified by the application template or overridden by the user|
| parameters |A map (string to string) containing the services parameter values.|

View File

@ -8,23 +8,28 @@ keywords: Docker, application template, Application Designer,
Docker Template is a CLI plugin that introduces a top-level `docker template` command that allows users to create new Docker applications by using a library of templates. There are two types of templates — service templates and application templates.
- A _service template_ is a container that holds the assets such as code, Dockerfile, docker-compose.yaml to generate an application.
- An _application template_ is a collection of one or more service templates.
A _service template_ is a container image that generates code and contains the metadata associated with the image.
A Docker template contains a predefined set of service and application templates. However, you can create **custom templates** by completing the following steps:
- The container image takes `/run/configuration` mounted file as input to generate assets such as code, Dockerfile, and `docker-compose.yaml` for a given service, and writes the output to the `/project` mounted folder.
1. Create a service template
- The metadata file that describes the service template is called the service definition. It contains the name of the service, description, and available parameters such as ports, volumes, etc. For a complete list of parameters that are allowed, see [Docker Template API reference](/ee/app-template/api-reference).
An _application template_ is a collection of one or more service templates.
## Create a custom service template
A Docker template contains a predefined set of service and application templates. To create a custom template based on your requirements, you must complete the following steps:
1. Create a service container image
2. Create the service template definition
3. Create the application template definition
4. Add the template to Docker Template settings
3. Add the service template to the library
4. Share the service template
## Create a service template
A service template is a container that generates all the assets such as code, Dockerfile, docker-compose.yaml for a given service.
### Create a service container image
A service template provides the description required by Docker Template to scaffold a project. A service template runs inside a container with two bind mounts:
1. `/run/configuration`, a JSON file which contains all settings such as parameters, image name, etc.
1. `/run/configuration`, a JSON file which contains all settings such as parameters, image name, etc. For example:
```
{
@ -36,11 +41,9 @@ A service template provides the description required by Docker Template to scaff
}
```
2. `/project`, where the container will write assets to
2. `/project`, the output folder to which the container image writes the generated assets.
Docker Template converts the `docker-compose.yaml` file into a template using a Go template and aggregates the services to create the final application.
### Create a basic service template
#### Basic service template
To create a basic service template, you need to create two files — a dockerfile and a docker compose file in a new folder. For example, to create a new MySQL service template, create the following files in a folder called `my-service`:
@ -49,7 +52,7 @@ To create a basic service template, you need to create two files — a dockerfil
```
version: "3.6"
services:
{{ .Name }}:
mysql:
image: mysql
```
@ -63,13 +66,13 @@ CMD cp docker-compose.yaml /project/
This adds a MySQL service to your application.
### Create a service with code
#### Create a service with code
Services that generate a template using code must contain the following files that are valid:
- A *Dockerfile* located at the root of the `my-service` folder. This is the Dockerfile that is used for the service when running the application.
- A *docker-compose.yaml* file located at the root of the `my-service` folder. The `docker-compose.yaml` file must contain the service declaration and any optional volumes / secrets.
- A *docker-compose.yaml* file located at the root of the `my-service` folder. The `docker-compose.yaml` file must contain the service declaration and any optional volumes or secrets.
Heres an example of a simple NodeJS service:
@ -81,7 +84,7 @@ my-service
└── docker-compose.yaml # The service declaration
```
The NodeJS service contains the following contents:
The NodeJS service contains the following files:
`my-service/Dockerfile`
@ -89,6 +92,8 @@ The NodeJS service contains the following contents:
FROM alpine
COPY assets /assets
CMD ["cp", "/assets", "/project"]
FROM dockertemplate/interpolator:v0.0.8 as interpolator
COPY assets /assets
```
`my-service/assets/docker-compose.yaml`
@ -115,8 +120,6 @@ CMD ["yarn", "run", "start"]
> **Note:** After scaffolding the template, you can add the default files your template contains to the `assets` folder.
#### Build and push the template image
The next step is to build and push the service template image to a remote repository by running the following command:
```
@ -125,7 +128,7 @@ docker build -t org/my-service .
docker push org/my-service
```
To build and push the image to an instance of Docker Trusted Registry(DTR), specify a repository name:
To build and push the image to an instance of Docker Trusted Registry(DTR), or to an external registry, specify the name of the repository:
```
cd [...]/my-service
@ -133,14 +136,14 @@ docker build -t myrepo:5000/my-service .
docker push myrepo:5000/my-service
```
## Create the service template definition
### Create the service template definition
The service definition contains metadata that describes a service template. It contains the name of the service, description, and available parameters such as ports, volumes, etc.
After creating the service definition, you can proceed to [Add templates to Docker Template](#add-templates-to-docker-template) to add the service definition to the Docker Template repository.
Of all the available service and application definitions, Docker Template has access to only one catalog, referred to as the repository. It uses the catalog content to display service and application templates to the end user.
Here is an example of the Express service definition:
Here is an example of the Express service definition:
```
- apiVersion: v1alpha1 # constant
@ -157,17 +160,16 @@ Here is an example of the Express service definition:
The most important section here is `image: org/my-service:latest`. This is the image associated with this service template. You can use this line to point to any image. For example, you can use an Express image directly from the hub `docker.io/dockertemplate/express:latest` or from the DTR private repository `myrepo:5000/my-service:latest`. The other properties in the service definition are mostly metadata for display and indexation purposes.
### Adding parameters to the service
#### Adding parameters to the service
Now that you have created a simple express service, you can customize it based on your requirements. For example, you can choose the version of NodeJS to use when running the service.
To customize a service, you need to complete the following tasks:
1. Declare the parameters in the service definition. This allows [Application Designer](/ee/desktop/app-designer) to be aware of the new options.
1. Declare the parameters in the service definition. This tells Docker Template whether or not the CLI can accept the parameters, and allows the [Application Designer](/ee/desktop/app-designer) to be aware of the new options.
2. Use the parameters during service construction.
#### Declare the parameters
Add the parameters available to the application. The following example adds the NodeJS version and the external port:
@ -197,7 +199,7 @@ Add the parameters available to the application. The following example adds the
#### Use the parameters during service construction
When you run the service template container, a volume is mounted making the service parameters available at `/run/configuration`.
When you run the service template container, a volume is mounted making the service parameters available at `/run/configuration`.
The file matches the following go struct:
@ -224,13 +226,7 @@ type ConfiguredService struct {
}
```
You can then use the file to obtain values for the parameters and use this information based on your requirements. However, in most cases, the file is used to interpolate the variables. Therefore, we provide a utility called `interpolator` that expands variables in templates.
Basically, `interpolator` is an image that:
- takes a folder (assets folder) and a service parameter map as input,
- replaces variables in the input folder using the parameters specified by the user (for example, the service name, external port, etc), and
- writes the interpolated parameters to the application destination.
You can then use the file to obtain values for the parameters and use this information based on your requirements. However, in most cases, the JSON file is used to interpolate the variables. Therefore, we provide a utility called `interpolator` that expands variables in templates. For more information, see [Interpolator](#interpolator).
To use the `interpolator` image, update `my-service/Dockerfile` to use the following Dockerfile:
@ -239,7 +235,7 @@ FROM dockertemplate/interpolator:v0.0.3-beta1
COPY assets .
```
> **Note:** The interpolator tag must match the version used in docker template. Verify this using the `docker template version` command .
> **Note:** The interpolator tag must match the version used in Docker Template. Verify this using the `docker template version` command .
This places the interpolator image in the `/assets` folder and copies the folder to the target `/project` folder. If you prefer to do this manually, use a Dockerfile instead:
@ -258,38 +254,11 @@ with
Now, build and push the image to your repository.
For instructions on building and pushing an image to your repository, see [Build and push the template image](#build-and-push-the-template-image).
### Add service template to the library
## Create the application definition
You must add the service to a repository file in order to see it when you run the `docker template ls` command, or to make the service available in the Application Designer.
An application template definition contains metadata that describes an application template. It contains information such as the name and description of the template, the services it contains, and the parameters for each of the services.
Before you create an application template definition, you must create a repository that contains the services you are planning to include in the template. For more information, see [Create the repository file](#create-the-repository-file).
For example, to create an Express and MySQL application, the application definition must be similar to the following yaml file:
```
apiVersion: v1alpha1 #constant
kind: ApplicationTemplate #constant
metadata:
name: express-mysql #the name of the application
spec:
description: Sample application with a NodeJS backend and a MySQL database
services: # list of the services
- name: back
serviceId: express # service name
parameters: # (optional) define the default application parameters
externalPort: 9000
- name: db
serviceId: mysql
title: Express / MySQL application
```
## Add templates to Docker Template
You must add the service you have created to a repository file in order to see the service when running the `docker template ls` command, or to make the service available in Application Designer. The following sections contain instructions on creating a repository file and adding the repository to the Docker Template settings.
### Create the repository file
#### Create the repository file
Create a local repository file called `library.yaml` anywhere on your local drive and add the newly created service definitions and application definitions to it.
@ -306,20 +275,13 @@ services: # List of service templates available
spec:
title: Express
[...]
templates: # List of application templates available
- apiVersion: v1alpha1 #constant
kind: ApplicationTemplate # here is the application definition for our application template
metadata:
name: express-mysql
spec:
[...]
```
### Add the local repository to Docker Template settings
#### Add the local repository to docker-template settings
> **Note:** You can also use the instructions in this section to add templates to the Application Designer. For more information, see [Application Designer](/ee/desktop/app-designer).
> **Note:** You can also use the instructions in this section to add templates to the [Application Designer](/ee/desktop/app-designer).
Now that you have created a local repository and added service and application definitions to it, you must make Docker Template aware of these. To do this:
Now that you have created a local repository and added service definitions to it, you must make Docker Template aware of these. To do this:
1. Edit `~/.docker/dockertemplate/preferences.yaml` as follows:
@ -345,14 +307,139 @@ repositories:
url: https://docker-application-template.s3.amazonaws.com/master/library.yaml
```
After updating the `preferences.yaml` file, run `docker template ls` or restart the Application Designer and select Custom application. The new service should now be visible in the list of available services.
After updating the `preferences.yaml` file, run `docker template ls` or restart the Application Designer and select **Custom application**. The new service should now be visible in the list of available services.
## Sharing custom service templates
### Share custom service templates
To share a service template, you must complete the following steps:
To share a custom service template, you must complete the following steps:
1. Push the image to an available end point (you can use Docker hub)
1. Push the image to an available endpoint (for example, Docker Hub)
2. Share the service definition (for example, GitHub)
3. Ensure the receiver has modified their `preferences.yaml` file to point to the service definition that you have shared on GitHub.
3. Ensure the receiver has modified their `preferences.yaml` file to point to the service definition that you have shared, and are permitted to accept remote images.
## Create a custom application template
An application template is a collection of one or more service templates. You must complete the following steps to create a custom application template:
1. Create an application template definition
2. Add the application template to the library
3. Share your custom application template
### Create the application definition
An application template definition contains metadata that describes an application template. It contains information such as the name and description of the template, the services it contains, and the parameters for each of the services.
Before you create an application template definition, you must create a repository that contains the services you are planning to include in the template. For more information, see [Create the repository file](#create-the-repository-file).
For example, to create an Express and MySQL application, the application definition must be similar to the following yaml file:
```
apiVersion: v1alpha1 #constant
kind: ApplicationTemplate #constant
metadata:
name: express-mysql #the name of the application
spec:
description: Sample application with a NodeJS backend and a MySQL database
services: # list of the services
- name: back
serviceId: express # service name
parameters: # (optional) define the default application parameters
externalPort: 9000
- name: db
serviceId: mysql
title: Express / MySQL application
```
### Add the template to the library
Create a local repository file called `library.yaml` anywhere on your local drive. If you have already created the `library.yaml` file, add the application definitions to it.
`library.yaml`
```
apiVersion: v1alpha1
generated: "2018-06-13T09:24:07.392654524Z"
kind: RepositoryContent
services: # List of service templates available
- apiVersion: v1alpha1 # here is the service definition for our service template.
kind: ServiceTemplate
name: express
spec:
title: Express
[...]
templates: # List of application templates available
- apiVersion: v1alpha1 #constant
kind: ApplicationTemplate # here is the application definition for our application template
metadata:
name: express-mysql
spec:
```
### Add the local repository to `docker-template` settings
Now that you have created a local repository and added application definitions, you must make Docker Template aware of these. To do this:
1. Edit `~/.docker/dockertemplate/preferences.yaml` as follows:
```
apiVersion: v1alpha1
channel: master
kind: Preferences
repositories:
- name: library-master
url: https://docker-application-template.s3.amazonaws.com/master/library.yaml
```
2. Add your local repository:
```
apiVersion: v1alpha1
channel: master
kind: Preferences
repositories:
- name: custom-services # here
url: file://path/to/my/library.yaml
- name: library-master
url: https://docker-application-template.s3.amazonaws.com/master/library.yaml
```
After updating the `preferences.yaml` file, run `docker template ls` or restart the Application Designer and select **Custom application**. The new template should now be visible in the list of available templates.
### Share the custom application template
To share a custom application template, you must complete the following steps:
1. Push the image to an available endpoint (for example, Docker Hub)
2. Share the application definition (for example, GitHub)
3. Ensure the receiver has modified their `preferences.yaml` file to point to the application definition that you have shared, and are permitted to accept remote images.
## Interpolator
The `interpolator` utility is basically an image containing a binary which:
- takes a folder (assets folder) and the service parameter file as input,
- replaces variables in the input folder using the parameters specified by the user (for example, the service name, external port, etc), and
- writes the interpolated files to the destination folder.
The interpolator implementation uses [Golang template](https://golang.org/pkg/text/template/) to aggregate the services to create the final application. If your service template uses the `interpolator` image by default, it expects all the asset files to be located in the `/assets` folder:
`/interpolator -source /assets -destination /project`
However, you can create your own scaffolding script that performs calls to the `interpolator`.
> **Note:** It is not mandatory to use the `interpolator` utility. You can use a utility of your choice to handle parameter replacement and file copying to achieve the same result.
The following table lists the `interpolator` binary options:
| Parameter | Default value | Description |
| :----------------------|:---------------------------|:----------------------------------------|
| `-source` | none | Source file or folder to interpolate from|
| `-destination` | none | Destination file or folder to copy the interpolated files to|
| `-config` | `/run/configuration` | The path to the json configuration file |
| `-skip-template` | false | If set to `true`, it copies assets without any transformation |