在linux虚拟机上面配置Docker时所踩过的坑

一开始,老师要求我们在虚拟机上面部署Docker,以便完成实验。

我最初是跟着csdn的几篇文章去配置的。

第一步: 安装依赖包

yum install -y yum-utils device-mapper-persistent-data lvm2

接着发现报错

[root@node2 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"


 One of the configured repositories failed (未知),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

于是去查csdn发现可以按照这篇文章的做法来解决这个问题

文章链接:【成功解决!】Linux时间同步ntp安装报错Could not retrieve mirrorlist解决方案_yum -y install ntp报错-CSDN博客

[root@node2 ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@node2 ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[root@node2 ~]# sudo yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: base extras updates
Cleaning up list of fastest mirrors
[root@node2 ~]# yum install -y ntp
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
base                                                                                                                                                                                       | 3.6 kB  00:00:00     
extras                                                                                                                                                                                     | 3.6 kB  00:00:00     
updates                                                                                                                                                                                    | 3.6 kB  00:00:00     
(1/6): base/7/x86_64/group_gz                                                                                                                                                              | 153 kB  00:00:00     
(2/6): extras/7/x86_64/group_gz                                                                                                                                                            | 153 kB  00:00:00     
(3/6): extras/7/x86_64/primary_db                                                                                                                                                          | 6.1 MB  00:00:01     
(4/6): updates/7/x86_64/group_gz                                                                                                                                                           | 153 kB  00:00:00     
(5/6): base/7/x86_64/primary_db                                                                                                                                                            | 6.1 MB  00:00:02     
(6/6): updates/7/x86_64/primary_db                                                                                                                                                         | 6.1 MB  00:00:01     
软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 已安装并且是最新版本
无须任何处理
[root@node2 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 yum-utils-1.1.31-54.el7_8.noarch 已安装并且是最新版本
软件包 device-mapper-persistent-data-0.8.5-3.el7.x86_64 已安装并且是最新版本
软件包 7:lvm2-2.02.187-6.el7.x86_64 已安装并且是最新版本
无须任何处理

第二步: 设置阿里云docker-ce镜像源

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

这一步没有报错,正常运行。

[root@node2 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

第三步: docker-ce安装

yum install -y docker-ce

接着发现报错

[root@node2 ~]# yum install -y docker-ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
docker-ce-stable                                                                                                                                                                           | 3.5 kB  00:00:00     
(1/2): docker-ce-stable/7/x86_64/updateinfo                                                                                                                                                |   55 B  00:00:00     
(2/2): docker-ce-stable/7/x86_64/primary_db                                                                                                                                                | 152 kB  00:00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.3.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 containerd.io >= 1.6.24,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-cli,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 docker-ce-rootless-extras,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.6.33-3.1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 containerd.io-1.6.33-3.1.el7.x86_64 需要
---> 软件包 docker-ce.x86_64.3.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
---> 软件包 docker-ce-cli.x86_64.1.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 docker-buildx-plugin,它被软件包 1:docker-ce-cli-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 docker-compose-plugin,它被软件包 1:docker-ce-cli-26.1.4-1.el7.x86_64 需要
---> 软件包 docker-ce-rootless-extras.x86_64.0.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 fuse-overlayfs >= 0.7,它被软件包 docker-ce-rootless-extras-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 slirp4netns >= 0.4,它被软件包 docker-ce-rootless-extras-26.1.4-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 containerd.io.x86_64.0.1.6.33-3.1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 containerd.io-1.6.33-3.1.el7.x86_64 需要
---> 软件包 docker-buildx-plugin.x86_64.0.0.14.1-1.el7 将被 安装
---> 软件包 docker-ce.x86_64.3.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 container-selinux >= 2:2.74,它被软件包 3:docker-ce-26.1.4-1.el7.x86_64 需要
---> 软件包 docker-ce-rootless-extras.x86_64.0.26.1.4-1.el7 将被 安装
--> 正在处理依赖关系 fuse-overlayfs >= 0.7,它被软件包 docker-ce-rootless-extras-26.1.4-1.el7.x86_64 需要
--> 正在处理依赖关系 slirp4netns >= 0.4,它被软件包 docker-ce-rootless-extras-26.1.4-1.el7.x86_64 需要
---> 软件包 docker-compose-plugin.x86_64.0.2.27.1-1.el7 将被 安装
--> 解决依赖关系完成
错误:软件包:3:docker-ce-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:container-selinux >= 2:2.74
错误:软件包:docker-ce-rootless-extras-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:slirp4netns >= 0.4
错误:软件包:docker-ce-rootless-extras-26.1.4-1.el7.x86_64 (docker-ce-stable)
          需要:fuse-overlayfs >= 0.7
错误:软件包:containerd.io-1.6.33-3.1.el7.x86_64 (docker-ce-stable)
          需要:container-selinux >= 2:2.74
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest

于是去查csdn发现可以按照这篇文章的做法来解决这个问题

文章链接:Linux docker 安装时报错,提示:需要 container-selinux >= 2:2.74_需要:container-selinux >= 2:2.74-CSDN博客

[root@node2 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2024-11-15 15:21:27--  http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 183.216.187.202, 183.216.187.199, 183.216.187.198, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|183.216.187.202|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[========================================================================================================================================================================>] 2,523       --.-K/s 用时 0s      

2024-11-15 15:21:27 (320 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

[root@node2 ~]# yum makecache fast
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                                                                       | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                                           | 3.5 kB  00:00:00     
extras                                                                                                                                                                                     | 2.9 kB  00:00:00     
updates                                                                                                                                                                                    | 2.9 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                                          | 253 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                                         |  27 MB  00:00:05     
元数据缓存已建立
[root@node2 ~]# yum install docker-ce -y
----(这里显示的内容很多,所以就省略了大部分,只留了下面这一小部分,
也就是安装成功时所显示的最下面的内容)
已安装:
  docker-ce.x86_64 3:26.1.4-1.el7                                                                                                                                                                                 

作为依赖被安装:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8         containerd.io.x86_64 0:1.6.33-3.1.el7               docker-buildx-plugin.x86_64 0:0.14.1-1.el7         docker-ce-cli.x86_64 1:26.1.4-1.el7        
  docker-ce-rootless-extras.x86_64 0:26.1.4-1.el7            docker-compose-plugin.x86_64 0:2.27.1-1.el7         fuse-overlayfs.x86_64 0:0.7.2-6.el7_8              fuse3-libs.x86_64 0:3.6.1-4.el7            
  slirp4netns.x86_64 0:0.4.3-4.el7_8                        

完毕!

第四步:配置Docker 镜像以及尝试拉取mysql镜像

这时候Docker已经安装好了

[root@node2 ~]# docker version
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45
 Go version:        go1.21.11
 Git commit:        5650f9b
 Built:             Wed Jun  5 11:32:04 2024
 OS/Arch:           linux/amd64
 Context:           default

先别开启docker,还需要进行Docker 镜像配置(如果已经开启,就先关闭docker)

创建这个文件

sudo vi /etc/docker/daemon.json

然后把以下镜像源复制进去(这个镜像源有可能会失效,如果失效就得去网上找别的镜像源)

{
  "registry-mirrors": [
    "http://hub-mirror.c.163.com",
    "https://mirror.ccs.tencentyun.com",
    "https://docker-proxy.741001.xyz",
    "https://registry.docker-cn.com"
  ]
}

接着先使用 systemctl start docker 命令启动docker,然后使用docker info 命令查看一下Docker 镜像配置是否成功,如下方信息显示,显然成功了。

[root@node2 ~]# docker info
Client: Docker Engine - Community
 Version:    26.1.4
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.27.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 26.1.4
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: d2d58213f83a351ca8f528a95fbd145f5654e957
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 3.10.0-1160.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.777GiB
 Name: node2
 ID: b3f9445f-a5f7-4f3d-a620-30466b9d4db4
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://docker.1panel.dev/
  https://docker.fxxk.dedyn.io/
  https://docker.xn--6oq72ry9d5zx.cn/
  https://docker.m.daocloud.io/
  https://a.ussh.net/
  https://docker.zhai.cm/
 Live Restore Enabled: false

第五步:最后试着使用 docker pull mysql 命令拉取一下mysql镜像(这里如果不指定mysql版本就会默认拉取最新版本的mysql)

[root@node2 ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
f1a9f94fc2db: Pull complete 
f98254a2b688: Pull complete 
6ad83e89f981: Pull complete 
a42d733ea779: Pull complete 
6fd1af2601dd: Pull complete 
0233a63dc5cd: Pull complete 
5f31e56c9bea: Pull complete 
c0fb96d14e5b: Pull complete 
d57074c62694: Pull complete 
7030c241d9b8: Pull complete 
Digest: sha256:2be51594eba5983f47e67ff5cb87d666a223e309c6c64450f30b5c59a788ea40
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

第六步:最后使用 docker images 命令查看一下

[root@node2 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        latest    10db11fef9ce   4 weeks ago   602MB
[root@node2 ~]# 

下面是一些我配置的过程中所参考的文章链接:

1.

【成功解决!】Linux时间同步ntp安装报错Could not retrieve mirrorlist解决方案_yum -y install ntp报错-CSDN博客

 2.Linux docker 安装时报错,提示:需要 container-selinux >= 2:2.74_需要:container-selinux >= 2:2.74-CSDN博客

3.Docker 镜像配置_docker配置镜像-CSDN博客 

4.Linux安装最新版Docker完整教程(建议收藏)_linux安装docker教程-CSDN博客 

 5.docker 常用命令大全_docker常用命令-CSDN博客

作者:"感谢您的耐心阅读,至此,所有问题已基本解答,感谢您的关注与支持!"

为了在Alpine Linux虚拟机配置Docker环境并确保安全访问,首先推荐查看这份资料:《在虚拟机配置Alpine LinuxDocker》。这份资料将帮助你理解在虚拟机中安装Alpine Linux并设置Docker的完整过程。 参考资源链接:[在虚拟机配置Alpine LinuxDocker](https://wenku.csdn.net/doc/6412b719be7fbd1778d49158?spm=1055.2569.3001.10343) 启动虚拟机并以root用户登录后,首先进行基本的系统配置,包括设置主机名、密码、区和网络等。在安装过程中,选择将Alpine Linux安装到硬盘,并使用LVM管理磁盘空间以提高灵活性。 一旦系统安装完成,需要为Docker配置相应的软件仓库。通过编辑`/etc/apk/repositories`文件添加Docker官方仓库,更新软件源索引后,使用`apk add docker`命令安装Docker。为了使Docker在系统启动自动运行,执行`rc-update add docker boot`。之后,启动Docker服务并检查版本确保安装成功。 对于SSH的配置,需要确认已安装SSH服务器软件包,并通过修改`/etc/ssh/sshd_config`文件来配置安全设置,比如更改默认端口、禁用root登录和密码认证等。完成配置后,重启SSH服务并从另一台机器上通过SSH安全连接到Alpine Linux虚拟机。 掌握这些步骤后,你将能够在Alpine Linux上成功搭建和配置Docker环境,并通过SSH进行安全访问。为了进一步加深理解和学习更多相关知识,建议继续查阅《在虚拟机配置Alpine LinuxDocker》中关于网络配置和软件安装的高级内容。 参考资源链接:[在虚拟机配置Alpine LinuxDocker](https://wenku.csdn.net/doc/6412b719be7fbd1778d49158?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值