Docker 创建 centos 容器,启动 sshd 服务

一、下载 centos image (镜像)

docker pull centos

二、创建 centos container (容器)

docker run --hostname mycentos \
  --name cos7 -p 3322:22 \
  --restart=always \
  --privileged=true \
  -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  centos /usr/sbin/init &

为了在 centos 容器中使用 systemctl,需要两个条件:

1、需要包含 Volume(卷) /sys/fs/cgroup
  如果不包含这个卷,执行 systemctl 会报错:
Couldn't find an alternative telinit implementation to spawn。

2、需要在 run 时加 --privileged,运行在特权模式,容器内的 root 用户不再是普通用户,拥有真正的 root 权限。
  如果不加此选项,那么在启动时,挂载卷的时候会报错:
Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
[!!!!!!] Failed to mount API filesystems, freezing.

其它参数说明:
--hostname,-h:指定容器中的 centos 操作系统主机名
--name:指定容器的名称,用 docker container list --all 显示时,最后一列即为容器名称
--publish,-p:指定网络端口映射
--restart:值为 always 为了使容器随 docker 服务启动时,自动运行

三、进入 centos 容器,安装 ssh server

docker container exec -it cos7 bash

[root@mycentos /]yum install openssh-server
[root@mycentos /]vi /etc/ssh/sshd_config
/** 打开选项 PasswordAuthentication yes **/
[root@mycentos /]systemctl start sshd
[root@mycentos /]useradd username -p password
/** 注意密码要符合检查要求,太简单的密码不会生效 **/

客户端 ssh 登录时,如果提示:
Permission denied, please try again.
有可能是 useradd 时,指定的密码太简单,没有生效。重设用户密码的命令是 passwd username,一定要确认密码是否修改成功。

 

参考:https://stackoverflow.com/questions/36545105/docker-couldnt-find-an-alternative-telinit-implementation-to-spawn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值