V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MrSheng
V2EX  ›  Docker

Docker build ubuntu 无法执行 apt-get update

  •  
  •   MrSheng · 2019-06-20 17:08:23 +08:00 · 8496 次点击
    这是一个创建于 2015 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是一个老问题了,但确实没找到原因。

    Dockerfile 内容:

    FROM ubuntu:xenial
    RUN apt-get update && apt-get upgrade -y
    RUN apt-get install -y net-tools
    CMD ifconfig
    

    build 时报错:

    Step 1/4 : FROM ubuntu:xenial
     ---> 13c9f1285025
    Step 2/4 : RUN apt-get update && apt-get upgrade -y
     ---> Running in 7d9b2ce81413
    Err:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
      Temporary failure resolving 'security.ubuntu.com'
    Err:2 http://archive.ubuntu.com/ubuntu xenial InRelease
      Temporary failure resolving 'archive.ubuntu.com'
    Err:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
      Temporary failure resolving 'archive.ubuntu.com'
    Err:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
      Temporary failure resolving 'archive.ubuntu.com'
    Reading package lists...
    W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'archive.ubuntu.com'
    W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
    W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
    W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
    W: Some index files failed to download. They have been ignored, or old ones used instead.
    Reading package lists...
    Building dependency tree...
    Reading state information...
    Calculating upgrade...
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
     ---> 2bbbf2281fbf
    Removing intermediate container 7d9b2ce81413
    Step 3/4 : RUN apt-get install -y net-tools
     ---> Running in ce96aba3aea8
    Reading package lists...
    Building dependency tree...
    Reading state information...
    E: Unable to locate package net-tools
    The command '/bin/sh -c apt-get install -y net-tools' returned a non-zero code: 100
    

    很明显是 apt-get 无法 update

    已经尝试的解决方式:

    1、更改为国内源(阿里源与 163 源,也测试过 sed 直接修改),无效:

    RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
    echo 'deb http://mirrors.163.com/debian/ jessie main non-free contrib' > /etc/apt/sources.list && \
    echo 'deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib' >> /etc/apt/sources.list
    

    报错原因同之前:

    Err:1 http://mirrors.163.com/debian jessie InRelease
      Temporary failure resolving 'mirrors.163.com'
    

    2、尝试更改 DNS ( 1 )我需要在 docker build 时指定 DNS,不是 run 的时候 ( 2 ) DNS 中已经有 114.114.114.114

    PS:宿主机可以执行 apt-update

    3 条回复    2019-09-23 09:52:59 +08:00
    XSG
        1
    XSG  
       2019-07-01 16:34:23 +08:00
    你的源错了
    看下你 FROM 的镜像是 xenial,但是修改的 apt 源是 jessie,不匹配!
    pacoxu
        2
    pacoxu  
       2019-07-02 13:21:30 +08:00
    https://github.com/pacoxu/dao-tomcat/tree/master/samples

    写了个简单的例子,找了个 ubuntu 的 aliyun 源

    docker build .
    Sending build context to Docker daemon 3.584kB
    Step 1/3 : FROM ubuntu:xenial
    xenial: Pulling from library/ubuntu
    Digest: sha256:a4d8e674ee993e5ec88823391de828a5e9286a1597b731eaecaaf9066cfdf539
    Status: Downloaded newer image for ubuntu:xenial
    ---> 13c9f1285025
    Step 2/3 : COPY sources.list /etc/apt/sources.list
    ---> 93899785ec2a
    Step 3/3 : RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*
    ---> Running in 2369ec47a573
    Get:1 http://mirrors.aliyun.com/ubuntu xenial InRelease [247 kB]
    Get:2 http://mirrors.aliyun.com/ubuntu xenial-security InRelease [109 kB]
    Get:3 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease [109 kB]
    MrSheng
        3
    MrSheng  
    OP
       2019-09-23 09:52:59 +08:00   ❤️ 2
    最后解决的方法是 docker build 指定 --network=host
    问题中已经说明:宿主机中可以 update,容器中不可以,应该是 docker 配置问题

    PS:
    更改源版本不对是复制的代码,实际上是正确的版本
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1119 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 18:45 · PVG 02:45 · LAX 10:45 · JFK 13:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.