From fc0327569cbe03c0a48f074ae8ca25eeb04fc8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Szczekutowicz?= Date: Tue, 24 Mar 2020 19:38:10 +0100 Subject: [PATCH] Add example for templated config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Szczekutowicz --- engine/swarm/configs.md | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/engine/swarm/configs.md b/engine/swarm/configs.md index 79b635a951..57b5a0cc99 100644 --- a/engine/swarm/configs.md +++ b/engine/swarm/configs.md @@ -278,6 +278,56 @@ This example assumes that you have PowerShell installed. docker config rm homepage ``` +### Example: Use a templated config + +To create a configuration in which the content will be generated using a +template engine, use the `--template-driver` parameter and specify the engine +name as its argument. The template will be rendered when container is created. + +1. Save the following into a new file `index.html.tmpl`. + + ```html + + Hello Docker + +

Hello {% raw %}{{ env "HELLO" }}{% endraw %}! I'm service {% raw %}{{ .Service.Name }}{% endraw %}.

+ + + ``` + +2. Save the `index.html.tmpl` file as a swarm config named `homepage`. Provide + parameter `--template-driver` and specify `golang` as template engine. + + ```bash + $ docker config create --template-driver golang homepage index.html.tmpl + ``` + +3. Create a service that runs Nginx and has access to the environment variable + HELLO and to the config. + + ```bash + $ docker service create \ + --name hello-template \ + --env HELLO="Docker" \ + --config source=homepage,target=/usr/share/nginx/html/index.html \ + --publish published=3000,target=80 \ + nginx:alpine + ``` + +4. Verify that the service is operational: you can reach the Nginx server, and + that the correct output is being served. + + ```bash + $ curl http://0.0.0.0:3000 + + + Hello Docker + +

Hello Docker! I'm service hello-template.

+ + + ``` + ### Advanced example: Use configs with a Nginx service This example is divided into two parts.