每個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
一個已經建立不能改變的圖像。你應該可以找到一些圖像標籤約定來解決這個問題。 – Grimmy