简体   繁体   English

如何使用 dockerfile 访问环境变量

[英]How to acces env variable with dockerfile

I'm stuck with my dockerfile, i'll try to get acces to an env.我坚持使用我的 dockerfile,我会尝试访问环境。 variable who is declaring in my docker-compose.yml but it tell me that the variable do not exist.在我的 docker-compose.yml 中声明的变量,但它告诉我该变量不存在。

docker-compose.yml docker-compose.yml

my-project-dev:
container_name: my-project-frontend
build:
  context: .
  dockerfile: Dockerfile
volumes:
  - './my-project-frontend:/usr/src/app'
  - '/usr/src/app/node_modules'
ports:
  - 15242:80
  # - 49153:49153
environment:
  - ENV=dev

dockerfile dockerfile

FROM node:16.14.2 as builder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY . /usr/src/app
RUN npm install
RUN npm run build-${ENV}  

For debuging i try in my dockerfile to "RUN env", that's the result.为了进行调试,我尝试将 dockerfile 设置为“RUN env”,这就是结果。

But, when my container is start and i enter on it and i execute "env" i see my "ENV" variable但是,当我的容器启动并进入它并执行“env”时,我看到了我的“ENV”变量

Anyone have a solution?有人有解决办法吗?

Environment variable in docker-compose are intended for the application . docker-compose中的环境变量用于应用程序 Ie runtime environment of your application.即您的应用程序的运行时环境。

Dockerfile does not have environment variables. Dockerfile 没有环境变量。 Instead - it has build arguments.相反 - 它具有构建 arguments。

Thus, you need to use build arguments io env.因此,您需要使用 build arguments io env。 variables for customization of build process.用于定制构建过程的变量。

Namely:即:

Dockerfile Dockerfile

ARG ENV=prod    # default value
RUN npm run build-${ENV}

Build script构建脚本

docker build --build-arg ENV=dev

or in docker-compose :或者在docker-compose中:

build:
    context: .
    args:
        ENV: dev

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM