我有一个Dockerfile,它有Scala和SBT,如下所示:
# 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
现在,如果我想将此作为我的基本图像,我有以下问题:
答案 0 :(得分:2)
每个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
答案 1 :(得分:1)
ARG
值在构建时定义
构建基础映像后,您不能只更改--build-arg
以在最终映像中获取另一个SCALA_VERSION
(或者您必须在新的Dockerfile中再次安装它)
目前的Dockerfile就像是其他Dockerfile的模板
您可以使用ONBUILD
instruction在构建其他图像期间有效地进行安装,使用此图像作为基本图像。