Docker运行centos7

发布时间 2023-12-03 14:32:25作者: 小小酥oAo

Docker 安装 centos7 :

步骤:

1.使用Docker创建虚拟网络:

# 创建一个网段为192.168.0.0/24的虚拟网络,名字为centosNet
docker network create --subnet=192.168.0.0/24  centosNet

# 查看
docker network ls

2.创建容器;

#"--privileged=true" 参数使得容器具有对主机系统设备和内核功能的完全访问权限。
#容器运行的是 CentOS 7 镜像,主进程为 "/sbin/init"。

docker run -itd --name node2 --net centosNet --ip 192.168.0.2 --privileged=true centos:7.9.2009 /sbin/init

3.安装ssh服务:

yum -y install net-tools openssh-server openssh-clients vim 

4.配置sshd_config文件开启ssh端口和监听服务,配置容器密码:

vim /etc/ssh/sshd_config

#将文件中,关于监听端口、监听地址前的 # 号去除
 
Port 22                    //端口
#AddressFamily any
ListenAddress 0.0.0.0      //监听地址
ListenAddress : :


# 保存退出后启动sshd服务
systemctl start sshd
# 查看sshd服务状态
systemctl status sshd


遇到的问题:

每次启动容器后都需要,source /etc/profile;

解决办法:在 ~/.bashrc 里面加一句

source /etc/profile

2.解决systemctl不能用的问题:

# 替换:(whereis systemctl)
wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/bin/systemctl

#
chmod a+x /usr/bin/systemctl