在指令中使用全局 ARG 变量,FROM
例如RUN
例如我想在第二个构建阶段使用${CUDA_VERSION}
ARG 变量FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}
libcudnn7=${CUDNN_VERSION}-1+cuda${CUDA_VERSION}
但是全局 ARG 变量${CUDA_VERSION}
在通过FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}
9.0 到 9.0.176之后发生了更改
在 Ubuntu 18.04、Docker-CE 18.09.04
我尝试了很多事情
- 在构建阶段更改 ARG 变量行位置
- 从原始 ${CUDA_VERSION} 变量中复制其他 ARG 变量
- 在第一个构建阶段为环境变量制作 .profile。并在第二阶段从第一阶段复制
.profile
文件并使用source
命令应用它 - 使用 ENV 变量(但 ENV 变量在进入其他构建阶段时消失)
示例 dockerfile 和构建 dockerfile 的结果如下
Dockerfile
ARG handler_file=handler.py
ARG handler_name=Handler
ARG HANDLER_DIR=/handler
ARG HANDLER_FILE=${HANDLER_DIR}/${handler_file}
ARG HANDLER_NAME=${handler_name}
# Global arguments for Nvidia-docker
ARG CUDA_VERSION=9.0
ARG CUDNN_VERSION=7.4.1.5
ARG UBUNTU_VERSION=16.04
# == MutiStage Build ==
# 1-Stage
FROM python:3.7-alpine
ARG HANDLER_DIR
ARG HANDLER_FILE
ARG HANDLER_NAME
ARG handler_file
ARG handler_name
ARG CUDA_VERSION
RUN echo "${CUDA_VERSION}"
RUN mkdir -p ${HANDLER_DIR}
WORKDIR ${HANDLER_DIR}
COPY . .
RUN touch ${HANDLER_DIR}/__init__.py
# 2-Stage
FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}
# For Nvidia-Docker
ARG CUDA_VERSION
ARG CUDNN_VERSION
RUN echo "${CUDA_VERSION}"
# Copy directory from 1-stage
ARG HANDLER_DIR
RUN mkdir -p ${HANDLER_DIR}
WORKDIR ${HANDLER_DIR}
COPY --from=0 ${HANDLER_DIR} .
RUN echo "/usr/local/cuda-${CUDA_VERSION}/extras/CUPTI/lib64" > /etc/ld.so.conf.d/cupti.conf
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
wget \
tar \
libgomp1 \
libcudnn7=${CUDNN_VERSION}-1+cuda${CUDA_VERSION} \
python \
python-dev \
python-numpy \
python-pip \
python-setuptools \
python3 \
python3-dev \
python3-numpy \
python3-pip \
python3-setuptools \
python3-tk \
libgtk2.0-dev \
${ADDITIONAL_PACKAGE} \
&& rm -rf /var/lib/apt/lists/*
ENV LD_LIBRARY_PATH /usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
RUN pip3 --no-cache-dir install --upgrade \
pip setuptools
RUN pip3 install --upgrdae pip && \
pip3 install -r requirements.txt
构建消息
...
Step 9/33 : FROM python:3.7-alpine
---> 2caaa0e9feab
...
Step 16/33 : RUN echo "${CUDA_VERSION}"
---> Running in d057b0fd57a7
9.0
...
Step 21/33 : FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}
---> 2f9810b1b916
...
Step 24/33 : RUN echo "${CUDA_VERSION}"
---> Running in dc676c2a2992
9.0.176
...
Step 30/33 : RUN apt-get update && apt-get install -y --no-install-recommends build-essential wget tar libgomp1 libcudnn7=${CUDNN_VERSION}-1+cuda${CUDA_VERSION} python python-dev python-numpy python-pip python-setuptools python3 python3-dev python3-numpy python3-pip python3-setuptools python3-tk libgtk2.0-dev ${ADDITIONAL_PACKAGE} && rm -rf /var/lib/apt/lists/*
---> Running in 8518fb8d755c
...
E: Version '7.4.1.5-1+cuda9.0.176' for 'libcudnn7' was not found
The command '/bin/sh -c apt-get update && apt-get install -y --no-install-recommends build-essential wget tar libgomp1 libcudnn7=${CUDNN_VERSION}-1+cuda${CUDA_VERSION} python python-dev python-numpy python-pip python-setuptools python3 python3-dev python3-numpy python3-pip python3-setuptools python3-tk libgtk2.0-dev ${ADDITIONAL_PACKAGE} && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
预期的结果是 docker 文件构建成功
但 ARG 变量更改导致以下错误
E: Version '7.4.1.5-1+cuda9.0.176' for 'libcudnn7' was not found