1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

dockerでNginxコンテナ立てたら.conf.templateが効かなくなった

Posted at

ローカルでdocker-composeを使って構築していた環境を共用環境に構築しようとしたらNginxでtemplateが効かなくなったので、備忘録としてメモしておきます

設定

詳細は省略します。

docker-compose.yml
  proxy:
    image: nginx
    container_name: proxy
    volumes:
      - ./nginx/dev.conf.template:/etc/nginx/templates/default.conf.template
    ports:
      - 80:80
   environment:
      - SERVER_PORT=3000
      - LISTEN_PORT=80
nginx/dev.conf.template
server {
    listen ${LISTEN_PORT};
    server_name localhost;

    location / {
        proxy_pass http://localhost:${SERVER_PORT}/;
    }
}

結論

nginxのDockerイメージを1.20にする

調査内容

docker exec -it proxy shでコンテナ内に入ってtemplateファイルを確認しましたがちゃんとコピーされていました。
ローカルと共用環境でdocker imagesでイメージのバージョンを確認したところ、両方ともlatestでしたがCREATEDの日付が異なっていました。
DockerHubでバージョンを確認したところ、ローカル環境構築のlatestは1.20で共用環境構築時のlatestは1.21のようでした。

そこで、試しにNginxのDockerイメージを1.20にしてみたところちゃんとtemplateに従ってリバプロしてくれるようになりました。

docker-compose.yml
  proxy:
    image: nginx:1.20

真の原因は不明ですが、いったんこれで回避することにしました

DockerHubの説明を見るとtemplate自体が1.19で新しく追加された機能みたいなのでまた不安定なのかもしれないですね。

Using environment variables in nginx configuration (new in 1.19)

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.

Here is an example using docker-compose.yml:

web:
  image: nginx
  volumes:
   - ./templates:/etc/nginx/templates
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80

By default, this function reads template files in /etc/nginx/templates/*.template and outputs the result of executing envsubst to /etc/nginx/conf.d.

So if you place templates/default.conf.template file, which contains variable references like this:

listen ${NGINX_PORT};

outputs to /etc/nginx/conf.d/default.conf like this:

listen 80;

This behavior can be changed via the following environment variables:

  • NGINX_ENVSUBST_TEMPLATE_DIR
    • A directory which contains template files (default: /etc/nginx/templates)
    • When this directory doesn't exist, this function will do nothing about template processing.
  • NGINX_ENVSUBST_TEMPLATE_SUFFIX
    • A suffix of template files (default: .template)
    • This function only processes the files whose name ends with this suffix.
  • NGINX_ENVSUBST_OUTPUT_DIR
    • A directory where the result of executing envsubst is output (default: /etc/nginx/conf.d)
    • The output filename is the template filename with the suffix removed.
      • ex.) /etc/nginx/templates/default.conf.template will be output with the filename /etc/nginx/conf.d/default.conf.
    • This directory must be writable by the user running a container.

引用:https://hub.docker.com/_/nginx

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?