![](/img/trans.png)
[英]docker-compose of Nginx image doesn't work when the docker-compose.yml file is moved
[英]nginx docker alpine envsubst doesn't work when there's a command in docker-compose
我想將 envsubst 與 nginx docker alpine 一起使用。 文檔:
在 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。
我有一個形式為 nginx 的容器服務:
version: '3'
services:
nginx:
image: nginx:alpine
restart: unless-stopped
volumes:
#- ./data/nginx:/etc/nginx/conf.d
- ./templates:/etc/nginx/templates
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- "80:80"
- "443:443"
env_file: .env
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
當我運行此服務時,envsubst 不起作用。 但是,如果我刪除command:
在 docker-compose 中,它確實有效。
我嘗試將命令復制到 a.sh 文件並將其移動到/docker-entrypoint.d/mycommand.sh
的容器內。 這里默認有幾個 other.sh 文件在 nginx 加載之前運行:
ls -l docker-entrypoint.d
total 16
-rwxrwxr-x 1 root root 1961 May 25 15:44 10-listen-on-ipv6-by-default.sh
-rwxrwxr-x 1 root root 1037 May 25 15:44 20-envsubst-on-templates.sh
-rwxrwxr-x 1 root root 4613 May 25 15:44 30-tune-worker-processes.sh
我也使用卷在其中添加了mycommand.sh
。 這是 mycommand.sh 的樣子:
#! /bin/bash
# reload nginx config every 60 seconds in case certs get updated
while :; do
sleep 60
echo reloading
nginx -s reload
done
但是,當我將此腳本添加到/docker-entrypoint.d
目錄時,nginx 似乎永遠不會啟動。
如何根據文檔運行我的命令並使用 nginx alpine 使用 envsubst?
我希望能夠 go 這條路線,而不是例如使用具有共享卷的新 nginx 容器來獲得相同的結果。 將 a.sh 腳本添加到docker-entrypoint.d
“感覺”就像是“正確”的方式,並且會產生更簡約的 docker-compose。
更改命令不起作用,因為/docker-entrypoint.sh
包含:
if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then
...
當您更改命令時, $1
變為/bin/sh
。
/docker-entrypoint.d 中的腳本需要在/docker-entrypoint.d
到達運行命令的docker-entrypoint.sh
之前運行並退出(默認情況下,啟動 nginx)。 您可以將該 while 循環放在后台來實現此目的,只需在完成后附加一個&
:
#! /bin/bash
# reload nginx config every 60 seconds in case certs get updated
while :; do
sleep 60
echo reloading
nginx -s reload
done &
請注意,這在很大程度上是容器的反模式,通常僅在開發環境中才有意義。 在生產中,您將使用新配置重新部署一個新容器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.