Docker Hub官方Nginx镜像配置问题

浏览:54日期:2023-02-26

问题描述

Docker Hub官方Nginx镜像文档https://hub.docker.com/_/nginx/ 跟着文档说明进行配置,有些问题不知道什么意思,原文引用:

using environment variables in nginx configuration

Out-of-the-box, Nginx doesn’t support using environment variables inside most configuration blocks. But envsubst may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts.

Here is an example using docker-compose.yml:

image: nginx volumes: - ./mysite.template:/etc/nginx/conf.d/mysite.template ports: - '8080:80' environment: - NGINX_HOST=foobar.com - NGINX_PORT=80 command: /bin/bash -c 'envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g ’daemon off;’'

The mysite.template file may then contain variable references like this :

listen ${NGINX_PORT};

问题: 在代码部分最后一行,也就是command:那一行,1、envsubst作用是什么?2、daemon off;作用是什么?3、command:命令本身表示什么意思?什么时候执行后面的命令?

问题解答

回答1:

envsubst 大概是操作环境变量的东西,详情看命令说明

daemon off : 是nginx的参数, 表示不用后台启动的方式command 是docker run nginx /bin/sh 中镜像名nginx 后面那段/bin/sh, 可以执行任何命令好像在你别一个帖子说了

回答2:

/bin/bash -c 'envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf替换配置文件nginx -g ’daemon off;’' 楼上正解command: 容器运行后要执行的命令,可以被docker run语句中的命令覆盖。

相关文章: