![](/img/trans.png)
[英]Nginx, Certbot & Docker Compose: /etc/nginx/user.conf.d/*.conf: No such file or directory
[英]Add a file to `/docker-entrypoint.d/` as part of a app.conf.template file to swap global variables into nginx set up
我想使用 docker 和 docker-compose 通過 app.conf.template 文件將全局變量傳遞給我的 nginx app.conf。
當在docker-compose.yaml
中使用沒有命令的 app.conf.template 文件時,我的變量轉換成功,我通過 nginx 的重定向按預期工作。 但是當我在 docker-compose 中使用命令時,我的 nginx 和重定向失敗。
我的設置是按照文檔上的說明進行的,在“在 nginx 配置中使用環境變量(1.19 中的新功能)”部分下:
開箱即用的 nginx 不支持大多數配置塊中的環境變量。 但是這個鏡像有一個function,它會在nginx啟動之前提取環境變量。
這是使用 docker-compose.yml 的示例:
web:圖像:nginx 卷:
- ./templates:/etc/nginx/templates 端口:
- “8080:80”環境:
- NGINX_HOST=foobar.com
- NGINX_PORT=80
默認情況下,這個 function 讀取 /etc/nginx/templates/*.template 中的模板文件,並將執行 envsubst 的結果輸出到 /etc/nginx/conf.d... 更多...
我的 docker-compose.yaml 看起來像這樣:
version: "3.5"
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:
- "80:80"
- "443:443"
env_file: .env
networks:
- collabora
在主機上,我有一個 conf 文件./data/nginx/templates/app.conf.template
,其中包含一個帶有全局變量的 conf 文件,格式${variable_name}
。
通過這個設置,我可以運行容器並且我的重定向按預期工作。 當我exec
到容器中時,我可以cat /etc/nginx/conf.d/app.conf
並查看帶有從 .env 文件中交換的正確變量的文件。
但我需要向我的 docker-compose.yaml 添加一個命令:
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
當我添加該命令時,設置失敗並且全局變量不會交換到容器內的 app.conf 文件中。
在另一個論壇上,有人建議我將command
移到容器中它自己的文件中。 然后我嘗試了一下並創建了一個 shell 腳本test.sh
:
#/bin/sh
while :;
do sleep 6h & wait $${!};
nginx -s reload;
done;
我的新 docker-compose:
version: "3.5"
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:
- "80:80"
- "443:443"
env_file: .env
networks:
- collabora
這失敗了。 雖然當我執行到容器中並cat /etc/nginx/conf.d/app.conf
我確實看到了正確的配置,但它似乎在我的重定向中不起作用,否則當我不這樣做時它會起作用將此 test.sh 腳本包含在/docker-entrypoint.d/
中。
我昨天問了幾乎同樣的問題,並得到了一個可行的解決方案。 但是,將 shell 腳本添加到/docker-entrypoint.d/
和 go 的容器中,而不是像我在這篇文章中嘗試的那樣,“感覺更正確”。
對於您正在嘗試做的事情,我認為最好的解決方案是創建一個邊車容器,如下所示:
version: "3.5"
networks:
collabora:
volumes:
shared_run:
services:
nginx:
image: nginx:1.19
volumes:
- "shared_run:/run"
- ./data/nginx/templates:/etc/nginx/templates
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
env_file: .env
networks:
- collabora
nginx_reloader:
image: nginx:1.19
pid: service:nginx
volumes:
- "shared_run:/run"
entrypoint:
- /bin/bash
- -c
command:
- |
while :; do
sleep 60
echo reloading
nginx -s reload
done
這使您可以使用上游nginx
圖像,而無需對其機制進行處理。 這里的關鍵是(a)我們在與nginx
容器本身相同的 PID 命名空間中運行nginx_reloader
容器,以及(b)我們安排兩個容器共享一個/run
目錄,以便nginx -s reload
命令可以找到nginx
進程在預期位置的 pid。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.