1.创建目录
[root@openeuler-node1 db]# mkdir sshd_ubuntu
2.创建 dockerfile、 run.sh 、authorized_keys、vim aliyun.list 文件
[root@openeuler-node1 sshd_ubuntu]# cd sshd_ubuntu [root@openeuler-node1 sshd_ubuntu]# touch dockerfile run.sh authorized_keys vim aliyun.list
3.在宿主主机上生成 ssh 密钥对,写入authorized_keys
[root@openeuler-node1 ~]# ssh-keygen -t rsa [root@openeuler-node1 sshd_ubuntu]# cd sshd_ubuntu [root@openeuler-node1 sshd_ubuntu]# cat ~/.ssh/id_rsa.pub > authorized_keys
4.编写更改ubuntu的源为国内aliyun源
[root@openeuler-node1 sshd_ubuntu]# vim aliyun.list deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
5.编写run.sh
#!/bin/bash /usr/sbin/sshd -d
5.编写 dockerfile
[root@openeuler-node1 sshd_ubuntu]# vim dockerfile from ubuntu:18.04 maintainer yuj<yj@qq.com> copy aliyun.list /etc/apt/sources.list.d/aliyun.list run apt update && \ apt install -y openssh-server && \ mkdir /var/run/sshd && \ sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd && \ mkdir /root/.ssh copy authorized_keys /root/.ssh/ copy run.sh /run.sh run chmod 755 /run.sh expose 22 cmd ["/run.sh"]
6.制作镜像并启动容器
[root@openeuler-node1 sshd_ubuntu]# docker build -t sshd:ubuntu_v1 ./ [root@openeuler-node1 sshd_ubuntu]# docker run -d -p sshd:ubuntu_v1
7.查看容器运行状态
[root@openeuler-node1 sshd_ubuntu]# docker ps container id image command created status ports names 3df76cc3dedd sshd:ubuntu_v1 "/run.sh" 6 seconds ago up 5 seconds 0.0.0.0:32776->22/tcp, :::32776->22/tcp vigorous_williamson
8.用ssh连接登陆这个容器
[root@openeuler-node1 sshd_ubuntu]# ssh 192.168.136.55 -p 32776 the authenticity of host '[192.168.136.55]:32776 ([192.168.136.55]:32776)' can't be established. ed25519 key fingerprint is sha256:pcuipopbts35izroq3pvzsu0+w+i7o1zhevc1xmdghu. this key is not known by any other names are you sure you want to continue connecting (yes/no/[fingerprint])? yes warning: permanently added '[192.168.136.55]:32776' (ed25519) to the list of known hosts. welcome to ubuntu 18.04.6 lts (gnu/linux 5.10.0-182.0.0.95.oe2203sp3.x86_64 x86_64) root@3df76cc3dedd:~#
到此这篇关于dockerfile为镜像添加ssh服务的实现步骤的文章就介绍到这了,更多相关dockerfile添加ssh服务内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论