Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template nginx.tmpl error : nil pointer evaluating interface {}.Networks #2189

Closed
genestouxguy opened this issue Mar 9, 2023 · 9 comments · Fixed by #2191
Closed

template nginx.tmpl error : nil pointer evaluating interface {}.Networks #2189

genestouxguy opened this issue Mar 9, 2023 · 9 comments · Fixed by #2191
Labels
kind/bug Issue reporting a bug

Comments

@genestouxguy
Copy link

Hello,

I got the following error:
Template error: template: nginx.tmpl:24:39: executing "nginx.tmpl" at <$globals.CurrentContainer.Networks>: nil pointer evaluating interface {}.Networks

The nginx.tmpl is https://github.com/nginx-proxy/nginx-proxy/blob/main/nginx.tmpl

my docker-compose.yml is :

version: '3.9'

services:
  portail:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    logging:
        driver: "json-file"
        options:
            max-size: "200k"
            max-file: "10"
    volumes:
      - porte_conf:/etc/nginx/conf.d:ro
      - porte_htpasswd:/etc/nginx/htpasswd:ro
      - porte_vhost:/etc/nginx/vhost.d:ro
      - porte_html:/usr/share/nginx/html:ro
      - certif_certs:/etc/nginx/certs:ro
    ports:
      - 80:80
      - 443:443
    networks:
      - net-front
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: 'true'

  docker-gen:
    image: nginxproxy/docker-gen:0.10.1
    restart: unless-stopped
    logging:
        driver: "json-file"
        options:
            max-size: "200k"
            max-file: "10"
    depends_on:
      - portail
    container_name: portail_gen
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certif_certs:/etc/nginx/certs:ro
      - porte_conf:/etc/nginx/conf.d:rw
      - porte_html:/usr/share/nginx/html:rw
      - porte_htpasswd:/etc/nginx/htpasswd:rw
      - porte_templates:/etc/docker-gen/templates:ro
      - porte_vhost:/etc/nginx/vhost.d:rw
    command: -notify-sighup nginx -watch -only-exposed -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    labels:
      com.github.jrcs.letsencrypt_nginx_proxy_companion.docker_gen: 'true'
    networks:
      - net-front

  certif:
    image: nginxproxy/acme-companion:2.2.6
    container_name: portail_certif
    restart: unless-stopped
    logging:
        driver: "json-file"
        options:
            max-size: "200k"
            max-file: "10"
    depends_on:
      - docker-gen
    volumes:
      - certif_certs:/etc/nginx/certs:rw
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - porte_conf:/etc/nginx/conf.d
      - porte_vhost:/etc/nginx/vhost.d
      - porte_html:/usr/share/nginx/html
    networks:
      - net-front
    environment:
      NGINX_PROXY_CONTAINER: 'nginx'
      NGINX_DOCKER_GEN_CONTAINER: 'portail_gen'
      DEBUG: 'false'
  vim:
    image: disp/vim
    volumes:
      - porte_conf:/etc/nginx/conf.d:rw
      - porte_html:/usr/share/nginx/html:rw
      - porte_htpasswd:/etc/nginx/htpasswd:rw
      - porte_templates:/etc/docker-gen/templates:rw
      - porte_vhost:/etc/nginx/vhost.d:rw

networks:
  net-front:
    external:
      name: net-proxy

volumes:
  certif_certs:
  porte_conf:
  porte_html:
  porte_htpasswd:
  porte_templates:
  porte_vhost:
docker version
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:47:01 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:47:01 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

And I run the following container :
docker run --env VIRTUAL_HOST=test.example.com --env VIRTUAL_PORT=8000 --env LETSENCRYPT_HOST=test.example.com --env WWW='no-www' --network=net-proxy jwilder/whoami

Could you tell me why this error please ?

@SchoNie
Copy link
Contributor

SchoNie commented Mar 9, 2023

The network in your compose is net-front
But your whoami container is attached to net-proxy

So the containers cannot reach eachother. That is probably why it is giving a nil pointers as there are zero (nil) available networks.

@genestouxguy
Copy link
Author

Thanks for your response.

The network net-front is only a reference to the neworks section where I specify net-front is external and the name is net-proxy but I changed all net-front by net-proxy and I got the same error :/

@SchoNie
Copy link
Contributor

SchoNie commented Mar 9, 2023

Yes sorry, you are right. I missed the name part.
I tried your config and indeed same issue. (only with docker-gen as separate container)

I narrowed it down to commit bcec2d9075c053b9930de52a32a712d69961a213 if that helps @rhansen

If you need a quick fix, you could use the nginx.tmpl from the commit before that.

@genestouxguy
Copy link
Author

Thanks for your response. It works, thanks.

I let you decide when you want to close the issue.

@genestouxguy
Copy link
Author

For information, I changed line 13 in nginx.tmpl by {{- $_ := set $globals "CurrentContainer" (where $globals.containers "Name" "nginx" | first) }}.

With this change the actual template works.

@rhansen
Copy link
Collaborator

rhansen commented Mar 10, 2023

I think I understand what's going wrong. I don't think the commit @SchoNie identified is wrong; instead, I think that it exposed nginx-proxy/docker-gen#458 in certain setups where that bug wasn't exposed before. Specifically, if there are no containers with VIRTUAL_HOST set, then $globals.CurrentContainer.Networks was never evaluated. Now it is.

As a workaround, I believe it would be sufficient to remove -only-exposed from the arguments passed to docker-gen. Can you confirm @genestouxguy?

@SchoNie
Copy link
Contributor

SchoNie commented Mar 10, 2023

As a workaround, I believe it would be sufficient to remove -only-exposed from the arguments passed to docker-gen. Can you confirm @genestouxguy?

Yes for me it already works, with the copied config from @genestouxguy

@genestouxguy
Copy link
Author

Sorry I didn't get enough time today to try. I try monday. Have a nice weekend.

@genestouxguy
Copy link
Author

Hi,

It works also for me.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Issue reporting a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants