mac系统docker运行centos8-ssh

背景

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

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

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

系统:MacOS Monterey 12.7.1 芯片:intel

前提说明:之前尝试过运行centos7,遇到很多问题,最终也没有完全解决掉。所以,使用centos latest,即centos8

docker 桌面版,下载安装文件,一步步就行了。我们重要关注centos的安装和运行。

centos8安装

拉取(下载)centos8的镜像

不指定版本,默认安装最新的。此时即centos8

  • $ docker pull centos

想指定版本,访问官网 centos images

截屏2024-11-17-6

如下:

1
2
3
4
5
6
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

运行centos8

  • $ docker run -d -p 5002:22 –name test-centos8 –privileged=true centos /usr/sbin/init

说明

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

进入centos8容器中

-$ docker exec -it test-centos8 bash

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

1
2
3
4
5
6
7
8
[root@5103c43cdab0 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 11:56 ? 00:00:00 /usr/sbin/init
root 30 1 0 11:56 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 32 1 0 11:56 ? 00:00:00 /usr/lib/systemd/systemd-udevd
dbus 98 1 0 11:56 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 100 0 0 11:57 pts/0 00:00:00 bash
root 114 100 0 11:57 pts/0 00:00:00 ps -ef

截屏2024-11-17-8

安装 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
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/8/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
  • $ ls -l
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    -rw-r--r-- 1 root root  718 Nov 17 12:01 CentOS-Linux-AppStream.repo
    -rw-r--r-- 1 root root 703 Nov 17 12:00 CentOS-Linux-BaseOS.repo
    -rw-r--r-- 1 root root 1130 Nov 10 2020 CentOS-Linux-ContinuousRelease.repo
    -rw-r--r-- 1 root root 318 Nov 10 2020 CentOS-Linux-Debuginfo.repo
    -rw-r--r-- 1 root root 732 Nov 10 2020 CentOS-Linux-Devel.repo
    -rw-r--r-- 1 root root 704 Nov 10 2020 CentOS-Linux-Extras.repo
    -rw-r--r-- 1 root root 719 Nov 10 2020 CentOS-Linux-FastTrack.repo
    -rw-r--r-- 1 root root 740 Nov 10 2020 CentOS-Linux-HighAvailability.repo
    -rw-r--r-- 1 root root 693 Nov 10 2020 CentOS-Linux-Media.repo
    -rw-r--r-- 1 root root 706 Nov 10 2020 CentOS-Linux-Plus.repo
    -rw-r--r-- 1 root root 724 Nov 10 2020 CentOS-Linux-PowerTools.repo
    -rw-r--r-- 1 root root 898 Nov 10 2020 CentOS-Linux-Sources.repo

修改以下文件中的mirrorlist

  • $ vi CentOS-Linux-BaseOS.repo
  • $ vi CentOS-Linux-AppStream.repo
  • $ vi CentOS-Linux-Extras.repo

修改CentOS-Linux-BaseOS.repo

1
2
3
4
5
注释掉mirrorlist
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra

打开baseurl,并修改为
baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/

修改CentOS-Linux-AppStream.repo

1
2
3
4
5
注释掉mirrorlist
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra

打开baseurl,并修改为
baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/

修改CentOS-Linux-Extras.repo

1
2
3
4
5
注释掉mirrorlist
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=Extras&infra=$infra

打开baseurl,并修改为
baseurl=https://vault.centos.org/centos/$releasever/Extras/$basearch/os/

以上文件中,如果enabled=0,修改为enabled=1

保存,退出。更新yum源信息,执行如下

  • $ yum -y update

我们再重新执行ssh安装

  • $ yum install -y openssh-server vim passwd

安装成功了

1
2
3
4
Installed:
openssh-8.0p1-10.el8.x86_64 openssh-server-8.0p1-10.el8.x86_64 passwd-0.80-3.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64 vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
...
Complete!

启动ssh

-$ systemctl start sshd.service

-$ ps -ef

可以看到ssh的进程了

1
2
3
4
5
6
7
8
9
10
11
UID          PID    PPID  C STIME TTY          TIME CMD
root 1 0 0 11:56 ? 00:00:00 /usr/sbin/init
root 30 1 0 11:56 ? 00:00:00 /usr/lib/systemd/systemd-journald
root 32 1 0 11:56 ? 00:00:00 /usr/lib/systemd/systemd-udevd
dbus 98 1 0 11:56 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root 100 0 0 11:57 pts/0 00:00:00 bash
root 274 1 0 12:07 ? 00:00:00 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-
root 280 274 0 12:10 ? 00:00:00 sshd: root [priv]
root 282 280 0 12:10 ? 00:00:00 sshd: root@pts/1
root 283 282 0 12:10 pts/1 00:00:00 -bash
root 301 100 0 12:10 pts/0 00:00:00 ps -ef

设置centos8容器的密码

  • $ whoami
    1
    root

我们给root账户设置密码

  • $ passwd

按照提示,输入两次新密码,及时生效。

客户端通过ssh连接centos8镜像

我们新打开一个终端窗口,执行ssh命令

  • $ ssh root@localhost -p 5002
    1
    2
    3
    root@localhost's password:
    Last failed login: Sun Nov 17 12:10:14 UTC 2024 from 192.20.0.1 on ssh:notty
    There was 1 failed login attempt since the last successful login.

可以看到,连接成功了。

我们已经做好了含有ssh能力的centos8镜像。以后我就可以复用这个镜像了,可以在新机器上拉取pull这个镜像直接使用了。以它为基准,继续扩展,如安装mysql,nginx,redis等等。大大节省时间和提高效率。

我们只需要将这个做好的镜像push到docker hub即可。

镜像上传到docker hub

提交镜像

  • $ docker commit -m “has ssh action” -a “test123” test-centos test-centos:v1
    1
    说明:docker commit -m “镜像描述” -a “制作者” 容器名 镜像名

推送镜像到docker hub

  • $ docker push test-centos8:v1

报错了,提示没有权限

1
2
3
4
The push refers to repository [docker.io/library/test-centos8]
f6e6bced2702: Preparing
74ddd0ec08fa: Preparing
denied: requested access to the resource is denied

  • $ docker image ls
    查看镜像
    1
    2
    REPOSITORY                        TAG        IMAGE ID       CREATED          SIZE
    test-centos8 v1 b3fd18bb5d2d 24 minutes ago 302MB

解决方案:我们重新tag下,将镜像加上你的docker hub的用户名。如yourUserName=test123

  • $ docker tag test-centos8:v1 test123/test-centos8:v1

再次push到docker hub

  • $ docker push test123/test-centos8:v1
    1
    2
    3
    4
    The push refers to repository [docker.io/{yourUserName}/test-centos8]
    f6e6bced2702: Pushed
    74ddd0ec08fa: Pushed
    v1: digest: sha256:123ed4e883cecef8180433b3a21d11b1e383d9da68bac01694b1c84575e3e72b size: 333

成功了。

我们通过docker desktop也可以看到hub上的镜像,如图

截屏2024-11-17-9

附录

Could not retrieve mirrorlist的解决方法

参考

M1芯片DockerDeskTop安装CentOS胎教级教程