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

18 篇文章 0 订阅

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值