我试图用我项目中的.env文件中的一些变量填充这个nginx.conf.template文件。My .env和我的docker-compose. yml位于同一个目录中。
下面是我的nginx.conf.template:
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name $DOCKER_DOMAIN_NAMES;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name $events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name $DOCKER_DOMAIN_NAMES;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name $DOCKER_SERVER_NAMES;
ssl_certificate /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH}/chain.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass http://app:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
};
ssl_certificate /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/$DOCKER_DOMAIN_PATH}/chain.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass http://app:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
我检查了容器docker exec -it nginx_container sh
,我看到像$DOCKER_SERVER_NAMES
这样的变量名仍然存在,而不是字符串值。
这是我的docker-composer.yml
nginx:
build: ./nginx
env_file:
- .env
ports:
- "80:80"
- "443:443"
environment:
- DOMAIN_NAMES=${DOCKER_DOMAIN_NAMES}
- CERTIFICATION_EMAIL=${DOCKER_CERTIFICATION_EMAIL}
- DOCKER_DOMAIN_PATH=${DOCKER_DOMAIN_PATH}
volumes:
- ./ssl-dhparams.pem:/etc/letsencrypt/ssl-dhparams.pem
- ./nginx/nginx.conf.template:/etc/nginx/nginx.conf.template:ro
- ./nginx/options-ssl-nginx.conf:/etc/letsencrypt/options-ssl-nginx.conf:ro
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
depends_on:
- app
我的变量似乎在其他部分工作,但由于某种原因,我不能让它们在confg.template文件中工作。
我的项目结构:
project/
├── Dockerfile
├── docker-compose.yml
├── .env
└── nginx/
├── Dockerfile
├── nginx.conf.template
└── myshellscript.sh
例如,myshellscript.sh将有一些/etc/letsencrypt/live/$DOCKER_DOMAIN_PATH/fullchain.pem
,它在容器中出现,正如预期的/etc/letsencrypt/live/mywebsite.com/fullchain.pem
,所以我不确定这些其他变量有什么问题。如何在模板文件中使其动态化?
1条答案
按热度按时间0yg35tkg1#
docker应该将这个变量$DOCKER_DOMAIN_NAMES替换为.env文件中的值
Docker不会在任何地方插入环境变量。Docker也不可能影响挂载文件的内容。
如果有的话,
docker-compose
在解析docker-compose.yml
时只在该文件中插入环境变量 *,并且使用与shell略有不同的语法,如https://docs.docker.com/compose/environment-variables/set-environment-variables/所述。如何在模板文件中使其动态化?
你必须写代码来做到这一点。最简单的是
envsubst
,如果它在您的图像中可用,并将其添加到您的图像的entrypoint
。