docker镜像构建sshd服务

系统环境 centos7.4 容器系统centos latest 默认启动服务sshd

一 、基于物理机网络的构建

第一步、新建dockerfile文件

FROM docker.io/centos
RUN yum -y install openssh-server && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key && \
sed -i "/^#Port 22/cPort 1022" /etc/ssh/sshd_config && \
echo "root:root" | chpasswd && \
yum clean all
CMD ["/usr/sbin/sshd", "-D"]

注意事项:绑定物理机网络需要后使用的IP为物理机的地址,需要注意容器服务的端口和物理机的端口是否冲突
sed -i “/^#Port 22/cPort 1022” 查询以#Port 22为开头的行,并替换为Port 1022
第二步、构建镜像

docker build -t sshd .

-t选项后跟镜像的名字 . 代表当前dockerfile所在目录的路径为当前目录
第三步、启动容器

docker run --privileged -i -d --net host --name 容器名 镜像id

备注:–net 参数指定指定使用物理网络 值为:host

二、绑定固定IP的镜像

参考资料:https://blog.csdn.net/qq_34021712/article/details/75948566
第一步:创建自定义网络

docker network create --subnet=172.172.0.0/24 docker-br0

创建网桥之后,使用ifconfig查看 会多出一个网桥,该网桥在docker启动或者重启之后,会自动显示出来。永久的,可以使用docker network rm docker-br0 移除网桥。
第二步、新建dockerfile文件

from docker.io/centos
RUN yum -y install openssh-server && \
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key && \
echo "root:root" | chpasswd && \
yum clean all
CMD ["/usr/sbin/sshd", "-D"]

备注:命令解释
echo “用户名:密码” | 修改用户密码的命令
echo “root:root” | chpasswd
第三步、构建镜像

docker build -t sshd .

第四步、以固定IP启动容器

docker run --privileged -i -d --net docker-br0 --ip 172.172.0.10 --name 容器名 镜像id

–privileged选项以root权限启动容器

三、sftpd镜像(alpine、东八区时区)

dockerfile文件内容

from alpine
run set -ex \
    \
#修改时区
    && apk add --no-cache --virtual tzdata \
    && cp -r -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && rm -rf /usr/share/zoneinfo/* \
#安装远程服务
    && apk add --no-cache --virtual openssh \
    #修改sshd服务端口号:目的(防止启动容器时使用主机网络--net host ,造成端口冲突)
    && sed -i "/^#Port 22/cPort 1022" /etc/ssh/sshd_config \
    #允许root用户远程登陆
    && sed -i "/^#PermitRootLogin/cPermitRootLogin yes" /etc/ssh/sshd_config \
    && sed -i "/^#StrictModes yes/cStrictModes yes" /etc/ssh/sshd_config \
    && sed -i "/^#LoginGraceTime 2m/cLoginGraceTime 2m" /etc/ssh/sshd_config \
    #处理sshd服务启动报错(sshd re-exec requires execution with an absolute path)
    && 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 \
    #修改root用户密码“用户名:密码”
    && echo "root:root" | chpasswd
cmd ["/usr/sbin/sshd", "-D"]

apk镜像搜索
https://pkgs.alpinelinux.org/packages?name=geos&branch=edge

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值