Skip to content

Commit 2c8c441

Browse files
authored
Merge pull request #108184 from kgremban/mar16-createoptions
New article for createOptions
2 parents 718859e + 75f8738 commit 2c8c441

File tree

5 files changed

+146
-8
lines changed

5 files changed

+146
-8
lines changed

articles/iot-edge/TOC.yml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,18 @@
143143
href: how-to-auto-provision-x509-certs.md
144144
- name: Symmetric key attestation
145145
href: how-to-auto-provision-symmetric-keys.md
146-
- name: Develop and debug custom modules
146+
- name: Develop custom modules
147147
items:
148-
- name: Visual Studio 2019
148+
- name: Develop with Visual Studio 2019
149149
href: how-to-visual-studio-develop-module.md
150-
- name: Visual Studio Code
150+
- name: Develop with Visual Studio Code
151151
href: how-to-vs-code-develop-module.md
152+
- name: Configure module createOptions
153+
items:
154+
- name: Understand and use createOptions
155+
href: how-to-use-create-options.md
156+
- name: Give modules access to a device's local storage
157+
href: how-to-access-host-storage-from-module.md
152158
- name: Deploy modules
153159
items:
154160
- name: Deploy to individual devices
@@ -183,11 +189,9 @@
183189
href: deploy-modbus-gateway.md
184190
- name: Configure proxy support
185191
href: how-to-configure-proxy-support.md
186-
- name: Store data at the edge
192+
- name: Azure Blob Storage on IoT Edge
187193
items:
188-
- name: Give modules access to a device's local storage
189-
href: how-to-access-host-storage-from-module.md
190-
- name: Azure Blob Storage on IoT Edge
194+
- name: Understand blob storage at the edge
191195
href: how-to-store-data-blob.md
192196
- name: Deploy blob storage modules
193197
href: how-to-deploy-blob.md
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: Write createOptions for modules - Azure IoT Edge | Microsoft Docs
3+
description: How to use createOptions in the deployment manifest to configure modules at runtime
4+
keywords:
5+
author: kgremban
6+
manager: philmea
7+
ms.author: kgremban
8+
ms.date: 04/01/2020
9+
ms.topic: conceptual
10+
ms.service: iot-edge
11+
services: iot-edge
12+
---
13+
14+
# How to configure container create options for IoT Edge modules
15+
16+
The **createOptions** parameter in the deployment manifest enables you to configure the module containers at runtime. This parameter expands your control over the modules and allows for tasks like allowing or restricting the module's access to the host device's resources, or configuring networking.
17+
18+
IoT Edge modules are implemented as Docker-compatible containers on your IoT Edge device. Docker offers many options for creating containers, and those options apply to IoT Edge modules, too. For more information, see [Docker container create options](https://docs.docker.com/engine/api/v1.32/#operation/ContainerCreate).
19+
20+
## Format create options
21+
22+
The IoT Edge deployment manifest accepts create options formatted as JSON. For example, take the create options that are automatically included for every edgeHub module:
23+
24+
```json
25+
"createOptions": {
26+
"HostConfig": {
27+
"PortBindings": {
28+
"5671/tcp": [
29+
{
30+
"HostPort": "5671"
31+
}
32+
],
33+
"8883/tcp": [
34+
{
35+
"HostPort": "8883"
36+
}
37+
],
38+
"443/tcp": [
39+
{
40+
"HostPort": "443"
41+
}
42+
]
43+
}
44+
}
45+
}
46+
```
47+
48+
This edgeHub example uses the **HostConfig.PortBindings** parameter to map exposed ports on the container to a port on the host device.
49+
50+
If you use the Azure IoT Tools extensions for Visual Studio or Visual Studio Code, you can write the create options in JSON format in the **deployment.template.json** file. Then, when you use the extension to build the IoT Edge solution or generate the deployment manifest, it will stringify the JSON for you in the format that the IoT Edge runtime expects. For example:
51+
52+
```json
53+
"createOptions": "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}"
54+
```
55+
56+
One tip for writing create options is to use the `docker inspect` command. As part of your development process, run the module locally using `docker run <container name>`. Once you have the module working the way you want it, run `docker inspect <container name>`. This command outputs the module details in JSON format. Find the parameters that you configured, and copy the JSON. For example:
57+
58+
[ ![Results of docker inspect edgeHub](./media/how-to-use-create-options/docker-inspect-edgehub-inline-and-expanded.png) ](./media/how-to-use-create-options/docker-inspect-edgehub-inline-and-expanded.png#lightbox)
59+
60+
## Common scenarios
61+
62+
Container create options enable many scenarios, but here are some that come up most often when building IoT Edge solutions:
63+
64+
* [Give modules access to host storage](how-to-access-host-storage-from-module.md)
65+
* [Map host port to module port](#map-host-port-to-module-port)
66+
* [Restrict module memory and CPU usage](#restrict-module-memory-and-cpu-usage)
67+
68+
### Map host port to module port
69+
70+
If your module needs to communicate with a service outside of the IoT Edge solution, and isn't using message routing to do so, then you need to map a host port to a module port.
71+
72+
>[!TIP]
73+
>This port mapping is not required for module-to-module communication on the same device. If module A needs to query an API hosted on module B, it can do so without any port mapping. Module B needs to expose a port in its dockerfile, for example: `EXPOSE 8080`. Then module A can query the API using module B's name, for example: `http://ModuleB:8080/api`.
74+
75+
First, make sure that a port inside the module is exposed to listen for connections. You can do this using an [EXPOSE](https://docs.docker.com/engine/reference/builder/#expose) instruction in the dockerfile. For example, `EXPOSE 8080`. The expose instruction defaults to TCP protocol if not specified, or you can specify UDP.
76+
77+
Then, use the **PortBindings** setting in the **HostConfig** group of the [Docker container create options](https://docs.docker.com/engine/api/v1.32/#operation/ContainerCreate) to map the exposed port in the module to a port on the host device. For example, if you exposed port 8080 inside the module and want to map that to port 80 of the host device, the create options in the template.json file would look like the following example:
78+
79+
```json
80+
"createOptions": {
81+
"HostConfig": {
82+
"PortBindings": {
83+
"8080/tcp": [
84+
{
85+
"HostPort": "80"
86+
}
87+
]
88+
}
89+
}
90+
}
91+
```
92+
93+
Once stringified for the deployment manifest, the same configuration would look like the following example:
94+
95+
```json
96+
"createOptions": "{\"HostConfig\":{\"PortBindings\":{\"8080/tcp\":[{\"HostPort\":\"80\"}]}}}"
97+
```
98+
99+
### Restrict module memory and CPU usage
100+
101+
You can declare how much of the host resources a module can use. This control is helpful to ensure that one module can't consume too much memory or CPU usage and prevent other processes from running on the device. You can manage these settings with [Docker container create options](https://docs.docker.com/engine/api/v1.32/#operation/ContainerCreate) in the **HostConfig** group, including:
102+
103+
* **Memory**: Memory limit in bytes. For example, 268435456 bytes = 256 MB.
104+
* **MemorySwap**: Total memory limit (memory + swap). For example, 536870912 bytes = 512 MB
105+
* **CpuPeriod**: The length of a CPU period in microseconds. The default value is 100000 so, for example, a value of 25000 limits a container to 25% of the CPU resources.
106+
107+
In the template.json format, these values would look like the following example:
108+
109+
```json
110+
"createOptions": {
111+
"HostConfig": {
112+
"Memory": 268435456,
113+
"MemorySwap": 536870912,
114+
"CpuPeriod": 25000
115+
}
116+
}
117+
```
118+
119+
Once stringified for the final deployment manifest, these values would look like the following example:
120+
121+
```json
122+
"createOptions":"{\"HostConfig\":{\"Memory\":268435456,\"MemorySwap\":536870912,\"CpuPeriod\":25000}}"
123+
```
124+
125+
## Next steps
126+
127+
For more examples of create options in action, see the following IoT Edge samples:
128+
129+
* [Custom Vision and Azure IoT Edge on a Raspberry Pi 3](https://github.com/Azure-Samples/Custom-vision-service-iot-edge-raspberry-pi)
130+
* [Azure IoT Edge blob storage sample](https://github.com/Azure-Samples/azure-iotedge-blobstorage-sample)
165 KB
Loading

articles/iot-edge/module-composition.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Learn how a deployment manifest declares which modules to deploy, h
44
author: kgremban
55
manager: philmea
66
ms.author: kgremban
7-
ms.date: 05/28/2019
7+
ms.date: 03/26/2020
88
ms.topic: conceptual
99
ms.service: iot-edge
1010
services: iot-edge
@@ -247,6 +247,9 @@ The following example shows what a valid deployment manifest document may look l
247247
"type": "docker",
248248
"status": "running",
249249
"restartPolicy": "always",
250+
"env": {
251+
"tempLimit": {"value": "100"}
252+
},
250253
"settings": {
251254
"image": "myacr.azurecr.io/filtermodule:latest",
252255
"createOptions": "{}"

articles/iot-edge/module-edgeagent-edgehub.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The module twin for the IoT Edge agent is called `$edgeAgent` and coordinates th
5050
| modules.{moduleId}.status | {"running" \| "stopped"} | Yes |
5151
| modules.{moduleId}.restartPolicy | {"never" \| "on-failure" \| "on-unhealthy" \| "always"} | Yes |
5252
| modules.{moduleId}.imagePullPolicy | {"on-create" \| "never"} | No |
53+
| modules.{moduleId}.env | A list of environment variables to pass to the module. Takes the format `"<name>": {"value": "<value>"}` | No |
5354
| modules.{moduleId}.settings.image | The URI to the module image. | Yes |
5455
| modules.{moduleId}.settings.createOptions | A stringified JSON containing the options for the creation of the module container. [Docker create options](https://docs.docker.com/engine/api/v1.32/#operation/ContainerCreate) | No |
5556
| modules.{moduleId}.configuration.id | The ID of the deployment that deployed this module. | IoT Hub sets this property when the manifest is applied using a deployment. Not part of a deployment manifest. |

0 commit comments

Comments
 (0)