如何将参数从docker-compose传递到Dockerfile?

时间:2019-05-29 09:43:09

标签: bash docker docker-compose arguments dockerfile

我在文件夹backend中有一个小的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.yml:

version: "3"
services:
  generator:
    build: backend
    ports:
      - "8080:8080"

我从bash脚本开始:

#!/usr/bin/env bash

FTP_IP=$1 MONGO_IP=$2 QUORUM_IP=$3 docker-compose up -d

赞:

start-backend.sh 127.0.0.1 127.0.0.1 http://localhost:22000

但是它根本不起作用...当我在创建的容器上调用docker inspect时得到:

"Id": "bd3e05a8fffba6bb7b5c650d1f48c0ed13dca9108e01e1a82ec534a5f19d4393",
        "Created": "2019-05-29T09:38:32.723414205Z",
        "Path": "java",
        "Args": [
            "-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"
        ]

我在做什么错了?

1 个答案:

答案 0 :(得分:-1)

在脚本start-backend.sh中,您使用了脚本本地的变量FTP_IP,MONGO_IP和QUORUM_IP,将它们导出为env变量即可使用。

请记住,shell中的值优先于.env文件和dockerfile中指定的值,因此您可能会覆盖在那里定义的值...