Reputation: 1255
Docker version:
Client: Docker Engine - Community
Version: 27.5.0
API version: 1.47
Go version: go1.22.10
Git commit: a187fa5
Built: Mon Jan 13 15:25:19 2025
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 27.5.0
API version: 1.47 (minimum version 1.24)
Go version: go1.22.10
Git commit: 38b84dc
Built: Mon Jan 13 15:25:19 2025
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.7.25
GitCommit: bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
runc:
Version: 1.2.4
GitCommit: v1.2.4-0-g6c52b3f
docker-init:
Version: 0.19.0
GitCommit: de40ad0
I want to use docker swarm deploy with the docker compose plugin. This works. However when I try to access system attributes like {{Node.Hostname}}
and {{Task.Slot}}
, the containers never seem to be able to use them. The containers deploy but the env vars are just bank for these keys.
I have read online that this is not possible is some places, is that true? I have not specifically seen anywhere that docker swarm + docker compose plugin (not docker-compose) that this does not work.
Is this supported? or will this not work?
here is my Docker compose file:
services:
filebeat:
build:
context: ./filebeat
image: scale-filebeat
deploy:
mode: replicated
replicas: 6
endpoint_mode: dnsrr
hostname: "{{.Node.Hostname}}-filebeat-{{.Task.Slot}}"
ports:
- "6515-6520:6514"
- "5071-5076:5066"
volumes:
- ./filebeat/filebeat.yml:/usr/share/filebeat/config/filebeat.yml
- ./filebeat/keystore/filebeat.keystore:/usr/share/filebeat/data/filebeat.keystore
environment:
- HOSTNAME={{.Node.Hostname}}-filebeat-{{.Task.Slot}}
Be aware I am using a Dockerfile
as well. Not sure if its interfering. Can anyone help?
EDIT:
Well it seems that those attributes are not support with docker compose
commands. They are only supported in docker service
commands. There is no easy way to get to local env vars with docker compose / docker commands without manually setting the env vars directly via variety of methods. But using docker service
made it work. Now my hostnames on the containers align with the name of the host and task :)
I initiated swarm, and deployed a service instead. Make sure to change your --advertise-addr
to the interface ip you want the service to listen on.
docker swarm init --advertise-addr 192.168.1.10
docker stack deploy -c docker-compose.yml scale
Then checked to make sure it deployed with
docker service ps scale_filebeat
# OR
docker ps
Note: This actually deployes and uses the Jinja attributes {{.Node.Hostname}}
, and {{.Task.Slot}}
. Some downsides to using a docker service is...
docker stack deploy
command, it will seem like nothing happened. Well you have to wait for everything to happen before the containers appear and the ports are open, be patient.docker ps
and docker service ps
no longer show Ports that are open. It will just be a blank column. But rest assured, the ports are open. Check ss
or netstat
to confirm. Or you can run docker service inspect scale_filebeat --format '{{json .Endpoint.Ports}}' | jq
To confirm.Upvotes: 0
Views: 21