进入docker容器中安装软件失败解,国外源慢,时间不同步,执行命令权限不够等问题解决办法

进入docker容器中安装软件失败解,时间不同步, 国外源慢,执行命令权限不够 等问题解决办法

首先我进入docker容器中,为了安装一个软件,引出了很多报错问题,报错如下:

1、无法用 ifconfig 或者 ip addr 的方式查看容器内的 ip

2、安装软件 iputils-ping 失败报错 E: Unable to locate package iputils-ping

3、执行 apt-get update 报错 E: Release file for xxxxx/InRelease is not valid yet (invalid for another 1d 20h 59min 59s). Updates for this repository will not be applied. - 原因:时间不同步

4、默认的外国源比较慢,换成国内源

5、执行命令权限不够报错 date: cannot set date: Operation not permitted

# 进入nginx 容器里面
docker exec -it nginx /bin/bash

无法用 ifconfig 或者 ip addr 的方式查看该容器的 ip

解决办法:

找到网上一篇文章《docker查看容器IP的方法》有办法查看。

直接执行下面命令可以查看。

cat /etc/hosts

最后一行对应的就是该容器的 ip 地址。
在这里插入图片描述


安装软件 iputils-ping 报错 E: Unable to locate package iputils-ping

接下来,我想安装一个 iputils-ping 工具,用于测试docker 内的网络连通。
先使用以下命令查看该 docker 容器使用的是什么系统。

cat /etc/os-release
# 或者
cat /etc/*-release

可以看到该容器使用的系统是 Debian 系统,系统版本是 11。所以使用 apt-get 安装软件。
在这里插入图片描述
接下来安装 iputils-ping

apt-get install -y iputils-ping

发现软件安装的特别慢,于是我按 Ctrl + C 停止安装了。

但是我又不小心再次执行了 apt-get install -y iputils-ping 命令,结果报错:

apt-get install -y iputils-ping
# 报错信息,发现安装其他软件也都抱这个错误了,只有少部分软件能安装(比如 apt-get install -y ntpdate  这个可以装)
E: Unable to locate package iputils-ping

看了很多网上的文章,发现都是执行 apt-get update, 再重装软件。

我执行了 apt-get update , 结果报了一个新的错误:

root@54329cea5ead:/# apt-get update
Reading package lists... Done                        
E: Release file for xxxxx/InRelease is not valid yet (invalid for another 2d 19h 59min 59s). Updates for this repository will not be applied.

又看了很多文章,发现这个错是因为源的服务器时间和本地系统时间不同步问题。

默认的外国源比较慢,换成国内源

于是先看看系统使用的是什么源,

执行 cat /etc/apt/sources.list 查看使用的源 。

root@54329cea5ead:/etc/apt# cat sources.list
# deb http://snapshot.debian.org/archive/debian/20211220T000000Z bullseye main
deb http://deb.debian.org/debian bullseye main
# deb http://snapshot.debian.org/archive/debian-security/20211220T000000Z bullseye-security main
deb http://security.debian.org/debian-security bullseye-security main
# deb http://snapshot.debian.org/archive/debian/20211220T000000Z bullseye-updates main
deb http://deb.debian.org/debian bullseye-updates main
root@54329cea5ead:/etc/apt#

可以看到,它默认使用的是 debian 的官方 源,是国外的,下载软件也会很慢。

准备把它换成国内的源。这里有几个大厂的源可以选择。比如 阿里云 和 清华大学 的源。

阿里云的源可以参考阿里云官方的镜像站,找到 Debian 镜像,找到 debian 11.x (bullseye) 版本的源。

在这里插入图片描述

清华大学的源可以参考《清华大学开源软件镜像站》,选择对应的 Debian 版本。

在这里插入图片描述
也可以参考相关博客:
Docker下安装vim 报错 E: Unable to locate package vim

# 备份原来的文件
mv /etc/apt/sources.list /etc/apt/sources.list.bak

# 更换为清华大学的源
echo  "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free" >/etc/apt/sources.list
echo  "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free" >>/etc/apt/sources.list
echo  "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free" >>/etc/apt/sources.list
echo  "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security buster/updates main contrib non-free" >>/etc/apt/sources.list

在这里插入图片描述

执行 apt-get update 报错 E: Release file for xxxxx/InRelease is not valid yet (invalid for another 1d 20h 59min 59s). Updates for this repository will not be applied. - 原因:时间不同步

更换源完成之后,想再次试一下 apt-get update 有没有用。结果还是报原来的错误:

root@54329cea5ead:/# apt-get update
Reading package lists... Done                        
E: Release file for xxxxx/InRelease is not valid yet (invalid for another 1d 20h 59min 59s). Updates for this repository will not be applied.

所以还是要解决时间同步问题。

看到网上的文章《kali更新提示Release file is not valid yet ,Updates for this repository will not be applied

说使用 date 命令直接修改时间, 结果还是报错。

执行命令权限不够报错 date: cannot set date: Operation not permitted

root@54329cea5ead:/# date -s 2024-03-17
date: cannot set date: Operation not permitted
Sun Mar 17 00:00:00 UTC 2024

这是因为在 Docker 容器内部执行 date 命令设置系统时间可能会遇到权限限制的问题,因为 Docker 默认情况下并不允许容器内的进程修改宿主机的时间设置。您可以尝试以下方法来解决这个问题:

使用 --privileged 标志:在运行 Docker 容器时,可以添加 --privileged 标志来提升容器的权限,使其能够修改系统时间。
相关命令可以参考 dokcer 官方文档

在这里插入图片描述

示例:

docker run --privileged -it <your_image_name> /bin/bash

接下来就可以参考上面提到的文章继续操作了《kali更新提示Release file is not valid yet ,Updates for this repository will not be applied

# 设置年月日
date -s 2024-03-07
# 设置时分秒
date -s 23:56:05

注:秒可以不用那么精准,随便写即可。

在这里插入图片描述
于是再次执行 apt-get update ,更新成功。
在这里插入图片描述

安装 vim ,iputils-ping 等软件都可以成功了。
在这里插入图片描述

最近更新

  1. TCP协议是安全的吗?

    2024-03-18 06:40:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-18 06:40:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-18 06:40:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-18 06:40:01       20 阅读

热门阅读

  1. 系统开发中的快速测试与调试策略

    2024-03-18 06:40:01       22 阅读
  2. VSCode配置Python教程

    2024-03-18 06:40:01       23 阅读
  3. [力扣 Hot100]Day51 岛屿数量

    2024-03-18 06:40:01       19 阅读
  4. 【学习笔记】云原生的关键技术初步

    2024-03-18 06:40:01       22 阅读
  5. Install Consul on Kubernetes with Helm

    2024-03-18 06:40:01       24 阅读
  6. 20240313-设计模式

    2024-03-18 06:40:01       19 阅读