简体   繁体   English

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

First I want to use my application.env variables on my webpack.config.prod.js, so I did this in my webpack file.首先我想在我的 webpack.config.prod.js 上使用我的 application.env 变量,所以我在我的 webpack 文件中这样做了。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

I am successfully able to access process.env.BUILD variables.我能够成功访问 process.env.BUILD 变量。

My application's env has this configuration -我的应用程序的环境有这个配置 -

在此处输入图像描述

My nodejs web app is running fine locally, no problem at all.我的 nodejs web 应用程序在本地运行良好,完全没有问题。 I want to build docker image of this application and need to use docker-compose to create the container.我想构建此应用程序的 docker 映像,需要使用 docker-compose 来创建容器。

I built my docker image and everything good so far.我构建了我的 docker 图像,到目前为止一切都很好。

now to create container, instead of docker run.现在创建容器,而不是 docker 运行。 I am using separate folder which consists of docker-compose.yml and.env files.我正在使用由 docker-compose.yml 和 .env 文件组成的单独文件夹。 Attached the screenshot below附上下面的截图

![在此处输入图像描述

My docker-compose.yml has this code -我的 docker-compose.yml 有这个代码 -

version: '3.9'
services:     
  api:
    image: 'api:latest'
    ports:
      - '17000:17000'
    env_file:
      - .env
    volumes:
      - ./logs:/app/logs
    networks:
      - default

My docker-compose.env has this redis details我的 docker-compose.env 有这个 redis 详细信息

在此处输入图像描述

My application has this logs -我的应用程序有这个日志 -

在此处输入图像描述

I started my docker container by doing docker-compose up.我通过执行 docker-compose 启动了我的 docker 容器。 Containers created and up and running, but the problem is容器创建并启动并运行,但问题是

In the console, after connecting to redis.. process.env.REDIS_HOST contains the value called 'localhost' (which came from the first env where I used to build docker image).在控制台中,连接到 redis.. 后,process.env.REDIS_HOST 包含名为“localhost”的值(来自我用来构建 docker 映像的第一个环境)。 Docker compose.env is not getting accessed. Docker compose.env 未被访问。

After spending 5+ hours.花了5个多小时后。 I found the culprit, It was webpack.我找到了罪魁祸首,它是 webpack。 On my initial code, I added some env related things in my webpack right?在我的初始代码中,我在 webpack 中添加了一些与环境相关的内容,对吧? Once I commented those, taken a new build.一旦我评论了这些,就进行了新的构建。 Everything is working fine.一切正常。

But my problem is how I can actually use process.ENV in webpack, also docker-compose need to use the.env from its directory.但我的问题是如何在 webpack 中实际使用 process.ENV,docker-compose 也需要使用其目录中的.env。

Updated -更新 -

My DockerFile looks like this:我的 DockerFile 看起来像这样:

在此处输入图像描述

Just, It will copy the dist which contains bundle.js, npm start will do - pm2 run bundle.js.只是,它会复制包含 bundle.js 的 dist,npm start 就可以了 - pm2 运行 bundle.js。

From what I know webpack picks up the.env at build time, not at runtime.据我所知,webpack 在构建时而不是在运行时获取.env。 This means that it needs the environment variables when the image is built.这意味着它在构建映像时需要环境变量。

The one you pass in docker-compose.yml is not used because by then your application is already built.您在 docker-compose.yml 中传递的那个没有被使用,因为那时您的应用程序已经构建好了。 Is that correct?那是对的吗? In order to user your.env you should build the image with docker-compose and pass the env variables as build arguments to your Dockerfile.为了使用 your.env,您应该使用 docker-compose 构建映像,并将 env 变量作为构建 arguments 传递给您的 Dockerfile。

In order to build the image using your docker-compose.yml, you should do something like this:为了使用您的 docker-compose.yml 构建映像,您应该执行以下操作:

version: '3.9'
services:     
  api:
    image: 'api:latest'
    build:
      context: .
      args:
      - REDIS_HOST
      - REDIS_PORT
    ports:
      - '17000:17000'
    volumes:
      - ./logs:/app/logs

Note: the context above points to the current folder.注意:上面的context指向当前文件夹。 You can change it to point to a folder where you Dockerfile (and the rest of the project is) or you can put your docker-compose.yml together with the rest of the project directly and then context stays . You can change it to point to a folder where you Dockerfile (and the rest of the project is) or you can put your docker-compose.yml together with the rest of the project directly and then context stays . . .

In your Dockerfile you need to specify these arguments:在您的Dockerfile ,您需要指定这些 arguments:

FROM node:14 as build

ARG REDIS_HOST
ARG REDIS_PORT

...

With these changes you can build and run with docker-compose:通过这些更改,您可以使用 docker-compose 构建和运行:

docker-compose up -d --build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法解析 dockerfile - 没有这样的文件或目录 - docker-compose.yml - Failed to resolve dockerfile - no such file or directory - docker-compose.yml 基于 docker-compose.yml 文件的节点环境变量的切换命令 - switching command based on node env variable for the docker-compose.yml file Docker Compose: .env: no such file or directory / NodeJS - Docker Compose: .env: no such file or directory / NodeJS 如何在不使用 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 使用 Docker-Compose 时,Dockerfile 中的“ENTRYPOINT [”./init.sh“]”会产生“没有这样的文件或目录”? - “ENTRYPOINT [”./init.sh“]” in Dockerfile produces “no such file or directory” when using Docker-Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM