简体   繁体   English

如何将环境变量从 docker-compose 传递到 NodeJS 项目中?

[英]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:通过拥有这些文件,我担心:

  • will I be able to use the variable 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.

相关问题 带有Docker-compose的NodeJS - NodeJS with Docker-compose MongoDB在docker-compose环境中通过mongoose与nodeJs的连接 - MongoDB Connection with nodeJs through mongoose in docker-compose environment 如何使用 docker-compose 文件中的命名图像和指定图像的 Dockerfile 启动命名 NodeJS 容器? - How to start a named NodeJS container with a named image from a docker-compose file and and a Dockerfile specifying the image? 如何使用 nodejs 在 docker-compose 上的 mongoDB 上设置身份验证? - How to setup authentication on mongoDB on docker-compose with nodejs? 为什么 docker compose 不将环境变量传递给 nodejs 应用程序? - Why docker compose dont pass the environment variable to nodejs app? docker-compose:来自.env 的变量在服务中不可用 - docker-compose : variables from .env not available in the service Docker组成环境中的环回Postgres - Loopback Postgres in Docker-compose environment 如何在 monorepo 项目中使用 docker-compose 处理 node_modules - How to handle node_modules with docker-compose in a monorepo project nodeJS 上 Postgres 的 docker-compose ECONNREFUSED - docker-compose ECONNREFUSED for Postgres on nodeJS docker-compose 总是使用 nodejs 执行 - docker-compose always exec using nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM