Merge pull request #10512 from pszczekutowicz/master

Add example for templated config
This commit is contained in:
Sebastiaan van Stijn 2020-04-11 13:06:13 +02:00 committed by GitHub
commit 1e98526f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 50 additions and 0 deletions

View File

@ -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
<html>
<head><title>Hello Docker</title></head>
<body>
<p>Hello {% raw %}{{ env "HELLO" }}{% endraw %}! I'm service {% raw %}{{ .Service.Name }}{% endraw %}.</p>
</body>
</html>
```
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
<html>
<head><title>Hello Docker</title></head>
<body>
<p>Hello Docker! I'm service hello-template.</p>
</body>
</html>
```
### Advanced example: Use configs with a Nginx service
This example is divided into two parts.