alpine 构建ssh最小镜像

  • 创建构建目录
mkdir /mnt/alpine_ssh && cd /mnt/alpine_ssh && touch Dockerfile
  • 创建Dockerfile
vim Docker
# 指定创建的基础镜像
FROM alpine:latest
 
# 作者描述信息
LABEL author="Brian"
 
# 替换阿里云的源
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories

# 更新源、安装openssh 并修改配置文件和生成key,修改root密码admin
RUN apk update && \
    apk add --no-cache openssh tzdata && \ 
    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  
 
# 同步时间
 RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 开放22端口
EXPOSE 22
 
# 执行ssh启动命令
CMD ["/usr/sbin/sshd", "-D"]
  • 构建镜像
cd  /mnt/alpine_ssh  
docker build -t ssh:alpine .
[+] Building 4.3s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 785B                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                             0.0s
 => [1/4] FROM docker.io/library/alpine:latest                                                                                                                               0.0s
 => CACHED [2/4] RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories                                                                     0.0s
 => CACHED [3/4] RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories                                                               0.0s
 => [4/4] RUN apk update &&     apk add --no-cache openssh tzdata &&     cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     sed -i "s/#PermitRootLogin.*/PermitRoot  4.0s
 => exporting to image                                                                                                                                                       0.2s
 => => exporting layers                                                                                                                                                      0.2s
 => => writing image sha256:6e83f452acd14603bc92edac422f1f84d9781ba7863d69076f641570cc21c641                                                                                 0.0s
 => => naming to docker.io/library/ssh:alpine
  • 用构建的镜像创建运行容器
docker run --name ssh -itd --rm ssh:alpine
be67fdeb38590e7c0144e8bb2126f300fdf3f4c93aaf31bd79ef1f16219d7894
docker ps | grep ssh
be67fdeb3859   ssh:alpine             "/usr/sbin/sshd -D"   About a minute ago   Up About a minute   22/tcp    ssh
上一篇:[Alpine] 安装Alpine虚拟机,并安装docker,docker-compose


下一篇:微服务-springcloud一次线上内存优化