title: Add a file to `/docker-entrypoint.d/` as part of a app.conf.template file to swap global variables into nginx set up
body: <p>I would like to pass global variables to my nginx app.conf via a app.conf.template file using docker and docker-compose.</p>
  <p>When using an app.conf.template file with no commands in <code>docker-compose.yaml</code> my variables translate successfully and my redirects via nginx work as expected. But when I use a command in docker-compose my nginx and redirects fail.</p>
  <p>My set up is per the instructions on <a href="https://hub.docker.com/_/nginx/" rel="nofollow noreferrer">the documentation</a>, under section 'Using environment variables in nginx configuration (new in 1.19)':</p>
  <blockquote>
  <p>Out-of-the-box, nginx doesn't support environment variables inside
  most configuration blocks. But this image has a function, which will
  extract environment variables before nginx starts.</p>
  <p>Here is an example using docker-compose.yml:</p>
  <p>web:   image: nginx   volumes:</p>
  <ul>
  <li>./templates:/etc/nginx/templates   ports:</li>
  <li>&quot;8080:80&quot;   environment:</li>
  <li>NGINX_HOST=foobar.com</li>
  <li>NGINX_PORT=80</li>
  </ul>
  <p>By default, this function reads template files in
  /etc/nginx/templates/*.template and outputs the result of executing
  envsubst to /etc/nginx/conf.d
  ... more ...</p>
  </blockquote>
  <p>My docker-compose.yaml works when it looks like this:</p>
  <pre><code>version: &quot;3.5&quot;
  networks:
    collabora:
    
  services:
    nginx:
      image: nginx
      depends_on:
        - certbot   
        - collabora 
      volumes:
        - ./data/nginx/templates:/etc/nginx/templates
        - ./data/certbot/conf:/etc/letsencrypt
        - ./data/certbot/www:/var/www/certbot
      ports:
        - &quot;80:80&quot;
        - &quot;443:443&quot;
      env_file: .env
      networks:
        - collabora
  </code></pre>
  <p>On host I have a conf file <code>./data/nginx/templates/app.conf.template</code> which contains a conf file with global variables throughout in the form <code>${variable_name}</code>.</p>
  <p>With this set up I'm able to run the container and my redirects work as expected. When I <code>exec</code> into the container I can <code>cat /etc/nginx/conf.d/app.conf</code> and see the file with the correct variables swapped in from the .env file.</p>
  <p>But I need to add a command to my docker-compose.yaml:</p>
  <pre><code>command: &quot;/bin/sh -c 'while :; do sleep 6h &amp; wait $${!}; nginx -s reload; done &amp; nginx -g \&quot;daemon off;\&quot;'&quot;
  </code></pre>
  <p>When I add that command the set up fails and the global variables are not swapped into the app.conf file within the container.</p>
  <p>On another forum it was suggested I move the <code>command</code> into it's own file in the container. I then gave this a try and created a shell script <code>test.sh</code>:</p>
  <pre><code>#/bin/sh 
  
  while :; 
      do sleep 6h &amp; wait $${!}; 
      nginx -s reload; 
  done;
  </code></pre>
  <p>My new docker-compose:</p>
  <pre><code>version: &quot;3.5&quot;
  networks:
    collabora:
    
  services:
    nginx:
      image: nginx
      depends_on:
        - certbot   
        - collabora 
      volumes:
        - ./data/nginx/templates:/etc/nginx/templates
        - ./test.sh:/docker-entrypoint.d/test.sh # new - added test.sh into the container here
        - ./data/certbot/conf:/etc/letsencrypt
        - ./data/certbot/www:/var/www/certbot
      ports:
        - &quot;80:80&quot;
        - &quot;443:443&quot;
      env_file: .env
      networks:
        - collabora
  </code></pre>
  <p>This fails. Although when I exec into the container and <code>cat /etc/nginx/conf.d/app.conf</code> I DO see the correct config, it just does not seem to be working in that my redirects, which otherwise do work when I don't include this test.sh script within <code>/docker-entrypoint.d/</code>.</p>
  <p>I <a href="https://stackoverflow.com/questions/66732240/docker-compose-with-nginx-templates-to-pass-global-variables-adding-a-command-t">asked nearly same question</a> yesterday and was given a working solution. However, it 'feels more correct' to add a shell script to the container at <code>/docker-entrypoint.d/</code> and go that route instead like I've attempted in this post.</p>