Dockerfile封装遇到returned a non-zero code: 2报错

在使用dockerfile封装镜像的时候,每次运行到下面这条指令

 RUN pip3 install tensorflow==1.8 -i https://pypi.tuna.tsinghua.edu.cn/simple/

就会报错

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 620, in _prepare_file
    session=self.session, hashes=hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 821, in unpack_url
    hashes=hashes
  File "/usr/lib/python3/dist-packages/pip/download.py", line 659, in unpack_http_url
    hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 882, in _download_http_url
    _download_url(resp, link, content_file, hashes)
  File "/usr/lib/python3/dist-packages/pip/download.py", line 603, in _download_url
    hashes.check_against_chunks(downloaded_chunks)
  File "/usr/lib/python3/dist-packages/pip/utils/hashes.py", line 46, in check_against_chunks
    for chunk in chunks:
  File "/usr/lib/python3/dist-packages/pip/download.py", line 571, in written_chunks
    for chunk in chunks:
  File "/usr/lib/python3/dist-packages/pip/utils/ui.py", line 139, in iter
    for x in it:
  File "/usr/lib/python3/dist-packages/pip/download.py", line 560, in resp_read
    decode_content=False):
  File "/usr/share/python-wheels/urllib3-1.22-py2.py3-none-any.whl/urllib3/response.py", line 436, i                                                                                           n stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/usr/share/python-wheels/urllib3-1.22-py2.py3-none-any.whl/urllib3/response.py", line 401, i                                                                                           n read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "/usr/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/share/python-wheels/urllib3-1.22-py2.py3-none-any.whl/urllib3/response.py", line 307, i                                                                                           n _error_catcher
    raise ReadTimeoutError(self._pool, None, 'Read timed out.')
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443)                                                                                           : Read timed out.
The command '/bin/sh -c pip3 install tensorflow==1.8 -i https://pypi.tuna.tsinghua.edu.cn/simple/' r                                                                                           eturned a non-zero code: 2

在网上找不到类似的错误情况,于是请教我们组的同事,按照dockerfile中的步骤一步一步排查,最后的解决方案如下:

解决方法分为三步走:

  1. 在封装的上下文环境中,创建sources.list
vim sources.list
  1. 将下面这段内容复制到sources.list中去,这里我的dockerfile使用的ubuntu18.04的镜像,所以使用的下面的镜像语句,如果是别的版本的ubuntu,那么需要根据实际情况修改,可以参考https://opsx.alibaba.com/mirror这个阿里的开源镜像站。
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  1. 在Dockerfile中涉及下载的内容之前,添加下面的语句(写一次即可)这个语句的作用就是覆盖ubuntu镜像的下载源设置
ADD sources.list /etc/apt/

完成这三步之后,重新运行sudo docker build 指令,然后会发现现在下载的特别快,然后很快就完成了docker镜像的包装。

可能的原因,因为ubuntu 默认的下载服务源是国外镜像,比较慢,替换成阿里的源之后,就比较快

你可能感兴趣的:(Dockerfile封装遇到returned a non-zero code: 2报错)