mac系统docker运行centos7

背景

最初的事情是想观察mysql与java程序网络交互细节。结果在使用 netstat 的时候,发现 macos系统不支持 -p 参数。这个在linux 上常用的参数却成了问题。

显然,是系统环境的问题。所以,可见,统一环境的必要性,这时,docker的作用就显示出来了。

docker 桌面版安装,docker上centos7安装与运行,就成为必须要做的事情。

NOTE: 通过docker 与通过 dockerfile安装是不同的方式。我的理解通过docker是入门,初学者更容易操作和接受;dockerfile方式更正式,是推荐的方式,因为后面开始真正使用docker的时候,都是通过dockerfile来用的。

参见:mac系统通过dockerfile运行centos8-ssh

安装

google 搜索 docker centos7 安装,会出现一堆的信息。我看了下,都不是我想要的。其实,docker官网拥有所有的官方的镜像。

访问 hub.docker.com,搜索centoscentos纯纯的官方镜像就在这,如下图
截屏2024-11-17-1

截屏2024-11-17-3

从上图,可以找到地方,也可直接点击链接 centos images

我们使用 docker pull centos:centos7,复制到终端执行 docker pull centos:centos7,表示拉取centos7的镜像

拉取(下载)centos7的镜像

  • $ docker pull centos:centos7
    如下:
    1
    2
    3
    4
    5
    centos7: Pulling from library/centos
    2d473b07cdd5: Pull complete
    Digest: sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4
    Status: Downloaded newer image for centos:centos7
    docker.io/library/centos:centos7

查看centos7镜像

  • $ docker images
    1
    2
    3
    4
    REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
    nginx latest 60c8a892f36f 6 weeks ago 192MB
    docker/welcome-to-docker latest c1f619b6477e 12 months ago 18.6MB
    centos centos7 eeb6ee3f44bd 3 years ago 204MB

运行centos7

后台启动容器

  • $ docker run -d –privileged –name centos-mytest centos:centos7 /usr/sbin/init

说明

1
2
--privileged:特权模式,相当于管理员权限
/usr/sbin/init: 启动容器之后可以使用systemctl方法

进入centos7容器中

-$ docker exec -it centos-mytest /bin/bash

执行命令后就进入了centos7容器内。这时,可以执行命令了,如查看容器的进程 ps -ef

1
2
3
4
5
6
[root@456b168ad8f2 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 05:44 ? 00:00:00 /usr/sbin/init
root 7 0 0 05:46 pts/0 00:00:00 /bin/bash
root 21 7 0 05:46 pts/0 00:00:00 ps -ef
[root@456b168ad8f2 /]#

截屏2024-11-17-4

安装 ssh

为了以后可以直接使用这个centos7容器,需要安装 ssh 服务,并赋给一个对外的端口,同时设置开机启动。以便外部访问

安装 ssh

  • $ yum install -y openssh-server

报错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: HTTP Error 502 - Bad Gateway


One of the configured repositories failed (Unknown),
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

开始我以为是网络问题,通过ping www.baidu.com 是通的。排除网络问题

这个错误信息折腾了一下午,最后通过搜索这个关键词 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was 找到了解决方案

大概意思是mirrorlist不提供维护了,需要将mirrorlist改为baseurl;同时需要将baseurl的地址mirror.centos.org 改为 vault.centos.org

你也许会一头雾水,mirrorlist是啥,在哪呢?

  • $ cd /etc/yum.repos.d
  • $ ll
    1
    2
    3
    4
    5
    6
    7
    8
    9
    -rw-r--r-- 1 root root 1660 Nov 17 09:18 CentOS-Base.repo
    -rw-r--r-- 1 root root 1664 Nov 17 09:15 CentOS-Base.repo.backup
    -rw-r--r-- 1 root root 1309 May 21 14:48 CentOS-CR.repo
    -rw-r--r-- 1 root root 649 May 21 14:48 CentOS-Debuginfo.repo
    -rw-r--r-- 1 root root 630 May 21 14:48 CentOS-Media.repo
    -rw-r--r-- 1 root root 1331 May 21 14:48 CentOS-Sources.repo
    -rw-r--r-- 1 root root 9454 May 21 14:48 CentOS-Vault.repo
    -rw-r--r-- 1 root root 314 May 21 14:48 CentOS-fasttrack.repo
    -rw-r--r-- 1 root root 616 May 21 14:48 CentOS-x86_64-kernel.repo

截屏2024-11-17-5

mirrorlist 这个链接就在 CentOS-Base.repo 文件中,

  • $ vi CentOS-Base.repo

按照解决方案修改CentOS-Base.repo,将mirrorlist改为baseurl;同时需要将baseurl的地址mirror.centos.org 改为 vault.centos.org

保存,退出。执行如下

  • $ yum -y update

我们再重新执行ssh安装

  • $ yum install -y openssh-server

安装成功了

1
2
3
4
Installed:
openssh-server.x86_64 0:7.4p1-23.el7_9

Complete!

对于这个问题的解决过程的思考:
查找问题的过程中,我多次通过google搜索如下两个错误信息
Cannot find a valid baseurl for repo: base/7/x86_6414: HTTP Error 502 - Bad Gateway
找到的答案,尝试后都是无效的。

最后反复看这大段的信息,通过google docker Could not retrieve mirrorlist 找到解决方法。

回头看,整个错误信息的开头就是 Could not retrieve mirrorlist ...,却完美的错过了,用了一下午的时间。

反思:最先出现的错误,才是最本质的

继续docker运行centos7

启动ssh

-$ systemctl start sshd.service

报错了

1
Failed to get D-Bus connection: No such file or directory

很遗憾,这个问题没有解决掉

猜想可能是这个镜像与我的mac机器不兼容性太强了。

转换大思路

另辟蹊径,从头再来,换个思路,重新 google mac docker centos

果然,最新的centos是centos8,安装和运行非常顺滑
macOS M1芯片DockerDeskTop安装CentOS胎教级教程

总结

这样一篇反面教材的文章,希望可以从错误中得到锻炼和提升

附录

Could not retrieve mirrorlist的解决方法

docker简单运行centos7

解决问题后的 CentOS-Base.repo 文件,内容如下

  • $ cat CentOS-Base.repo
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    # ...

    [base]
    name=CentOS-$releasever - Base
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
    baseurl=http://vault.centos.org/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    #released updates
    [updates]
    name=CentOS-$releasever - Updates
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
    baseurl=http://vault.centos.org/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
    baseurl=http://vault.centos.org/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus
    #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
    baseurl=http://vault.centos.org/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7