1、下载alpine镜像
1 2 3 4 5 6 7 8 9 10 | [root@docker43 ~] # docker pull alpine Using default tag: latest Trying to pull repository docker.io /library/alpine ... latest: Pulling from docker.io /library/alpine 4fe2ade4980c: Pull complete Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528 Status: Downloaded newer image for docker.io /alpine :latest [root@docker43 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io /alpine latest 196d12cf6ab1 3 weeks ago 4.41 MB |
2、编写dockerfile
2.1.创建一个工作目录
1 2 3 4 5 6 | [root@docker43 ~] # cd /opt/ [root@docker43 opt] # mkdir alpine_ssh && cd alpine_ssh && touch Dockerfile [root@docker43 alpine_ssh] # ll 总用量 4 -rw-r--r-- 1 root root 654 10月 3 23:21 Dockerfile |
2.2.编写Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # 指定创建的基础镜像 FROM alpine # 作者描述信息 MAINTAINER alpine_sshd ([email protected]) # 替换阿里云的源 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 并且同步时间 RUN apk update && \ apk add --no-cache openssh-server tzdata && \ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ 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 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" ] |
2.3.创建镜像
1 2 3 4 | # 在dockerfile所在的目录下 [root@docker43 alpine_ssh] # pwd /opt/alpine_ssh [root@docker43 alpine_ssh] # docker build -t alpine:sshd . |