docker 的 ubuntu jammy 如何使用 apt 安装软件?

FROM ubuntu:jammy
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list
RUN apt update

我要安装软件

要先替换软件源

替换之后需要 apt update

但是貌似不行?

Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Err:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
W: Some index files failed to download. They have been ignored, or old ones used instead.

改成下面这样也不行

FROM ubuntu:jammy
RUN apt install -y ca-certificates
RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list
RUN apt update

也报错

docker build -t "ponponon/redis-cluster" .
[+] Building 0.3s (5/7)                                                                                                                                                         
 => [internal] load .dockerignore                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                            0.0s
 => [internal] load build definition from Dockerfile                                                                                                                       0.0s
 => => transferring dockerfile: 234B                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ubuntu:jammy                                                                                                            0.0s
 => CACHED [1/4] FROM docker.io/library/ubuntu:jammy                                                                                                                       0.0s
 => ERROR [2/4] RUN apt install -y ca-certificates                                                                                                                         0.2s
------
 > [2/4] RUN apt install -y ca-certificates:
#0 0.216 
#0 0.216 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
#0 0.216 
#0 0.218 Reading package lists...
#0 0.222 Building dependency tree...
#0 0.222 Reading state information...
#0 0.222 Package ca-certificates is not available, but is referred to by another package.
#0 0.222 This may mean that the package is missing, has been obsoleted, or
#0 0.222 is only available from another source
#0 0.222 
#0 0.222 E: Package 'ca-certificates' has no installation candidate
------
Dockerfile:2
--------------------
   1 |     FROM ubuntu:jammy
   2 | >>> RUN apt install -y ca-certificates
   3 |     RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" > /etc/apt/sources.list
   4 |     RUN apt update
--------------------
ERROR: failed to solve: process "/bin/sh -c apt install -y ca-certificates" did not complete successfully: exit code: 100
make: *** [Makefile:12:docker-build] 错误 1

所以这不久变成了先有鸡还是先有蛋的问题了吗?

我安装软件需要网络,需要使用网络需要 ca-certificates,安装 ca-certificates 需要网络?



FROM ubuntu:jammy
RUN source /etc/os-release
RUN cat > /etc/apt/sources.list <<EOF
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
    EOF

报错:

docker build -t "ponponon/redis-cluster" .
[+] Building 0.2s (2/2) FINISHED                                                                                                                                                                                                                                                                  
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                         0.0s
 => => transferring dockerfile: 589B                                                                                                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                              0.0s
Dockerfile:3
--------------------
   2 |     RUN source /etc/os-release
   3 | >>> RUN cat > /etc/apt/sources.list <<EOF
   4 | >>>     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
   5 | >>>     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
   6 | >>>     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
   7 | >>>     deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
   8 | >>>     EOF
   9 |     
--------------------
ERROR: failed to solve: unterminated heredoc
make: *** [Makefile:12: docker-build] Error 1

问题解决

FROM ubuntu:jammy
RUN . /etc/os-release && cat > /etc/apt/sources.list <<EOF
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
    deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
EOF

注意需要把 source /etc/os-release 换成 . /etc/os-release。原因是打包的时候,使用的是 sh,而不是 bash 导致的

阅读 3.4k
1 个回答

要用http源才行,docker中的apt是没有安装apt-transport-https这个包的,是无法使用https源的

而且你就一个仓库也不对,会有一些包缺失的,按照tuna官方文档,应该配置至少4个源 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

一个简单的shell脚本自适应ubuntu镜像:

source /etc/os-release
cat > /etc/apt/sources.list <<EOF
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME} main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ ${VERSION_CODENAME}-security main restricted universe multiverse
EOF
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题