[英]switching command based on node env variable for the docker-compose.yml file
I am trying to host an application, so I have created a server and a client folder.我正在尝试托管一个应用程序,因此我创建了一个服务器和一个客户端文件夹。 In server folder node js and client its react using create-react-app.在服务器文件夹节点 js 和客户端中,它使用 create-react-app 做出反应。
So for docker, I have created two Dockerfile in server and client folder.所以对于 docker,我在 server 和 client 文件夹中创建了两个 Dockerfile。 In the project root created a docker-compose.yml file.在项目根目录中创建了一个 docker-compose.yml 文件。
for local development, I need to have the auto reloading capability for the server so, I have put对于本地开发,我需要具有服务器的自动重新加载功能,因此,我已将
command: nodemon index.js
in the docker-compose.yml file.在 docker-compose.yml 文件中。 Everything is working fine.一切正常。 when i build the docker.当我构建 docker 时。 But when I host this application I need to change to但是当我托管这个应用程序时,我需要更改为
`command: node index.js`
The only way I think it will add a node environment variable when I host the application, but the problem is I can access in the index.js of server folder like我认为它会在我托管应用程序时添加节点环境变量的唯一方法,但问题是我可以在服务器文件夹的 index.js 中访问,例如
process.env.APPLICATION_ENVIRONMENT
but how can I access in the docker-compose.yml file?但是如何在 docker-compose.yml 文件中访问? Since I want to use the same docker-compose.yml file for hosting and make developer to start work easily by having the capability of server auto reloading.因为我想使用相同的 docker-compose.yml 文件进行托管,并通过具有服务器自动重新加载的功能让开发人员轻松开始工作。
Is there any other better way to do this.有没有其他更好的方法来做到这一点。 ? ?
docker-compose
files support variable substitution . docker-compose
文件支持变量替换。 You can then use this to store and set the command you want to run directly in the docker-compose
file.然后,您可以使用它来直接在docker-compose
文件中存储和设置要运行的命令。
Eg a sample docker-compose.yml
:例如一个示例docker-compose.yml
:
version: "3"
services:
server:
build: ./server
command: ${NODE_COMMAND:-nodemon} index.js
${NODE_COMMAND:-nodemon}
will default to nodemon
if no NODE_COMMAND
variable is present in your shell.如果 shell 中不存在NODE_COMMAND
变量,则${NODE_COMMAND:-nodemon}
将默认为nodemon
。 You can override this value in production by providing a value for NODE_COMMAND
when you start the containers ie :您可以通过在启动容器时为NODE_COMMAND
提供值来覆盖生产中的此值,即:
$ NODE_COMMAND=node docker-compose up -d
Alternatively, on your hosted server you could create a .env
file in the same directory that you run your docker-compose
commands like so:或者,在您的托管服务器上,您可以在运行.env
docker-compose
命令的同一目录中创建一个.env
文件,如下所示:
NODE_COMMAND=node
And docker-compose
will automatically substitute the value in your compose file.并且docker-compose
将自动替换您撰写文件中的值。 See the linked page about variable substitution for more details.有关更多详细信息,请参阅有关变量替换的链接页面。
Hopefully this helps.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.