简体   繁体   English

基于 docker-compose.yml 文件的节点环境变量的切换命令

[英]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.

相关问题 Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack - Docker compose is using the env which was built by Dockerfile, instead of using env_file located in docker-compose.yml directory because of webpack 在没有docker-compose.yml文件的单个docker文件中运行react和node - running react and node in a single docker file without docker-compose.yml file 为什么我的节点容器会忽略我在docker-compose.yml中设置的环境变量? - Why does my node container ignore the environment variable I set in docker-compose.yml? 如何在不使用 Dockerfile 的情况下仅使用 Docker-compose.yml 文件启动 Node.js 应用程序的容器 - How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile 源代码控制(git)docker-compose.yml - Source controlling (git) docker-compose.yml .dockerignore 不忽略 docker-compose.yml - .dockerignore not ignoring docker-compose.yml docker-compose.yml问题nodejs和mysql - docker-compose.yml issue nodejs and mysql docker-compose.yml用于postgres遇到错误 - docker-compose.yml for postgres encountering error 移动 docker-compose.yml 文件时,Nginx 图像的 docker-compose 不起作用 - docker-compose of Nginx image doesn't work when the docker-compose.yml file is moved Node.js 未在 Docker 中运行,我的 docker-compose.yml 是否正确? - Node.js is not running in Docker is my docker-compose.yml correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM