Reputation: 4607
I am trying to set the hostname for the container as hostname: '{{.Node.Hostname}}'
in docker compose v3 in AWS EC2.
It is failing as Error response from daemon: rpc error: code = 3 desc = expanding hostname failed: template: expansion:1:7: executing "expansion" at <.Node.Hostname>: can't evaluate field Hostname in type struct { ID string }
.
Here are the version details,
Client:
Version: 17.06.2-ce
API version: 1.30
Go version: go1.8.4
Git commit: 3dfb8343b139d6342acfd9975d7f1068b5b1c3d3
Built: Fri Nov 10 00:50:37 2017
OS/Arch: linux/amd64
Server:
Version: 17.06.2-ce
API version: 1.30 (minimum version 1.12)
Go version: go1.8.4
Git commit: 402dd4a/17.06.2-ce
Built: Fri Nov 10 00:51:08 2017
OS/Arch: linux/amd64
Experimental: false
As per https://docs.docker.com/compose/compose-file/, 17.06.+ is compatible with docker-compose v3.
How I can make this templating work? Do I need to upgrade the docker? Thanks.
Upvotes: 0
Views: 1900
Reputation: 8616
This now works with compose files and v17.12 (Dec 2017 stable release) of Docker, but only works when used with Swarm Stacks.
I think you're using Swarm so just update your docker version and you should be good, but here's more info for others in case they are just trying single node docker-compose up
, which won't work with templates.
docker-compose
does not interpret go templates like Swarm does.
In addition, the .Node.Hostname
is a Swarm-specific label, so you can't use it with docker-compose up
on a single server, which doesn't have access to that label.
Just to verify, when I use this compose file:
version: "3.4"
services:
nginx:
image: nginx
hostname: '{{.Node.Hostname}}'
In docker-compose this is what I got:
docker-compose up -d
docker-compose exec nginx hostname
{{.Node.Hostname}}
Then if I use that file in Swarm Stacks:
docker stack deploy -c docker-compose.yml hn
# find the node and container ID of that service task
# found them on node 1 with hostname "swarm1"
docker exec 0a087 hostname
swarm1
If you need to use docker-compose
in a single node and get the hostname into the compose file, maybe use variable substitution, then set the hostname to a variable and then docker-compose up
will replace the variable in the compose file.
Upvotes: 1