在 Docker 中安装 Centos7 操作系统
最编程
2024-04-08 11:21:14
...
Docker中安装Centos7操作系统
如果还没安装Docker请移步到:
Centos7.x安装Docker
我的服务器版本
[root@shendu ~]# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Mon Aug 5 15:09:42 2019
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-102.git7f2769b.el7.centos.x86_64
Go version: go1.10.3
Git commit: 7f2769b/1.13.1
Built: Mon Aug 5 15:09:42 2019
OS/Arch: linux/amd64
Experimental: false
[root@shendu ~]#
[root@shendu ~]# uname -a
Linux shendu 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@shendu ~]#
[root@shendu ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@shendu ~]#
说明:
鉴于国内网络问题,拉取Docker镜像十分缓慢,在安装之前需要先设置成国内镜像,可以加速。
一、设置镜像加速器
我使用的是网易的镜像地址:http://hub-mirror.c.163.com
网易云镜像官方地址 https://c.163yun.com/hub#/library/repository/info?repoId=1055
- 在centos7下配置:
[root@shendu ~]#
[root@shendu ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
[root@shendu ~]#
- 然后执行如下两个命令才会生效:
sudo systemctl daemon-reload
sudo systemctl restart docker
- 创建Dockerfile文件:(我直接在root用户目录下创建了)
- 文件内容:
FROM hub.c.163.com/netease_comb/centos:7
MAINTAINER netease
# 更新yum源
RUN yum makecache fast && yum -y update glibc
# 安装常用软件
RUN yum install -y openssh-server vim tar wget curl rsync bzip2 iptables tcpdump less telnet net-tools lsof
# 初始化ssh登陆
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
RUN echo "RSAAuthentication yes" >> /etc/ssh/sshd_config
RUN echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config
RUN yum clean all
# 启动sshd服务并且暴露22端口
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
二、获取Centos7镜像
- 在
Dockerfile
的文件夹下运行:docker build -t centos7-my .
-
centos7-my
是生成的镜像名称 - 如果成功了,执行
docker images
就会看到下面的结果
三、启动容器
- 命令:
docker run -itd --name os1 -p 10000:22 87bd98509c5e /bin/bash
-
--name os1
: 启动容器的容器名称 -
87bd98509c5e
: 镜像id -
-p 10000:22
: 端口映射,将容器22
端口映射到宿主机10000
端口,供后续ssh远程登录
-
四、开启ssh链接
1、进入os1容器:
命令:docker exec -it os1 bash
[root@shendu ~]#
[root@shendu ~]# docker exec -it os1 bash
[root@d6898c947c4b /]#
[root@d6898c947c4b /]#
2、启动ssh:
- 先执行
[root@d6898c947c4b /]# /usr/sbin/sshd -D
- 会出现如下结果:
- 然后执行(回车两次,如果 出现警告可以不用管)
[root@d6898c947c4b /]# /usr/sbin/sshd -D &
- 会出现如下结果:
3、查看是否已经启动成功
使用命令:netstat -ntpl |grep 22
或者 lsof -i:22
[root@d6898c947c4b /]#
[root@d6898c947c4b /]# netstat -ntpl |grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 44/sshd
tcp6 0 0 :::22 :::* LISTEN 44/sshd
[root@d6898c947c4b /]#
[root@d6898c947c4b /]#
[root@d6898c947c4b /]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 44 root 3u IPv4 32153435 0t0 TCP *:ssh (LISTEN)
sshd 44 root 4u IPv6 32153437 0t0 TCP *:ssh (LISTEN)
[root@d6898c947c4b /]#
[root@d6898c947c4b /]#
[root@d6898c947c4b /]#
如果出现以上结果,就算成功了。
五、修改root密码
[root@d6898c947c4b /]#
[root@d6898c947c4b /]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@d6898c947c4b /]#
六、进行ssh远程登录测试
- 1、我使用的是CRT连接的
- 连接成功之后
上一篇: FastGithub 下载和使用
推荐阅读
-
在 Anaconda 虚拟环境中安装 cuda 和 pytorch
-
在 Linux 中安装 mongodb,非常详细 - 四步、启动和关闭
-
在 Nginx 安装中添加 nginx-module-vts 模块。
-
Conda] 解决在 Conda 中安装软件包时遇到的 "收集软件包元数据 "问题解决在 Conda 中安装软件包时遇到的 "收集软件包元数据 "问题 - 简介
-
在 docker 中备份 mysql 容器数据(复制单个表)
-
在本地部署 Milvus-1。在 Docker 中安装 Milvus
-
在 VMware WorkStation(NAS 系统)上安装飞牛操作系统
-
在 Docker 中运行微服务注册中心 Eureka
-
在 ubuntu 中安装库文件和头文件的位置
-
在 Linux 中升级 OpenSSH(编译并安装在 openssh 目录中)