参考:https://blog.csdn.net/AMimiDou_212/article/details/106502393
一、Dockerfile
FROM alpine:3.13
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update \
&& apk add --no-cache openssh tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config \
&& ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key \
&& ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key \
&& ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key \
&& ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key \
&& echo "root:admin" | chpasswd
# 开放22端口
EXPOSE 22
# 容器启动时执行ssh启动命令
CMD ["/usr/sbin/sshd", "-D"]
二、构建镜像
[root@centos7 alpine-ssh]# docker build -t alpine-ssh:3.13 .
三、使用
运行一个容器
[root@centos7 alpine-ssh]# docker run --name alipne-ssh -d -p 3022:22 alpine-ssh:3.13
xshell连接测试
[root@ubuntu ~]$ ssh [email protected] -p3022
The authenticity of host '[192.168.66.135]:3022 ([192.168.66.135]:3022)' can't be established.
ECDSA key fingerprint is SHA256:cZNrualIfoTpoDrhaeJnvrNTgAWL8Ijfz7VFGk/dXgI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.66.135]:3022' (ECDSA) to the list of known hosts.
[email protected]'s password:
Welcome to Alpine!
The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.
You can setup the system with the command: setup-alpine
You may change this message by editing /etc/motd.
2d37cf68d823:~#
四、镜像大小
15MB,能接受
[root@centos7 alpine-ssh]# docker image ls alpine-ssh
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine-ssh 3.13 ea069908a3f4 6 minutes ago 15.1MB