Skip to content

Commit dc026a8

Browse files
committed
examples of using go templates to create dynamic service params in stack files
1 parent a8f7882 commit dc026a8

File tree

7 files changed

+89
-0
lines changed

7 files changed

+89
-0
lines changed

service_templating/1/a

Whitespace-only changes.

service_templating/2/b

Whitespace-only changes.

service_templating/3/c

Whitespace-only changes.

service_templating/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Service Templates
2+
This stack is currently a valuable example of using Go Templates to modify the ContainerSpec of a swarm service from a stack file.
3+
At this point in time, you can template `.Service`, `.Task`, and `.Node` information into `.Hostname`, `.Env`, and `.Mounts`.
4+
See the Docker docs on this [here](https://docs.docker.com/engine/reference/commandline/service_create/#create-services-using-templates).
5+
6+
### volumes:
7+
There are some caveats with using templating in mount specs with stack files.
8+
There are issues like [this one](https://github.com/moby/moby/issues/30770#issuecomment-277874145) related to using volumes names.
9+
Bind mounts work well without modification.
10+
See `stack.yml` for more context.
11+
12+
### usage:
13+
```bash
14+
mkdir 1 2 3
15+
touch 1/a 2/a 3/a
16+
docker deploy -c stack.yml mounts
17+
docker service logs -f mounts_hello
18+
```
19+
```
20+
mounts_hello.3.7hlgvxpla3f6@moby | hi! im hello-3, task-3 of mounts_hello in stack: mounts
21+
mounts_hello.1.cxkdeu4jhg1n@moby | hi! im hello-1, task-1 of mounts_hello in stack: mounts
22+
mounts_hello.2.x7yt0o0g5he5@moby | hi! im hello-2, task-2 of mounts_hello in stack: mounts
23+
mounts_hello.2.x7yt0o0g5he5@moby | total 0
24+
mounts_hello.3.7hlgvxpla3f6@moby | total 0
25+
mounts_hello.1.cxkdeu4jhg1n@moby | total 0
26+
mounts_hello.2.x7yt0o0g5he5@moby | -rwxr-xr-x 1 root root 0 Jun 22 05:29 b
27+
mounts_hello.1.cxkdeu4jhg1n@moby | -rwxr-xr-x 1 root root 0 Jun 22 05:29 a
28+
mounts_hello.3.7hlgvxpla3f6@moby | -rwxr-xr-x 1 root root 0 Jun 22 05:29 c
29+
```
30+
31+
# scripts
32+
`deploy.sh` removes shrapnel and prints out helpful information.
33+
`service.sh` creates a similar service to the stackfile from the command line.
34+
You may need to run these scripts 2-3 times depending on how slow your swarm is. (because removal is async)

service_templating/deploy.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
docker stack rm mounts
3+
sleep 4
4+
docker deploy -c stack.yml mounts
5+
6+
sleep 2
7+
docker service inspect --pretty mounts_hello
8+
docker service ps --no-trunc mounts_hello
9+
sleep 2
10+
docker service logs mounts_hello

service_templating/service.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
docker service rm hello
3+
docker service create --detach=true \
4+
--name hello \
5+
--hostname 'hello-{{.Task.Slot}}' \
6+
--env SLOT='{{.Task.Slot}}' \
7+
--env SERVICE='{{.Service.Name}}' \
8+
--replicas 3 \
9+
--mount \
10+
'type=bind,src=//local/mounts/{{.Task.Slot}},target=/data' \
11+
alpine \
12+
sh -c '
13+
echo hi! im ${HOSTNAME}, task-${SLOT} of ${SERVICE}
14+
ls -l /data
15+
sleep 1000
16+
'
17+
18+
sleep 2
19+
docker service inspect --pretty hello
20+
docker service ps --no-trunc hello
21+
docker service logs hello

service_templating/stack.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# see https://github.com/moby/moby/issues/30770#issuecomment-277874145 for a volumes workaround
2+
version: '3.2'
3+
4+
services:
5+
hello:
6+
image: alpine
7+
hostname: 'hello-{{.Task.Slot}}'
8+
environment:
9+
SLOT: '{{.Task.Slot}}'
10+
SERVICE: '{{.Service.Name}}'
11+
STACK: '{{index .Service.Labels "com.docker.stack.namespace"}}'
12+
command:
13+
- sh
14+
- -c
15+
- |
16+
echo hi! im $${HOSTNAME}, task-$${SLOT} of $${SERVICE} in stack: $${STACK}
17+
ls -l /data
18+
sleep 1000
19+
volumes:
20+
- type: bind
21+
source: ./{{.Task.Slot}}
22+
target: /data
23+
deploy:
24+
replicas: 3

0 commit comments

Comments
 (0)