Skip to content

Instantly share code, notes, and snippets.

@shanewholloway
Last active August 4, 2022 03:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shanewholloway/6527cd71325dfcff2bd012edeaf97dfc to your computer and use it in GitHub Desktop.
Save shanewholloway/6527cd71325dfcff2bd012edeaf97dfc to your computer and use it in GitHub Desktop.
Example Docker Stack Deploy with Node/Service/Task values
## 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
@trajano
Copy link

trajano commented May 3, 2019

Do you have one where you can get the computed hostname so it can be embedded in a variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment