The environment files in .production.env is not being picked up when running `docker compose –env-file ./production.env up`

huangapple go评论68阅读模式
英文:

The environment files in .production.env is not being picked up when running `docker compose --env-file ./production.env up`

问题

# 我试图做的事情
我尝试使用以下命令在 Docker 上构建我的项目:
`docker compose --env-file ./production.env up --build`。我想要在 Dockerfile 外部的一个单独文件中定义环境变量。我使用了 --env-file 标志 (https://docs.docker.com/compose/environment-variables/set-environment-variables/#substitute-with---env-file)。

在执行该命令后,Docker 没有报错并成功创建了容器。然而,**我的项目检测到环境变量缺失**并退出。

# 我的设置
我正在运行一个 Node 项目。
我的文件结构如下:

- docker-compose.yml
- Dockerfile
- production.env
- ...其他文件 (src 文件夹等)

我的 Dockerfile:

FROM node:16-alpine as development

WORKDIR /usr/src/app

COPY package*.json .

RUN npm install

COPY . .

RUN npm run build

FROM node:16-alpine as production

WORKDIR /usr/src/app

COPY package*.json .

RUN npm ci --only=production

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/index.js"]


我的 docker-compose.yml:

version: "3.4";

services:
api:
build:
context: .
target: production

ports:
  - 8888:8888

我的 Node 项目需要环境变量才能运行,如果没有检测到所有环境变量,它将抛出异常。这些环境变量存储在 production.env 文件中,以下是我的 production.env 文件的示例:

DB_URI=https://urihere.net
PASSWORD_KEY=somepw

英文:

what I am trying to do

I tried to build my project on docker using the command:
docker compose --env-file ./production.env up --build. I want to define the environment variables in a separate file outside Dockerfile. I used the --env-file flag (https://docs.docker.com/compose/environment-variables/set-environment-variables/#substitute-with---env-file).

After executing the command, Docker did not complain and managed to create the container. However, my project detects that the environment variables are missing and exits.

My setup

I am running a node project.
My file structure is

  • docker-compose.yml
  • Dockerfile
  • production.env
  • ...other files (src folder, etc)

My Dockerfile

    FROM node:16-alpine as development

    WORKDIR /usr/src/app

    COPY package*.json .

    RUN npm install

    COPY . .

    RUN npm run build

    FROM node:16-alpine as production

    WORKDIR /usr/src/app

    COPY package*.json .

    RUN npm ci --only=production

    COPY --from=development /usr/src/app/dist ./dist

    CMD ["node", "dist/index.js"]

My docker-compose.yml:


    version: "3.4"

    services:
      api:
        build:
          context: .
          target: production

        ports:
          - 8888:8888

My node project requires environment variables to run and will throw and exception if not all env variables are detected. These env vars are stored in production.env file and a sample of how my production.env file looks:

    DB_URI=https://urihere.net
    PASSWORD_KEY=somepw

答案1

得分: 1

你实际上没有将环境变量传递给容器。

一种选择是使用Compose env_file: 将整个环境变量文件传递给容器;在这种情况下,您不需要docker-compose --env-file选项。

services:
  api:
    env_file:
      - production.env
docker-compose up --build -d  # 没有 --env-file

另一种选择是保留--env-file选项,但使用environment: 选项将每个值传递给容器。

services:
  api:
    environment:
      - DB_URI        # 没有 `=value` 部分
      - PASSWORD_KEY
docker-compose --env-file production.env up -d --build

Compose有两层环境变量。普通的Unix环境、.env文件和任何--env-file文件被用来创建一组可以用于docker-compose.yml文件中的变量替换的变量。但是,这些变量不会自动传递给各个容器的环境;只有environment:env_file: 内容被使用(增补和覆盖镜像的ENV行)。

英文:

You're not actually passing the environment variables on to the container.

One option is to use Compose env_file: to pass the entire file of environment variables to the container; you would not need a docker-compose --env-file option in this case.

services:
  api:
    env_file:
      - production.env
docker-compose up --build -d  # no --env-file

A second is to keep the --env-file option, but to use an environment: option to pass along each of the values to the container.

services:
  api:
    environment:
      - DB_URI        # without an `=value` part
      - PASSWORD_KEY
docker-compose --env-file production.env up -d --build

Compose has two layers of environment variables. The normal Unix environment, the .env file, and any --env-file files get used to create a set of variables that can be used for variable substitution in the docker-compose.yml file. However, these are not automatically passed on to individual containers' environments; only the environment: and env_file: contents are used (augmenting and overriding the images' ENV lines).

huangapple
  • 本文由 发表于 2023年4月11日 14:16:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定