docker-compose build environment variable

 ̄綄美尐妖づ 提交于 2020-05-25 12:07:46

问题


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

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

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:

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')
}

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.

I would expect that

env_file:
  - .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

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=development api

But it gives me this message :

[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 version : 18.06.1-ce docker-compose version : 1.19.0


回答1:


There's a note in the docs:

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.

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

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

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

Or even

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


来源:https://stackoverflow.com/questions/52429984/docker-compose-build-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!