Error response from daemon: Dockerfile parse error line xxx: unknown instruction: xxx

Error response from daemon: Dockerfile parse error line xxx: unknown instruction: xxx

前言

這是在將環境建構的腳本改寫成Dockerfile時所出現的錯誤。
經查詢的結果發現,原本在Linux環境裡可以運行的多行指令,到了Dockerfile,必須用\相連才可以運行。

錯誤訊息

Sending build context to Docker daemon 154.2MB
Error response from daemon: Dockerfile parse error line 11: unknown instruction: WGET

以下是造成這個錯誤的Dockerfile:

FROM ubuntu:16.04

RUN apt-get -y update

# We do this conditionally because it saves us some downloading if the
# version is the same.

RUN apt-get install -y wget bzip2

RUN apt-get install -y wget && if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
        wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh;
    else
        wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
    fi

發生原因

看到了Error response from daemon: Dockerfile parse error line 1: unknown instruction: #中jbrahy的回答,才發現在Dockerfile解析的過程中,wget xxx似乎被視為一條獨立的指令,也難怪錯誤訊息會說unknown instruction: WGET

解決辦法

Best practices for writing Dockerfiles - RUN中說明如果需要在RUN後面使用多行的命令時,需要在結尾加上\,這樣他們才會被視為一道指令。

以下是修改過後的Dockerfile:

FROM ubuntu:16.04

RUN apt-get -y update

# We do this conditionally because it saves us some downloading if the
# version is the same.

RUN apt-get install -y wget bzip2

RUN apt-get install -y wget && if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then \
        wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh; \
    else \
        wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; \
    fi

執行結果

使用docker build . -t xxx的執行結果:
docker build
可以看到RUN apt-get install -y wget ...原來分散在多行的指令到了這裡(Step 4/17)這壓縮成一行來執行。

參考連結

Error response from daemon: Dockerfile parse error line 1: unknown instruction: #
Best practices for writing Dockerfiles - RUN

猜你喜欢

转载自blog.csdn.net/keineahnung2345/article/details/86063328