Last active
August 4, 2022 03:44
-
-
Save shanewholloway/6527cd71325dfcff2bd012edeaf97dfc to your computer and use it in GitHub Desktop.
Example Docker Stack Deploy with Node/Service/Task values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## docker stack deploy -c docker-stackfile.yml gist_demo | |
version: "3.8" | |
services: | |
playground: | |
image: node:alpine | |
hostname: '{{.Task.Name}}' | |
environment: | |
SWARM_TASK: '{{.Task.Name}}' | |
SWARM_PEERS: "tasks.{{.Service.Name}}" | |
SWARM_TASK_FQN: '{{.Task.Name}}.{{index .Service.Labels "com.docker.stack.namespace"}}_default' | |
SWARM_PEERS_FQN: 'tasks.{{.Service.Name}}.{{index .Service.Labels "com.docker.stack.namespace"}}_default' | |
SWARM_INFO: | | |
{ "service": "{{.Service.Name}}", "labels": "{{.Service.Labels}}" | |
, "id": "{{.Service.ID}} {{.Task.ID}} {{.Node.ID}}" | |
, "node": "{{.Node}}", "task": "{{.Task.Name}}" | |
, "stack": "{{index .Service.Labels "com.docker.stack.namespace"}}" | |
, "image": "{{index .Service.Labels "com.docker.stack.image"}}" } | |
command: | |
- 'node' | |
- '-e' | |
- | | |
let {resolve4} = require("dns/promises"), as_err = err => ({err}) | |
let {HOSTNAME, SWARM_INFO, SWARM_TASK, SWARM_PEERS} = process.env | |
require("http").createServer() | |
.on('request', async (req,res) => { | |
res.end(JSON.stringify({ | |
"req": [req.method, req.httpVersion, req.url], | |
"info": JSON.parse(SWARM_INFO), | |
"env": {HOSTNAME, SWARM_TASK, SWARM_PEERS}, | |
"dns": { | |
HOSTNAME: await resolve4(HOSTNAME).catch(as_err), | |
SWARM_TASK: await resolve4(SWARM_TASK).catch(as_err), | |
SWARM_PEERS: await resolve4(SWARM_PEERS).catch(as_err), | |
}}, null, 2)) | |
}) | |
.listen(3001, "0.0.0.0", ()=> {}) | |
ports: ["3001:3001"] | |
deploy: | |
replicas: 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have one where you can get the computed hostname so it can be embedded in a variable?