传递ENV变量不适用于docker-compose

时间:2019-06-03 07:54:19

标签: docker variables parameters docker-compose dockerfile

我正在努力使用docker-compose传递ENV变量

我有一个FROM alpine:latest ENV ftp_ip 127.0.0.1 ENV mongo_ip 127.0.0.1 ENV quorum_ip http://localhost:22000 RUN apk add --update openjdk8 && mkdir /var/backend/ RUN apk update COPY license-system-0.0.1-SNAPSHOT.jar /var/backend/ EXPOSE 8080 ENTRYPOINT [ "java", "-jar", "-Dspring.quorum.host=${quorum_ip}", "-Dspring.ftp.server=${ftp_ip}", "-Dspring.data.mongodb.host=${mongo_ip}","/var/backend/license-system-0.0.1-SNAPSHOT.jar" ] 用Java应用程序来构建容器:

version: "3"
services:
  backend:
    network_mode: host
    build: backend
    ports:
      - "8080:8080"
    environment:
      - mongo_ip=${mongo_ip}
      - ftp_ip=${ftp_ip}
      - quorum_ip=${quorum_ip}

然后,泊坞窗组成文件:

quorum_ip="$1" mongo_ip="$2" ftp_ip="$3" docker-compose up -d --build

最后,容器通过bash命令启动:

docker inspect

但是 "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "ftp_ip=127.0.0.1", "mongo_ip=127.0.0.1", "quorum_ip=http://localhost:22000" ], "Cmd": null, "ArgsEscaped": true, "Image": "sha256:3ce51f52d70127f22462eafdb60321a4e477a4bec5aa092e860b8485e8575c26", "Volumes": null, "WorkingDir": "", "Entrypoint": [ "java", "-jar", "-Dspring.quorum.host=${quorum_ip}", "-Dspring.ftp.server=${ftp_ip}", "-Dspring.data.mongodb.host=${mongo_ip}", "/var/backend/license-system-0.0.1-SNAPSHOT.jar" ] 并没有什么希望。变量设置不正确(它们使用的是dockerfile中的默认值),并且参数arent甚至更改为默认值...

m <- load_model_hdf5("www//ae_model.h5").

我想念什么吗?还是我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果要在入口点使用environmnet变量,则应使用“ shell形式”而不是“ exec形式”。

ENTRYPOINT java -jar -Dspring.quorum.host=${quorum_ip} -Dspring.ftp.server=${ftp_ip} -Dspring.data.mongodb.host=${mongo_ip} /var/backend/license-system-0.0.1-SNAPSHOT.jar

您可能可以使其与“ exec表单”一起使用,但只会使语法复杂化。

答案 1 :(得分:-1)

您必须使用build args而不是env来从Dockerfile构建映像

Dockerfile

FROM alpine:latest

ARG ftp_ip
ARG mongo_ip
ARG quorum_ip

RUN apk add --update openjdk8 && mkdir /var/backend/
RUN apk update
COPY license-system-0.0.1-SNAPSHOT.jar /var/backend/
EXPOSE 8080
ENTRYPOINT [ "java", "-jar", "-Dspring.quorum.host="${quorum_ip}, "-Dspring.ftp.server="${ftp_ip}, "-Dspring.data.mongodb.host="${mongo_ip},"/var/backend/license-system-0.0.1-SNAPSHOT.jar" ]

docker-compose

version: "3"
services:
  backend:
    network_mode: host
    build:
      context: .
      dockerfile: ./path/to/backend/Dockerfile
      args:
        - mongo_ip=${mongo_ip}
        - ftp_ip=${ftp_ip}
        - quorum_ip=${quorum_ip}
    ports:
      - "8080:8080"

.env(传递env以在docker-compose中使用。docker-compose自动从.env文件中获取env)(如果存在)

ftp_ip=127.0.0.1
mongo_ip=127.0.0.1
quorum_ip=http://localhost:22000

然后运行docker-compose build以使用正确的envs构建映像