当前位置:   article > 正文

用 Dockerfile为镜像添加SSH服务

用 Dockerfile为镜像添加SSH服务

1、基础镜像ubuntu:18.04

2、替换为国内的安装源

3、安装openssh-server

4、允许root用户远程登陆

5、暴露端口22

6、服务开机自启动

1.创建目录

[root@openEuler-node1 db]# mkdir sshd_ubuntu

2.创建 Dockerfile、 run.sh 、authorized_keys、vim aliyun.list 文件

  1. [root@openEuler-node1 sshd_ubuntu]# cd sshd_ubuntu
  2. [root@openEuler-node1 sshd_ubuntu]# touch Dockerfile run.sh authorized_keys vim aliyun.list

3.在宿主主机上生成 SSH 密钥对,写入authorized_keys

  1. [root@openEuler-node1 ~]# ssh-keygen -t rsa
  2. [root@openEuler-node1 sshd_ubuntu]# cd sshd_ubuntu
  3. [root@openEuler-node1 sshd_ubuntu]# cat ~/.ssh/id_rsa.pub > authorized_keys

4.编写更改Ubuntu的源为国内aliyun源

  1. [root@openEuler-node1 sshd_ubuntu]# vim aliyun.list
  2. deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
  3. deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
  4. deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
  5. deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
  6. deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  7. deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
  8. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
  9. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
  10. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
  11. deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

5.编写run.sh

  1. #!/bin/bash
  2. /usr/sbin/sshd -D

5.编写 Dockerfile

  1. [root@openEuler-node1 sshd_ubuntu]# vim Dockerfile
  2. FROM ubuntu:18.04
  3. MAINTAINER yuj<yj@qq.com>
  4. COPY aliyun.list /etc/apt/sources.list.d/aliyun.list
  5. RUN apt update && \
  6. apt install -y openssh-server && \
  7. mkdir /var/run/sshd && \
  8. sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd && \
  9. mkdir /root/.ssh
  10. COPY authorized_keys /root/.ssh/
  11. COPY run.sh /run.sh
  12. RUN chmod 755 /run.sh
  13. EXPOSE 22
  14. CMD ["/run.sh"]

6.制作镜像并启动容器

  1. [root@openEuler-node1 sshd_ubuntu]# docker build -t sshd:ubuntu_v1 ./
  2. [root@openEuler-node1 sshd_ubuntu]# docker run -d -P sshd:ubuntu_v1

7.查看容器运行状态

  1. [root@openEuler-node1 sshd_ubuntu]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 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连接登陆这个容器

  1. [root@openEuler-node1 sshd_ubuntu]# ssh 192.168.136.55 -p 32776
  2. The authenticity of host '[192.168.136.55]:32776 ([192.168.136.55]:32776)' can't be established.
  3. ED25519 key fingerprint is SHA256:PCuiPOPbts35IzrOQ3PvZsU0+W+i7O1zheVc1XmDgHU.
  4. This key is not known by any other names
  5. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  6. Warning: Permanently added '[192.168.136.55]:32776' (ED25519) to the list of known hosts.
  7. Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.10.0-182.0.0.95.oe2203sp3.x86_64 x86_64)
  8. root@3df76cc3dedd:~#

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/214553
推荐阅读
  

闽ICP备14008679号