docker-compose使用来自.env文件

时间:2017-09-03 08:37:49

标签: docker environment-variables docker-compose dockerfile environment

我想要在docker-compose个配置和Dockerfile中使用一些环境变量。我有一个名为.env的环境文件,以便在docker-compose文件中使用它们(我用于this issue的解决方案);但是节点的Dockerfile中无法访问这些环境变量。将JMX_PORTWS_PORTIP视为我.env文件中定义的变量;我在echo中为每个Dockerfile添加了.env,以检查它们是否传递给它。

我尝试将env_file放入节点的version: "2" services: mynode: build: context: . dockerfile: Dockerfile-mynode env_file: - .env ports: - "${JMX_PORT}:${JMX_PORT}" - "${WS_PORT}:${WS_PORT}" 部分:

version: "2"
services:
  mynode:
    build:
      context: .
      dockerfile: Dockerfile-mynode
    environment:
      - IP=${IP}
      - JMX_PORT=${JMX_PORT}
      - WS_PORT=${WS_PORT}
    ports:
      - "${JMX_PORT}:${JMX_PORT}"
      - "${WS_PORT}:${WS_PORT}"

我也尝试过:

version: "2"
services:
  mynode:
    build:
      context: .
      dockerfile: Dockerfile-mynode
    environment:
      - IP
      - JMX_PORT
      - WS_PORT
    ports:
      - "${JMX_PORT}:${JMX_PORT}"
      - "${WS_PORT}:${WS_PORT}"

稍作修改:

Dockerfile

上述方法均无效。

我不想在另一个文件中重新定义它们。对此有什么解决方案吗?

编辑1:

这是我的FROM mybaseimage MAINTAINER Zeinab Abbasimazar RUN echo ${IP} RUN echo ${JMX_PORT} RUN echo ${WS_PORT}

docker-compose config

编辑2:

这是docker-compose命令的输出,暗示services: mynode: build: context: /home/zeinab/Workspace/ZiZi-Docker/Test/test-1 dockerfile: Dockerfile-WRCore environment: IP: 171.80.80.80 JMX_PORT: '1998' WS_PORT: '9000' ports: - 1998:1998 - 9000:9000 version: '2.0' volumes: {} 可以按预期访问环境变量:

docker-compose build --no-cache

这是Dockerfile命令的输出,暗示docker-compose build --no-cache Building mynode Step 1 : FROM mybaseimage ---> 875978d91e2e Step 2 : MAINTAINER Zeinab Abbasimazar ---> Running in 250eac87d078 ---> 0e7bd6a18c80 Removing intermediate container 250eac87d078 Step 3 : RUN echo ${IP} ---> Running in 4ee6dbf74b2a ---> fd2bff1de6b6 Removing intermediate container 4ee6dbf74b2a Step 4 : RUN echo ${JMX_PORT} ---> Running in 01245df43926 ---> 68b7e90b1c01 Removing intermediate container 01245df43926 Step 5 : RUN echo ${WS_PORT} ---> Running in d27db926ecac ---> a8c3d0cbade8 Removing intermediate container d27db926ecac Successfully built a8c3d0cbade8 无法按预期访问环境变量:

{
  "name": "Reviews Hotel",
  "version": "1.0.0",
  "description": "This repo contains the Reviews Hotel application that is built through Full Stack Web Developer course.",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "engines": {
    "node": "8.2.1",
    "npm": "5.3.0"
  },
  "dependencies": {
    "angular": "^1.5.6",
    "angular-route": "^1.5.6",
    "angular-jwt": "^0.0.9",
    "bcrypt-nodejs": "^0.0.3",
    "jsonwebtoken": "^7.1.3",
    "body-parser": "^1.14.2",
    "express": "^4.13.3",
    "mongodb": "^2.1.7",
    "mongoose": "^4.4.8"
  },
  "devDependencies": {
    "mocha": "^2.3.3"
  },
  "scripts": {
    "test": "mocha"
  },
  "author": "Aurora Ruggieri",
  "keywords": [
    "FullStack"
  ],
  "license": "ISC"
}

1 个答案:

答案 0 :(得分:1)

所以你所做的是错的。原因在于构建时要将环境变量传递给Dockerfile。所以有两种可能的解决方案

使用构建器参数

FROM mybaseimage
MAINTAINER Zeinab Abbasimazar
ARG IP
ARG JMX_PORT
ARG WS_PORT
RUN echo ${IP}
RUN echo ${JMX_PORT}
RUN echo ${WS_PORT}

然后使用compose

传递这些构建参数
version: "2"
services:
  mynode:
    build:
      context: .
      dockerfile: Dockerfile-mynode
      args:
        - IP=${IP}
        - JMX_PORT=${JMX_PORT}
        - WS_PORT=${WS_PORT}
    environment_file:
      - .env
    ports:
      - "${JMX_PORT}:${JMX_PORT}"
      - "${WS_PORT}:${WS_PORT}"

您也可以使用environment_file加载环境变量。现在,这仅在容器启动时可用,并且它们不适用于您的撰写文件或构建文件。

另外,添加构建参数只会使它们在构建期间可用,而不是在容器启动期间。如果您需要这样做,您需要定义变量两次

FROM mybaseimage
MAINTAINER Zeinab Abbasimazar
ARG IP
ARG JMX_PORT
ARG WS_PORT
ENV IP=${IP} JMX_PORT=${JMX_PORT} WS_PORT=${WS_PORT}
RUN env

复制环境文件

<强> .ENV

export NAME=TARUN

<强> Dockerfile

FROM alpine
COPY .env /tmp/.env
RUN cat /tmp/.env > /etc/profile.d/tarun.sh && chmod +x /etc/profile.d/tarun.sh
SHELL ["sh","-lc"]
RUN env

RUN cat /tmp/.env > /etc/profile.d/tarun.sh && chmod +x /etc/profile.d/tarun.sh我们创建配置文件时,应该在使用配置文件执行shell时加载该文件。

SHELL ["sh","-lc"]我们将默认shell从sh -c更改为sh -l,以便它也可以加载我们的个人资料。

最后一步的输出是

Step 5/5 : RUN env
 ---> Running in 329ec287a5a6
HOSTNAME=e1ede117fb1e
SHLVL=1
HOME=/root
PAGER=less
PS1=\h:\w\$
NAME=TARUN
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
CHARSET=UTF-8
 ---> 6ab95f46e940

正如您所见,环境变量就在那里。

  

注意:由于我们有一个bash命令的导出语句,因此无法在.env中加载此docker-compose格式