docker-compose构建环境变量 [英] docker-compose build environment variable

查看:156
本文介绍了docker-compose构建环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:当使用 docker-compose 构建映像并具有 docker时,如何传递env变量运行图像命令识别它们吗?

TL;DR : How can I pass env variable when building the image with docker-compose and have docker run image command recognize them ?

我有这个Dockerfile:

I have this Dockerfile :

FROM mhart/alpine-node:10
ADD . /app
WORKDIR /app
RUN apk --no-cache add --virtual builds-deps build-base python &&\
    yarn global add nodemon &&\
    yarn &&\
    apk del builds-deps build-base python

和此docker-compose.yml:

and this docker-compose.yml :

version: "3.3"
services:

  api:
    build:
      context: .
      dockerfile: Dockerfile-preprod
    image: registry.git.louis-girones.fr:4567/make-your-night/back:preprod
    environment:
      - NODE_ENV=development
    env_file:
      - .env
    volumes:
      - .:/app
    ports:
      - "${PORT_PREPROD}:${PORT_PREPROD}"
    command: sh -c "mkdir -p dist && touch ./dist/app.js && yarn run start"

  mongo:
    image: mongo:4.0
    ports:
      - "${MONGO_PREPROD}"
    command: mongod
    volumes:
      - ./data:/data/db

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1
    volumes:
      - ./esdata:/usr/share/elasticsearch/data
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    ports:
      - "9300:9300"
      - "9200:9200"

volumes:
  esdata:

使用此.env文件(位于docker-compose.yml和Dockerfile之类的根文件夹中):

With this .env file (which is in the root folder, like docker-compose.yml and Dockerfile) :

#!/usr/bin/env bash

NODE_ENV=development
PORT=9000
SECRET_SESSION=superSecr3t
APP_NAME=Night Vision
API_VERSION=/api/v0/
DEFAULT_TZ=Europe/Paris
ASSETS_URI=http://localhost:9000/public/img/
BCRYPT_WORKFACTOR=1
ES_PORT=9200
ES_LOG_LEVEL=trace

然后在节点服务器启动中执行以下代码:

And this code in the node server startup :

// Export the config object based on the NODE_ENV
// ==============================================
const config: IConfig = commonConfig

if (commonConfig.env === 'development') {
    _.merge(config, developmentConfig)
} else if (commonConfig.env === 'test') {
    _.merge(config, testConfig)
} else if (commonConfig.env === 'preproduction') {
    _.merge(config, preproductionConfig)
} else if (commonConfig.env === 'production') {
    _.merge(config, productionConfig)
} else {
    throw new Error('Please set an environment')
}

当我运行 docker-compose build 命令,一切正常,但例如,如果我尝试 docker run myimage yarn run test ,则会引发错误请设置环境。

When I run the docker-compose build command, everything is fine, but for instance If I try docker run myimage yarn run test the Error "Please set an environment" is thrown.

我希望

env_file:
  - .env

使该文件的env变量可在我的图像中访问,但事实并非如此,为什么我尝试添加

makes the env variables of this file accessible in my image but that is not the case, that's why I tried to add

 environment:
  - NODE_ENV=development

但是仍然没有成功,我也尝试在运行build时将env变量作为命令行参数传递:

But still no success, I have also tried to pass my env variable as command line argument when I run the build :

docker-compose build --build-arg NODE_ENV =开发api

但是给我以下消息:

[Warning] One or more build-args [NODE_ENV] were not consumed
Successfully built 9b14dd5abc3f

我真的很喜欢使用第一种或第二种方法

And I would really prefer to use the first or second methods

docker版本:18.06.1-ce
docker-compose版本:1.19.0

docker version : 18.06.1-ce docker-compose version : 1.19.0

推荐答案

有一条注释


注意:如果您的服务指定了构建选项,则在构建过程中不会自动显示环境中定义的变量。使用build的args子选项定义构建时环境变量。

Note: If your service specifies a build option, variables defined in environment are not automatically visible during the build. Use the args sub-option of build to define build-time environment variables.

它也是在此处中所述。首先(如上所述),您需要在Dockerfile中指定 ARG

It's also described in here. First (as mentioned above), you need to specify ARG in Dockerfile:

FROM mhart/alpine-node:10
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
ADD . /app
WORKDIR /app
RUN apk --no-cache add --virtual builds-deps build-base python &&\
    yarn global add nodemon &&\
    yarn &&\
    apk del builds-deps build-base python

,然后编辑您的 docker-compose 文件,使其在构建过程中包含自变量,如下所示:

And then edit your docker-compose file to include the argument during build, like so:

build:
  context: .
  dockerfile: Dockerfile-preprod
  args:
    - NODE_ENV=development

甚至

build:
  context: .
  dockerfile: Dockerfile-preprod
  args:
    - NODE_ENV=${NODE_ENV}

这篇关于docker-compose构建环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆