[英]How to pass environment variables from docker-compose into the NodeJS project?
I have a NodeJS application, which I want to docker-size.我有一个 NodeJS 应用程序,我想要 docker-size。
The application consists of two parts:该应用程序由两部分组成:
server part, running an API which is taking data from a DB.服务器部分,运行从数据库获取数据的 API。 This is running on the port 3000;这是在端口 3000 上运行的;
client part, which is doing a calls to the API end-points from the server part.客户端部分,它正在从服务器部分调用 API 端点。 This is running on the port 8080;这是在端口 8080 上运行的;
With this, I have a variable named "server_address" in my client part and it has the value of "localhost:3000".有了这个,我的客户端部分中有一个名为“server_address”的变量,它的值为“localhost:3000”。 But here is the thing, the both projects should be docker-sized in a separate Dockerimage
files and combined in one docker-compose.yml
file.但事情是这样的,这两个项目应该在一个单独的Dockerimage
文件中是Dockerimage
-sized 并合并在一个docker-compose.yml
文件中。
So due some reasons, I have to run the docker containers via docker-compose.yml
file.所以由于某些原因,我必须通过docker-compose.yml
文件运行docker-compose.yml
容器。 So is it possible to connect these things somehow and to pass the server address externally from dockerfile into the NodeJS project?那么是否有可能以某种方式连接这些东西并将服务器地址从 dockerfile 外部传递到 NodeJS 项目中?
docker-composer.yml docker-composer.yml
version: "3"
services:
client-side-app:
image: my-client-side-docker-image
environment:
- BACKEND_SERVER="here we need to enter backend server"
ports:
- "8080:8080"
server-side-app:
image: my-server-side-docker-image
ports:
- "3000:3000"
both of the Dockerfile 's looks like:两个Dockerfile看起来像:
FROM node:8.11.1
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
by having these files, I have the concern:通过拥有这些文件,我担心:
BACKEND_SERVER
somehow in the project?我可以在项目中以某种方式使用变量BACKEND_SERVER
吗? And if yes, how to do this?如果是,如何做到这一点? I'm not referring to the Dockerimage
file, instead into the project itself?我不是指Dockerimage
文件,而是指项目本身?Use process.env in node.js code, like this在 node.js 代码中使用 process.env,像这样
process.env.BACKEND_SERVER
Mention your variable in docker-compose file.在 docker-compose 文件中提及您的变量。
version: "3"
services:
client-side-app:
image: my-client-side-docker-image
environment:
- BACKEND_SERVER="here we need to enter backend server"
ports:
- "8080:8080"
server-side-app:
image: my-server-side-docker-image
ports:
- "3000:3000"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.