Docker中安装CentOS 6.x

Linux大全评论606 views阅读模式

当使用Docker安装最新的CentOS镜像时,拉取的是CentOS 7镜像,使用时会出现 Failed to get D-Bus connection: Operation not permitted 的坑,尝试了使用官方介绍的方法来处理,也是挺复杂的。最后还是决定在Docker中安装CentOS6镜像,避免这个烦心的问题。

使用官方的Docker hub拉取CentOS6镜像时,总是会出现下载失败,试了好多次都是这样

docker pull centos:6

后来通过配置国内的Docker镜像源,以下载CentOS6镜像

1、配置国内docker镜像源

使用中国科学大学的docker镜像缓存,在配置文件 /etc/docker/daemon.json 中加入以下内容(如果没有该文件,则新增):

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

重新启动dockerd

sudo service docker restart

2、拉取centos6镜像

docker pull centos:6

3、创建centos6容器

docker run --name mycentos -it centos:6 /bin/bash

进入到centos6之后,默认已经是有which、ifconfig、less、ip等常用命令了,而如果是在docker中使用centos7镜像时,是没有以上这些命令的,要重新安装

4、使用yum安装ssh

设置国内的yum镜像源(阿里云的centos镜像源),下载速度会大大提升,使用默认yum镜像也行,速度慢很多

curl http://mirrors.aliyun.com/repo/Centos-6.repo > /etc/yum.repos.d/CentOS-Base-6-aliyun.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
yum makecache
yum install -y openssh-clients openssh-server

注意:刚开始进入到docker中的centos6时,是没有service这个命令的,而当安装 openssh 时,里面会依赖到 initscripts 软件包,这个将自动进行安装,安装后就有 service 命令可以使用了,很方便

启动ssh

[root@14c0ec213102 /]# chkconfig sshd on
[root@14c0ec213102 /]# service sshd start
Generating SSH2 RSA host key:                              [ OK ]
Generating SSH1 RSA host key:                              [ OK ]
Generating SSH2 DSA host key:                              [ OK ]
Starting sshd:                                             [ OK ]

在docker的centos6中,启动ssh时,会自动创建ssh的rsa、dsa密钥,而如果是在docker中的centos7刚开始启动ssh时,则需要创建相应的密钥,否则会报相关的密钥不存在

# docker 中首次启动 centos 7 的 ssh
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

5、修改ssh配置

启动好ssh后,还要修改一下配置,否则会连接后自动关闭,连接本机或另的机器ssh连接过来都会

[root@14c0ec213102 /]# ssh localhost
root@localhost's password: Connection to localhost closed.

修改ssh的配置文件

vi /etc/ssh/sshd_config

将第97行的UsePAM yes,改为 UsePAM no

保存退出,重启ssh

[root@14c0ec213102 /]# service sshd restart
Stopping sshd:                                             [ OK ]
Starting sshd:                                             [ OK ]

现在就能正常使用ssh连接访问了

[root@14c0ec213102 /]# ssh localhost
root@localhost's password: Last login: Sun Jun 4 15:50:46 2017 from 172.17.42.1 

将UsePAM设置为no,主要是禁止PAM验证,usePam为非对称密钥认证 UsePam,如果是yes的话非对称密钥验证失败,仍然可用口令登录。

更多Docker相关教程见以下内容: 

Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Ubuntu 16.04 服务器上配置使用 Docker  http://www.linuxidc.com/Linux/2017-06/145176.htm 

Ubuntu 15.04下安装Docker  http://www.linuxidc.com/Linux/2015-07/120444.htm 

Docker 安装实例 http://www.linuxidc.com/Linux/2017-04/142666.htm 

Docker 创建基础镜像  http://www.linuxidc.com/Linux/2017-05/144112.htm 

在 Ubuntu 15.04 上如何安装Docker及基本用法 http://www.linuxidc.com/Linux/2015-09/122885.htm 

Ubuntu 16.04上Docker使用手记 http://www.linuxidc.com/Linux/2016-12/138490.htm 

使用Docker分分钟启动常用应用  http://www.linuxidc.com/Linux/2017-04/142649.htm 

Ubuntu 16.04下Docker修改配置文件不生效解决办法  http://www.linuxidc.com/Linux/2017-05/143862.htm 

企鹅博客
  • 本文由 发表于 2019年9月8日 05:03:52
  • 转载请务必保留本文链接:https://www.qieseo.com/169684.html

发表评论