Skip to content

error calling index: index out of range: 1, but "last works #279

@maxmeyer

Description

@maxmeyer

I get this error when I use the index-method with docker-gen on something else than 0, but the last and first-methods work as expected. I can't really see what I do wrong.

2018/03/08 23:22:34 Template error: template: vhost-nginx-new.tmpl:10:3: executing "vhost-nginx-neu.tmpl" at <index $v 1>: error calling index: index out of range: 1

This is an "annotated" template showing the results for the example given below:

{{ range $value := whereExist . "Env.VIRTUAL_HOST" }}
{{ range $index, $vhost := split $value.Env.VIRTUAL_HOST "," }}
{{ $v := split $vhost ":::" }}
# this is the content of $v after splitting up the string
# [nextcloud-1.cloud.in.example.org nextcloud-1:80]
{{ $v }}

{{ $frontend := first $v }}
# this succeeds with:
# nextcloud-1.cloud.in.example.org
{{ $frontend }}

{{ $backend := last $v }}
# this succeeds with
# nextcloud-1:80
{{ $backend }}


{{ index $v 0 }}
# this succeeds with
# nextcloud-1.cloud.in.example.org

{{ index $v 1 }}
# this fails with
# error calling index: index out of range: 1
{{ end }}
{{ end }}

The docker container is started this way:


/usr/bin/docker run \
  --rm \
  --name nextcloud-1 \
  --network internal \
  -v /srv/machines/nextcloud/storage:/var/www/html \
  -e VIRTUAL_HOST=nextcloud-1.cloud.in.example.org:::nextcloud-1:80 \
  nextcloud:13

That's the template for docker-gen:

{{ range $value := whereExist . "Env.VIRTUAL_HOST" }}
{{ range $index, $vhost := split $value.Env.VIRTUAL_HOST "," }}
{{ $v := split $vhost ":::" }}
{{ $v }}
{{ $frontend := first $v }}
{{ $frontend }}
{{ $backend := last $v }}
{{ $backend }}
{{ index $v 0 }}
{{ index $v 1 }}
{{ end }}
{{ end }}

That's the docker-gen-configuration-file

[[config]]
template = "/etc/docker-gen/templates/vhost-nginx-new.tmpl"
dest = "/var/docker-gen/exports/nginx/vhosts-new.conf"
watch = true
keep-blank-lines = true
notify-output = true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions