consul-template icon indicating copy to clipboard operation
consul-template copied to clipboard

Generating template in docker for nginx

Open sweetodev opened this issue 6 years ago • 1 comments

I went through this article and was able to get to this point with the help from the author. Article: https://danielparker.me/nginx/consul-template/consul/nginx-consul-template/

The issue is consul-template is not hot reloading the configuration and if I restart the container, it reads but returns erroneous information.

Issue 1: I've added -s reload flag but it's not taking any effect.

Issue 2: On force deleting and restarting the nginx (service) container, it reads the entry but returns

upstream department { 
    server 127.0.0.1:0;
}

Dockerfile

FROM hashicorp/consul-template:alpine as template

FROM nginx
COPY --from=template /bin/consul-template /bin/consul-template

WORKDIR /service
COPY nginx.ctmpl nginx.ctmpl
COPY entrypoint.sh entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]

CMD nginx -c /etc/nginx/nginx.conf -g 'daemon off;'

entrypoint.sh

#!/bin/bash -x
echo "starting entry script"

consul-template -template "nginx.ctmpl:nginx.conf" -consul-addr "consul:8500" -log-level debug

exec "$@"

Tried consul-template -template "nginx.ctmpl:nginx.conf -s reload" -consul-addr "consul:8500" -log-level debug as well

nginx.ctmpl

upstream department { {{range service "department" }}
    server {{.Address}}:{{.Port}};{{end}}
}

server {
    listen 80 default_server;

    location /departments {
        proxy_pass http://department
    }
}

docker-compose

version: '3'

services:

  consul:
    image: consul:1.6.1
    container_name: consul
    ports:
      - 8300:8300
      - 8400:8400
      - 8500:8500

  service:
    container_name: nginx
    build:
      context: service
      dockerfile: Dockerfile
    ports:
      - 8080:80
    depends_on:
      - consul
      - department

sweetodev avatar Nov 04 '19 20:11 sweetodev

Hey @sweetodev, sorry for the delayed response. For general questions like this you'll likely get a better response by using the community support channels instead of a github issue. We use these for bugs and feature requests.

One thing that is wrong above is that you aren't running the nginx process form consul-template. Consul-template is designed to managed the sub-process it generates the configuration for. Reworking your entrypoint.sh with this in mind. It might look like...

#!/bin/bash -x
echo "starting entry script"

exec consul-template -template "nginx.ctmpl:nginx.conf" -consul-addr "consul:8500" -log-level debug -exec "$@"

Mostly the same, just replacing the exec of the passed in command with the -exec argument to consul-template.

eikenb avatar Mar 20 '20 21:03 eikenb