有时候需要在alpine镜像中使用sshd以进行远程登录。

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
FROM alpine:3.5
RUN apk add --no-cache openssh

# for sshd
RUN set -ex \
    && sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config \
    && ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key \
    && ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_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 \
    && chmod 600 \
        /etc/ssh/ssh_host_rsa_key \
        /etc/ssh/ssh_host_dsa_key \
        /etc/ssh/ssh_host_ecdsa_key \
        /etc/ssh/ssh_host_ed25519_key \
    && chmod 644 \
        /etc/ssh/ssh_host_rsa_key.pub \
        /etc/ssh/ssh_host_dsa_key.pub \
        /etc/ssh/ssh_host_ecdsa_key.pub \
        /etc/ssh/ssh_host_ed25519_key.pub \
    && chown root:root /var/empty \
    && chmod 711 /var/empty \
    && echo "root:admin" | chpasswd

entrypoint.sh

1
/usr/sbin/sshd -p 2200 # 可以指定 2200 端口,默认使用22

参考链接:

  1. 基于alpine用dockerfile创建的ssh镜像

  2. Running OpenSSH in an Alpine Docker Container