FROM在多阶段构建后更改了全局ARG变量

时间:2019-07-04 02:20:41

标签: docker dockerfile docker-multi-stage-build

FROMRUN等指令中使用全局ARG变量

例如,我想在第二构建阶段的${CUDA_VERSION}FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}中使用libcudnn7=${CUDNN_VERSION}-1+cuda${CUDA_VERSION} ARG变量

但是将全局ARG变量${CUDA_VERSION}更改为将FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION} 9.0从9.0更改为9.0.176

在Ubuntu 18.04,Docker-CE 18.09.04

我尝试了很多事情

  1. 在构建阶段更改ARG变量行的位置
  2. 从原始$ {CUDA_VERSION}变量中复制其他ARG变量
  3. 在第一个构建阶段为环境变量创建.profile。然后在第二阶段从第一阶段复制.profile文件,并使用source命令应用
  4. 使用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

1 个答案:

答案 0 :(得分:0)

我按照以下步骤解决了我的问题

我发布了问题 https://github.com/docker/for-linux/issues/713

# Global arguments for Nvidia-docker
ARG CUDA_VERSION=9.0
ARG CUDNN_VERSION=7.4.1.5
ARG UBUNTU_VERSION=16.04
ARG BACKUP=${CUDA_VERSION}

...

RUN echo "/usr/local/cuda-${BACKUP}/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${BACKUP} \
    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/*

...

Dockerfile

# Arguments for Nvidia-Docker
# all combination set in CUDA, cuDNN, Ubuntu is not Incompatible please check REFERENCE OF NVIDIA-DOCKER
# REFERENCE OF NVIDIA-DOCKER 
# https://hub.docker.com/r/nvidia/cuda/

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
ARG BACKUP=${CUDA_VERSION}

# == MutiStage Build ==
# 1-Stage
# Get watcher - if watcher is uploaded on github, remove this line.
FROM python:3.7-alpine

ARG HANDLER_DIR
ARG HANDLER_FILE
ARG HANDLER_NAME
ARG handler_file
ARG handler_name

ARG BACKUP
ARG CUDA_VERSION
RUN echo "${CUDA_VERSION}"
RUN echo "${BACKUP}"

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}

ARG BACKUP
# For Nvidia-Docker
ARG CUDA_VERSION
ARG CUDNN_VERSION

RUN echo "${CUDA_VERSION}"
RUN echo "${BACKUP}"

# Copy directory from 0-stage
ARG HANDLER_DIR
RUN mkdir -p ${HANDLER_DIR}
WORKDIR ${HANDLER_DIR}
COPY --from=0 ${HANDLER_DIR} .

RUN echo "/usr/local/cuda-${BACKUP}/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${BACKUP} \
    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