2017-07-18 90 views
0

我有一個Dockerfile有Scala和SBT如下:如果我想用這個作爲我的基本圖像Dockerfile覆蓋ARG值

# Scala and sbt Dockerfile 
# Scala and SBT forked from: https://github.com/hseeberger/scala-sbt 
# Adds Docker client installation additionally 

# Pull base image 
FROM openjdk:$JAVA_VERSION 

ARG SCALA_VERSION 
ARG SBT_VERSION 

# Scala expects this file 
RUN touch /usr/lib/jvm/java-$JAVA_VERSION-openjdk-amd64/release 

# Install Scala 
## Piping curl directly in tar 
RUN \ 
    curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \ 
    echo >> /root/.bashrc && \ 
    echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc 

# Install sbt 
RUN \ 
    curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \ 
    dpkg -i sbt-$SBT_VERSION.deb && \ 
    rm sbt-$SBT_VERSION.deb && \ 
    apt-get update && \ 
    apt-get install sbt && \ 
    sbt sbtVersion 

現在,我有以下問題:

  1. 我可以在我的新Dockerfile中覆蓋ARG參數,它將使用上面那個作爲基礎圖像嗎?
+1

一個已經建立不能改變的圖像。你應該可以找到一些圖像標籤約定來解決這個問題。 – Grimmy

回答

1

每個Dockerfile都會生成一個不可變的圖像。所以你需要先建立一個特定的基礎圖像,然後建立你自己的FROM它。

你可以採取這種做法:

Dockerfile.base

# Scala and sbt Dockerfile 
# Scala and SBT forked from: https://github.com/hseeberger/scala-sbt 
# Adds Docker client installation additionally 

# Pull base image 
ARG JAVA_VERSION 
FROM openjdk:$JAVA_VERSION 

ARG SCALA_VERSION 
ARG SBT_VERSION 

# Scala expects this file 
RUN touch /usr/lib/jvm/java-$JAVA_VERSION-openjdk-amd64/release 

# Install Scala 
## Piping curl directly in tar 
RUN \ 
    curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \ 
    echo >> /root/.bashrc && \ 
    echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc 

# Install sbt 
RUN \ 
    curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \ 
    dpkg -i sbt-$SBT_VERSION.deb && \ 
    rm sbt-$SBT_VERSION.deb && \ 
    apt-get update && \ 
    apt-get install sbt && \ 
    sbt sbtVersion 

你的孩子Dockerfile Dockerfile.mine

ARG JAVA_VERSION 
ARG SCALA_VERSION 
ARG SBT_VERSION 
FROM basejava:java-$JAVA_VERSION-scala-$SCALA_VERSION-sbt-$SBT_VERSION 
# Your app stuf here 
# ... 
# ... 

構建例如:

export JAVA_VERSION=latest 
export SCALA_VERSION=1.8 
export SBT_VERSION=0.13.9 
docker build . \ 
    -f Dockerfile.base \ 
    --build-arg JAVA_VERSION=$JAVA_VERSION \ 
    --build-arg SCALA_VERSION=$SCALA_VERSION \ 
    --build-arg SBT_VERSION=$SBT_VERSION \ 
    -t basejava:java-$JAVA_VERSION-scala-$SCALA_VERSION-sbt-$SBT_VERSION 

docker build . \ 
    -f Dockerfile.mine \ 
    --build-arg JAVA_VERSION=latest \ 
    --build-arg SCALA_VERSION=1.8 \ 
    --build-arg SBT_VERSION=0.13.9 
    -t myjava:java-$JAVA_VERSION-scala-$SCALA_VERSION-sbt-$SBT_VERSION 

方法2,ONBUILD,由zigarn提及。

Dockerfile.base

# Scala and sbt Dockerfile 
# Scala and SBT forked from: https://github.com/hseeberger/scala-sbt 
# Adds Docker client installation additionally 

# Pull base image 
ONBUILD ARG JAVA_VERSION 
FROM openjdk:$JAVA_VERSION 

ONBUILD ARG SCALA_VERSION 
ONBUILD ARG SBT_VERSION 

# Scala expects this file 
ONBUILD RUN touch /usr/lib/jvm/java-$JAVA_VERSION-openjdk-amd64/release 

# Install Scala 
## Piping curl directly in tar 
ONBUILD RUN \ 
    curl -fsL http://downloads.typesafe.com/scala/$SCALA_VERSION/scala-$SCALA_VERSION.tgz | tar xfz - -C /root/ && \ 
    echo >> /root/.bashrc && \ 
    echo 'export PATH=~/scala-$SCALA_VERSION/bin:$PATH' >> /root/.bashrc 

# Install sbt 
ONBUILD RUN \ 
    curl -L -o sbt-$SBT_VERSION.deb http://dl.bintray.com/sbt/debian/sbt-$SBT_VERSION.deb && \ 
    dpkg -i sbt-$SBT_VERSION.deb && \ 
    rm sbt-$SBT_VERSION.deb && \ 
    apt-get update && \ 
    apt-get install sbt && \ 
    sbt sbtVersion 

你的孩子Dockerfile.mine

FROM basejava 
# Base ONLBUILD instructions are automatically inserted here. 
# Your app stuf here 
# ... 
# ... 

體形:

docker build . -f Dockerfile.base -t basejava 

export JAVA_VERSION=latest 
export SCALA_VERSION=1.8 
export SBT_VERSION=0.13.9 
docker build . \ 
    -f Dockerfile.mine \ 
    --build-arg JAVA_VERSION=$JAVA_VERSION \ 
    --build-arg SCALA_VERSION=$SCALA_VERSION \ 
    --build-arg SBT_VERSION=$SBT_VERSION \ 
    -t myjava:java-$JAVA_VERSION-scala-$SCALA_VERSION-sbt-$SBT_VERSION 
0

ARG值是在構建時定義的。
一旦你的基本映像建,你不能只是改變--build-arg再弄SCALA_VERSION您的最終圖像中(或你必須在你重新安裝新Dockerfile)

本Dockerfile是像其他Dockerfile模板。
您可以使用ONBUILD instruction在構建其他圖像時使用此圖像作爲基礎圖像來有效地進行安裝。

+0

我不能在我繼承的Dockerfile中覆蓋它嗎? – sparkr

+0

是的,你可以。從編輯的Dockerfile構建一個新的圖像。 – Vineeth

+0

是的,將會有一個新的Dockerfile生成的圖像,但新的Dockerfile不會傳遞這些參數,但會覆蓋它們。根據我的理解,似乎可以做到這一點。我會嘗試一下! – sparkr